Search in sources :

Example 11 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class ExpandSearchPhaseTests method testFailOneItemFailsEntirePhase.

public void testFailOneItemFailsEntirePhase() throws IOException {
    AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
    SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap()) }, 1, 1.0F);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    String collapseValue = randomBoolean() ? null : "boom";
    mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
    mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            assertTrue(executedMultiSearch.compareAndSet(false, true));
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
            listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(response, null) }));
        }
    };
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))) }, 1, 1.0F);
    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
    SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
    AtomicReference<SearchResponse> reference = new AtomicReference<>();
    ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            reference.set(r);
        }
    });
    phase.run();
    assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
    assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
    assertNotNull(mockSearchPhaseContext.phaseFailure.get());
    assertNull(reference.get());
    assertEquals(0, mockSearchPhaseContext.phasesExecuted.get());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchHits(org.elasticsearch.search.SearchHits) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHitField(org.elasticsearch.search.SearchHitField) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 12 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class CollapseBuilderTests method randomCollapseBuilder.

public static CollapseBuilder randomCollapseBuilder() {
    CollapseBuilder builder = new CollapseBuilder(randomAsciiOfLength(10));
    builder.setMaxConcurrentGroupRequests(randomIntBetween(1, 48));
    if (randomBoolean()) {
        InnerHitBuilder innerHit = InnerHitBuilderTests.randomInnerHits(false, false);
        builder.setInnerHits(innerHit);
    }
    return builder;
}
Also used : InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder)

Example 13 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class CollapseBuilderTests method testBuild.

public void testBuild() throws IOException {
    Directory dir = new RAMDirectory();
    try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
        writer.commit();
    }
    SearchContext searchContext = mockSearchContext();
    try (IndexReader reader = DirectoryReader.open(dir)) {
        when(searchContext.getQueryShardContext().getIndexReader()).thenReturn(reader);
        MappedFieldType numberFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
        MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType();
        for (MappedFieldType fieldType : new MappedFieldType[] { numberFieldType, keywordFieldType }) {
            fieldType.setName("field");
            fieldType.setHasDocValues(true);
            when(searchContext.getQueryShardContext().fieldMapper("field")).thenReturn(fieldType);
            CollapseBuilder builder = new CollapseBuilder("field");
            CollapseContext collapseContext = builder.build(searchContext);
            assertEquals(collapseContext.getFieldType(), fieldType);
            fieldType.setIndexOptions(IndexOptions.NONE);
            collapseContext = builder.build(searchContext);
            assertEquals(collapseContext.getFieldType(), fieldType);
            fieldType.setHasDocValues(false);
            SearchContextException exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
            assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
            fieldType.setHasDocValues(true);
            builder.setInnerHits(new InnerHitBuilder());
            exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
            assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, " + "only indexed field can retrieve `inner_hits`");
        }
    }
}
Also used : InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchContextException(org.elasticsearch.search.SearchContextException) RAMDirectory(org.apache.lucene.store.RAMDirectory) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 14 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testSimpleNested.

public void testSimpleNested() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments").field("type", "nested").startObject("properties").startObject("message").field("type", "text").field("fielddata", true).endObject().endObject().endObject().startObject("title").field("type", "text").endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("articles", "article", "2").setSource(jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject().field("message", "elephant captured").endObject().startObject().field("message", "mice squashed by elephant x").endObject().startObject().field("message", "elephant scared by mice x y").endObject().endArray().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), 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(2L));
    assertThat(innerHits.getHits().length, equalTo(2));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("1"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getShard(), notNullValue());
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(3L));
    assertThat(innerHits.getHits().length, equalTo(3));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("2"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(innerHits.getAt(2).getId(), equalTo("2"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getOffset(), equalTo(2));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message")).setExplain(true).addDocValueField("comments.message").addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).setSize(1), false)).get();
    assertNoFailures(response);
    innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
    assertThat(innerHits.getTotalHits(), equalTo(2L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
    assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(comments.message:fox in"));
    assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
    assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 15 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testNestedSourceFiltering.

public void testNestedSourceFiltering() throws Exception {
    assertAcked(prepareCreate("index1").addMapping("message", "comments", "type=nested"));
    client().prepareIndex("index1", "message", "1").setSource(jsonBuilder().startObject().field("message", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()).get();
    refresh();
    // the field name (comments.message) used for source filtering should be the same as when using that field for
    // other features (like in the query dsl or aggs) in order for consistency:
    SearchResponse response = client().prepareSearch().setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder().setFetchSourceContext(new FetchSourceContext(true, new String[] { "comments.message" }, null)), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getTotalHits(), equalTo(2L));
    assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getSourceAsMap()), equalTo("fox eat quick"));
    assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(1).getSourceAsMap()), equalTo("fox ate rabbit x y z"));
}
Also used : InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)22 SearchResponse (org.elasticsearch.action.search.SearchResponse)17 ArrayList (java.util.ArrayList)13 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)13 SearchHits (org.elasticsearch.search.SearchHits)10 SearchHit (org.elasticsearch.search.SearchHit)8 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)6 ElasticsearchAssertions.assertSearchHit (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)4 IOException (java.io.IOException)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Text (org.elasticsearch.common.text.Text)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 Script (org.elasticsearch.script.Script)2 SearchHitField (org.elasticsearch.search.SearchHitField)2 CollapseBuilder (org.elasticsearch.search.collapse.CollapseBuilder)2 InternalSearchResponse (org.elasticsearch.search.internal.InternalSearchResponse)2