Search in sources :

Example 26 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class ReindexParentChildTests method createParentChildIndex.

/**
     * Setup a parent/child index and return a query that should find the child
     * using the parent.
     */
private void createParentChildIndex(String indexName) throws Exception {
    CreateIndexRequestBuilder create = client().admin().indices().prepareCreate(indexName);
    create.addMapping("city", "{\"_parent\": {\"type\": \"country\"}}", XContentType.JSON);
    create.addMapping("neighborhood", "{\"_parent\": {\"type\": \"city\"}}", XContentType.JSON);
    assertAcked(create);
    ensureGreen();
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)

Example 27 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class GeoPointFieldMapperTests method testMultiField.

public void testMultiField() throws Exception {
    int numDocs = randomIntBetween(10, 100);
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("pin").startObject("properties").startObject("location").field("type", "geo_point").startObject("fields").startObject("geohash").field("type", "keyword").endObject().startObject("latlon").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("test").addMapping("pin", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    // create index and add random test points
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    for (int i = 0; i < numDocs; ++i) {
        final GeoPoint pt = RandomGeoGenerator.randomPoint(random());
        client().prepareIndex("test", "pin").setSource(jsonBuilder().startObject().startObject("location").field("lat", pt.lat()).field("lon", pt.lon()).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    }
    // TODO these tests are bogus and need to be Fix
    // query by geohash subfield
    SearchResponse searchResponse = client().prepareSearch().addStoredField("location.geohash").setQuery(matchAllQuery()).execute().actionGet();
    assertEquals(numDocs, searchResponse.getHits().getTotalHits());
    // query by latlon subfield
    searchResponse = client().prepareSearch().addStoredField("location.latlon").setQuery(matchAllQuery()).execute().actionGet();
    assertEquals(numDocs, searchResponse.getHits().getTotalHits());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 28 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class GeoFilterIT method testShapeRelations.

public void testShapeRelations() throws Exception {
    assertTrue("Intersect relation is not supported", intersectSupport);
    assertTrue("Disjoint relation is not supported", disjointSupport);
    assertTrue("within relation is not supported", withinSupport);
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("polygon").startObject("properties").startObject("area").field("type", "geo_shape").field("tree", "geohash").endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("shapes").addMapping("polygon", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    // Create a multipolygon with two polygons. The first is an rectangle of size 10x10
    // with a hole of size 5x5 equidistant from all sides. This hole in turn contains
    // the second polygon of size 4x4 equidistant from all sites
    MultiPolygonBuilder polygon = ShapeBuilders.newMultiPolygon().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    BytesReference data = jsonBuilder().startObject().field("area", polygon).endObject().bytes();
    client().prepareIndex("shapes", "polygon", "1").setSource(data, XContentType.JSON).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    // Point in polygon
    SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(3, 3))).execute().actionGet();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point in polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(4.5, 4.5))).execute().actionGet();
    assertHitCount(result, 0);
    // by definition the border of a polygon belongs to the inner
    // so the border of a polygons hole also belongs to the inner
    // of the polygon NOT the hole
    // Point on polygon border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(10.0, 5.0))).execute().actionGet();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point on hole border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(5.0, 2.0))).execute().actionGet();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    if (disjointSupport) {
        // Point not in polygon
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", ShapeBuilders.newPoint(3, 3))).execute().actionGet();
        assertHitCount(result, 0);
        // Point in polygon hole
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", ShapeBuilders.newPoint(4.5, 4.5))).execute().actionGet();
        assertHitCount(result, 1);
        assertFirstHit(result, hasId("1"));
    }
    // Create a polygon that fills the empty area of the polygon defined above
    PolygonBuilder inverse = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    data = jsonBuilder().startObject().field("area", inverse).endObject().bytes();
    client().prepareIndex("shapes", "polygon", "2").setSource(data, XContentType.JSON).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    // re-check point on polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(4.5, 4.5))).execute().actionGet();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("2"));
    // Create Polygon with hole and common edge
    PolygonBuilder builder = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(10, 5).coordinate(10, -5).close()));
    if (withinSupport) {
        // Polygon WithIn Polygon
        builder = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(-30, -30).coordinate(-30, 30).coordinate(30, 30).coordinate(30, -30).close());
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoWithinQuery("area", builder)).execute().actionGet();
        assertHitCount(result, 2);
    }
    // Create a polygon crossing longitude 180.
    builder = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
    data = jsonBuilder().startObject().field("area", builder).endObject().bytes();
    client().prepareIndex("shapes", "polygon", "1").setSource(data, XContentType.JSON).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    // Create a polygon crossing longitude 180 with hole.
    builder = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, -5).coordinate(185, -5).coordinate(185, 5).coordinate(175, 5).close()));
    data = jsonBuilder().startObject().field("area", builder).endObject().bytes();
    client().prepareIndex("shapes", "polygon", "1").setSource(data, XContentType.JSON).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(174, -4))).execute().actionGet();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(-174, -4))).execute().actionGet();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(180, -4))).execute().actionGet();
    assertHitCount(result, 0);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", ShapeBuilders.newPoint(180, -6))).execute().actionGet();
    assertHitCount(result, 1);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CoordinatesBuilder(org.elasticsearch.common.geo.builders.CoordinatesBuilder) MultiPolygonBuilder(org.elasticsearch.common.geo.builders.MultiPolygonBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) MultiPolygonBuilder(org.elasticsearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.elasticsearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.elasticsearch.common.geo.builders.LineStringBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 29 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project sonarqube by SonarSource.

the class ProxyCreateIndexRequestBuilderTest method trace_logs.

@Test
public void trace_logs() {
    logTester.setLevel(LoggerLevel.TRACE);
    CreateIndexRequestBuilder requestBuilder = esTester.client().prepareCreate(generateNewIndexName());
    requestBuilder.get();
    assertThat(logTester.logs()).hasSize(1);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Test(org.junit.Test)

Example 30 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project play2-elasticsearch by cleverage.

the class IndexService method createIndex.

/**
     * Create the index
     */
public static void createIndex(String indexName) {
    Logger.debug("ElasticSearch : creating index [" + indexName + "]");
    try {
        CreateIndexRequestBuilder creator = IndexClient.client.admin().indices().prepareCreate(indexName);
        String setting = IndexClient.config.indexSettings.get(indexName);
        if (setting != null) {
            creator.setSettings(setting);
        }
        creator.execute().actionGet();
    } catch (Exception e) {
        Logger.error("ElasticSearch : Index create error : " + e.toString());
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException)

Aggregations

CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)30 SearchResponse (org.elasticsearch.action.search.SearchResponse)11 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)11 ArrayList (java.util.ArrayList)8 PhraseSuggestionBuilder (org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Before (org.junit.Before)3 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 DirectCandidateGeneratorBuilder (org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder)2 StupidBackoff (org.elasticsearch.search.suggest.phrase.StupidBackoff)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1