Search in sources :

Example 76 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testRandomNested.

public void testRandomNested() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
    int numDocs = scaledRandomIntBetween(25, 100);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int[] field1InnerObjects = new int[numDocs];
    int[] field2InnerObjects = new int[numDocs];
    for (int i = 0; i < numDocs; i++) {
        int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        XContentBuilder source = jsonBuilder().startObject().startArray("field1");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        source.endArray().startArray("field2");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        source.endArray().endObject();
        requestBuilders.add(client().prepareIndex("idx", "type", String.format(Locale.ENGLISH, "%03d", i)).setSource(source));
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)), false));
    boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size), false));
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) field1InnerObjects[i]));
        for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) field2InnerObjects[i]));
        for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 77 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testEscapeHtml.

public void testEscapeHtml() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", "title", "type=text,store=true"));
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[5];
    for (int i = 0; i < indexRequestBuilders.length; i++) {
        indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource("title", "This is a html escaping highlighting test for *&? elasticsearch");
    }
    indexRandom(true, indexRequestBuilders);
    for (String type : UNIFIED_AND_NULL) {
        SearchResponse search = client().prepareSearch().setQuery(matchQuery("title", "test")).highlighter(new HighlightBuilder().encoder("html").field("title", 50, 1, 10).highlighterType(type)).get();
        for (int i = 0; i < indexRequestBuilders.length; i++) {
            assertHighlight(search, i, "title", 0, 1, startsWith("This is a html escaping highlighting <em>test</em> for *&amp;?"));
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 78 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class MultiMatchQueryIT method testFuzzyFieldLevelBoosting.

/**
     * Test for edge case where field level boosting is applied to field that doesn't exist on documents on
     * one shard. There was an issue reported in https://github.com/elastic/elasticsearch/issues/18710 where a
     * `multi_match` query using the fuzziness parameter with a boost on one of two fields returns the
     * same document score if both documents are placed on different shard. This test recreates that scenario
     * and checks that the returned scores are different.
     */
public void testFuzzyFieldLevelBoosting() throws InterruptedException, ExecutionException {
    String idx = "test18710";
    CreateIndexRequestBuilder builder = prepareCreate(idx).setSettings(Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 3).put(SETTING_NUMBER_OF_REPLICAS, 0));
    assertAcked(builder.addMapping("type", "title", "type=text", "body", "type=text"));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex(idx, "type", "1").setSource("title", "foo", "body", "bar"));
    builders.add(client().prepareIndex(idx, "type", "2").setSource("title", "bar", "body", "foo"));
    indexRandom(true, false, builders);
    SearchResponse searchResponse = client().prepareSearch(idx).setExplain(true).setQuery(multiMatchQuery("foo").field("title", 100).field("body").fuzziness(0)).get();
    SearchHit[] hits = searchResponse.getHits().getHits();
    assertNotEquals("both documents should be on different shards", hits[0].getShard().getShardId(), hits[1].getShard().getShardId());
    assertEquals("1", hits[0].getId());
    assertEquals("2", hits[1].getId());
    assertThat(hits[0].getScore(), greaterThan(hits[1].getScore()));
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 79 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class QueryStringIT method testBasicAllQuery.

public void testBasicAllQuery() throws Exception {
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo bar baz"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f2", "Bar"));
    reqs.add(client().prepareIndex("test", "doc", "3").setSource("f3", "foo bar baz"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(queryStringQuery("foo")).get();
    assertHitCount(resp, 2L);
    assertHits(resp.getHits(), "1", "3");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("bar")).get();
    assertHitCount(resp, 2L);
    assertHits(resp.getHits(), "1", "3");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("Bar")).get();
    assertHitCount(resp, 3L);
    assertHits(resp.getHits(), "1", "2", "3");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("foa")).get();
    assertHitCount(resp, 1L);
    assertHits(resp.getHits(), "3");
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 80 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class QueryStringIT method testDocWithAllTypes.

public void testDocWithAllTypes() throws Exception {
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    String docBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-example-document.json");
    reqs.add(client().prepareIndex("test", "doc", "1").setSource(docBody, XContentType.JSON));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(queryStringQuery("foo")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("Bar")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("Baz")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("sbaz")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("19")).get();
    assertHits(resp.getHits(), "1");
    // nested doesn't match because it's hidden
    resp = client().prepareSearch("test").setQuery(queryStringQuery("1476383971")).get();
    assertHits(resp.getHits(), "1");
    // bool doesn't match
    resp = client().prepareSearch("test").setQuery(queryStringQuery("7")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("23")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("1293")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("42")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("1.7")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("1.5")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("12.23")).get();
    assertHits(resp.getHits(), "1");
    resp = client().prepareSearch("test").setQuery(queryStringQuery("127.0.0.1")).get();
    assertHits(resp.getHits(), "1");
// binary doesn't match
// suggest doesn't match
// geo_point doesn't match
// geo_shape doesn't match
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15