Search in sources :

Example 71 with IndexRequestBuilder

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

the class HighlighterSearchIT method testSameContent.

public void testSameContent() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", "title", "type=text,store=true,term_vector=with_positions_offsets"));
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[5];
    for (int i = 0; i < 5; i++) {
        indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource("title", "This is a test on the highlighting bug present in elasticsearch");
    }
    indexRandom(true, indexRequestBuilders);
    for (String type : UNIFIED_AND_NULL) {
        SearchResponse search = client().prepareSearch().setQuery(matchQuery("title", "bug")).highlighter(new HighlightBuilder().field("title", -1, 0).highlighterType(type)).get();
        for (int i = 0; i < 5; i++) {
            assertHighlight(search, i, "title", 0, 1, equalTo("This is a test on the highlighting <em>bug</em> " + "present in elasticsearch"));
        }
    }
}
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 72 with IndexRequestBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterShouldFailIfNoOffsets.

public void testPostingsHighlighterShouldFailIfNoOffsets() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("title").field("type", "text").field("store", true).field("index_options", "docs").endObject().endObject().endObject().endObject()));
    ensureGreen();
    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 test for the postings highlighter");
    }
    indexRandom(true, indexRequestBuilders);
    SearchResponse search = client().prepareSearch().setQuery(matchQuery("title", "this is a test")).highlighter(new HighlightBuilder().field("title")).get();
    assertNoFailures(search);
    assertFailures(client().prepareSearch().setQuery(matchQuery("title", "this is a test")).highlighter(new HighlightBuilder().field("title").highlighterType("postings")), RestStatus.BAD_REQUEST, containsString("the field [title] should be indexed with positions and offsets in the " + "postings list to be used with postings highlighter"));
    assertFailures(client().prepareSearch().setQuery(matchQuery("title", "this is a test")).highlighter(new HighlightBuilder().field("title").highlighterType("postings")), RestStatus.BAD_REQUEST, containsString("the field [title] should be indexed with positions and offsets in the " + "postings list to be used with postings highlighter"));
    //should not fail if there is a wildcard
    assertNoFailures(client().prepareSearch().setQuery(matchQuery("title", "this is a test")).highlighter(new HighlightBuilder().field("tit*").highlighterType("postings")).get());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 73 with IndexRequestBuilder

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

the class HighlighterSearchIT method testFastVectorHighlighterManyDocs.

public void testFastVectorHighlighterManyDocs() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
    ensureGreen();
    int COUNT = between(20, 100);
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[COUNT];
    for (int i = 0; i < COUNT; i++) {
        indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field1", "test " + i);
    }
    logger.info("--> indexing docs");
    indexRandom(true, indexRequestBuilders);
    logger.info("--> searching explicitly on field1 and highlighting on it");
    SearchResponse searchResponse = client().prepareSearch().setSize(COUNT).setQuery(termQuery("field1", "test")).highlighter(new HighlightBuilder().field("field1", 100, 0)).get();
    for (int i = 0; i < COUNT; i++) {
        SearchHit hit = searchResponse.getHits().getHits()[i];
        // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
        assertHighlight(searchResponse, i, "field1", 0, 1, equalTo("<em>test</em> " + hit.getId()));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 74 with IndexRequestBuilder

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

the class InnerHitsIT method testParentChildMultipleLayers.

public void testParentChildMultipleLayers() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", "title", "type=text").addMapping("comment", "_parent", "type=article", "message", "type=text").addMapping("remark", "_parent", "type=comment", "message", "type=text"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource("title", "quick brown fox"));
    requests.add(client().prepareIndex("articles", "comment", "1").setParent("1").setSource("message", "fox eat quick"));
    requests.add(client().prepareIndex("articles", "remark", "1").setParent("1").setRouting("1").setSource("message", "good"));
    requests.add(client().prepareIndex("articles", "article", "2").setSource("title", "big gray elephant"));
    requests.add(client().prepareIndex("articles", "comment", "2").setParent("2").setSource("message", "elephant captured"));
    requests.add(client().prepareIndex("articles", "remark", "2").setParent("2").setRouting("2").setSource("message", "bad"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("remark"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getType(), equalTo("remark"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 75 with IndexRequestBuilder

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

the class InnerHitsIT method testDontExplode.

public void testDontExplode() throws Exception {
    assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1"));
    indexRandom(true, requests);
    QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
    SearchResponse response = client().prepareSearch("index1").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested"));
    client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject().startArray("nested").startObject().field("field", "value1").endObject().endArray().endObject()).setRefreshPolicy(IMMEDIATE).get();
    query = nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
    response = client().prepareSearch("index2").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) 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