Search in sources :

Example 6 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder 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 7 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder 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)

Example 8 with InnerHitBuilder

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

the class InnerHitsIT method testInnerHitsWithIgnoreUnmapped.

public void testInnerHitsWithIgnoreUnmapped() throws Exception {
    assertAcked(prepareCreate("index1").addMapping("parent_type", "nested_type", "type=nested").addMapping("child_type", "_parent", "type=parent_type"));
    createIndex("index2");
    client().prepareIndex("index1", "parent_type", "1").setSource("nested_type", Collections.singletonMap("key", "value")).get();
    client().prepareIndex("index1", "child_type", "2").setParent("1").setSource("{}", XContentType.JSON).get();
    client().prepareIndex("index2", "type", "3").setSource("key", "value").get();
    refresh();
    SearchResponse response = client().prepareSearch("index1", "index2").setQuery(boolQuery().should(nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true).innerHit(new InnerHitBuilder(), true)).should(termQuery("key", "value"))).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    response = client().prepareSearch("index1", "index2").setQuery(boolQuery().should(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true).innerHit(new InnerHitBuilder(), true)).should(termQuery("key", "value"))).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
}
Also used : InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder 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 10 with InnerHitBuilder

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

the class ExpandSearchPhaseTests method testCollapseSingleHit.

public void testCollapseSingleHit() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        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);
        AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
        QueryBuilder originalQuery = randomBoolean() ? null : QueryBuilders.termQuery("foo", "bar");
        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.getRequest().source().query(originalQuery);
        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));
                assertEquals(1, request.requests().size());
                SearchRequest searchRequest = request.requests().get(0);
                assertTrue(searchRequest.source().query() instanceof BoolQueryBuilder);
                BoolQueryBuilder groupBuilder = (BoolQueryBuilder) searchRequest.source().query();
                if (collapseValue == null) {
                    assertThat(groupBuilder.mustNot(), Matchers.contains(QueryBuilders.existsQuery("someField")));
                } else {
                    assertThat(groupBuilder.filter(), Matchers.contains(QueryBuilders.matchQuery("someField", "boom")));
                }
                if (originalQuery != null) {
                    assertThat(groupBuilder.must(), Matchers.contains(QueryBuilders.termQuery("foo", "bar")));
                }
                assertArrayEquals(mockSearchPhaseContext.getRequest().indices(), searchRequest.indices());
                assertArrayEquals(mockSearchPhaseContext.getRequest().types(), searchRequest.types());
                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(response, null) }));
            }
        };
        SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", 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();
        mockSearchPhaseContext.assertNoFailure();
        assertNotNull(reference.get());
        SearchResponse theResponse = reference.get();
        assertSame(theResponse, response);
        assertEquals(1, theResponse.getHits().getHits()[0].getInnerHits().size());
        assertSame(theResponse.getHits().getHits()[0].getInnerHits().get("foobarbaz"), collapsedHits);
        assertTrue(executedMultiSearch.get());
        assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) 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)

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