Search in sources :

Example 1 with InnerHitBuilder

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

the class InnerHitsIT method testNestedMultipleLayers.

public void testNestedMultipleLayers() 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").endObject().startObject("remarks").field("type", "nested").startObject("properties").startObject("message").field("type", "text").endObject().endObject().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").startArray("remarks").startObject().field("message", "good").endObject().endArray().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").startArray("remarks").startObject().field("message", "bad").endObject().endArray().endObject().endArray().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("remark"), false), ScoreMode.Avg).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("comments");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    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(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
    // Directly refer to the second level:
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg).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("comments.remarks");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    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(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("remark"), false), ScoreMode.Avg).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("comments");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    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(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
}
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 2 with InnerHitBuilder

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

the class InnerHitsIT method testNestedInnerHitWrappedInParentChildInnerhit.

public void testNestedInnerHitWrappedInParentChildInnerhit() throws Exception {
    assertAcked(prepareCreate("test").addMapping("child_type", "_parent", "type=parent_type", "nested_type", "type=nested"));
    client().prepareIndex("test", "parent_type", "1").setSource("key", "value").get();
    client().prepareIndex("test", "child_type", "2").setParent("1").setSource("nested_type", Collections.singletonMap("key", "value")).get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(boolQuery().must(matchQuery("key", "value")).should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false))).get();
    assertHitCount(response, 1);
    SearchHit hit = response.getHits().getAt(0);
    assertThat(hit.getInnerHits().get("child_type").getAt(0).field("_parent").getValue(), equalTo("1"));
    assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with InnerHitBuilder

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

the class InnerHitsIT method testInnerHitsOnHasParent.

public void testInnerHitsOnHasParent() throws Exception {
    assertAcked(prepareCreate("stack").addMapping("question", "body", "type=text").addMapping("answer", "_parent", "type=question", "body", "type=text"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("stack", "question", "1").setSource("body", "I'm using HTTPS + Basic authentication " + "to protect a resource. How can I throttle authentication attempts to protect against brute force attacks?"));
    requests.add(client().prepareIndex("stack", "answer", "1").setParent("1").setSource("body", "install fail2ban and enable rules for apache"));
    requests.add(client().prepareIndex("stack", "question", "2").setSource("body", "I have firewall rules set up and also denyhosts installed.\\ndo I also need to install fail2ban?"));
    requests.add(client().prepareIndex("stack", "answer", "2").setParent("2").setSource("body", "Denyhosts protects only ssh; Fail2Ban protects all daemons."));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("stack").setTypes("answer").addSort("_uid", SortOrder.ASC).setQuery(boolQuery().must(matchQuery("body", "fail2ban")).must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder(), false))).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    SearchHit searchHit = response.getHits().getAt(0);
    assertThat(searchHit.getId(), equalTo("1"));
    assertThat(searchHit.getType(), equalTo("answer"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits(), equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getType(), equalTo("question"));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("1"));
    searchHit = response.getHits().getAt(1);
    assertThat(searchHit.getId(), equalTo("2"));
    assertThat(searchHit.getType(), equalTo("answer"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits(), equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getType(), equalTo("question"));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("2"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with InnerHitBuilder

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

the class InnerHitsIT method testSimpleParentChild.

public void testSimpleParentChild() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", "title", "type=text").addMapping("comment", "_parent", "type=article", "message", "type=text,fielddata=true"));
    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", "comment", "2").setParent("1").setSource("message", "fox ate rabbit x y z"));
    requests.add(client().prepareIndex("articles", "comment", "3").setParent("1").setSource("message", "rabbit got away"));
    requests.add(client().prepareIndex("articles", "article", "2").setSource("title", "big gray elephant"));
    requests.add(client().prepareIndex("articles", "comment", "4").setParent("2").setSource("message", "elephant captured"));
    requests.add(client().prepareIndex("articles", "comment", "5").setParent("2").setSource("message", "mice squashed by elephant x"));
    requests.add(client().prepareIndex("articles", "comment", "6").setParent("2").setSource("message", "elephant scared by mice x y"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getShard(), notNullValue());
    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.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(1).getId(), equalTo("2"));
    assertThat(innerHits.getAt(1).getType(), equalTo("comment"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), 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(3L));
    assertThat(innerHits.getAt(0).getId(), equalTo("4"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(1).getId(), equalTo("5"));
    assertThat(innerHits.getAt(1).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(2).getId(), equalTo("6"));
    assertThat(innerHits.getAt(2).getType(), equalTo("comment"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder().addDocValueField("message").setHighlightBuilder(new HighlightBuilder().field("message")).setExplain(true).setSize(1).addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())), false)).get();
    assertNoFailures(response);
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getHighlightFields().get("message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
    assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(message:fox"));
    assertThat(innerHits.getAt(0).getFields().get("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 5 with InnerHitBuilder

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

the class InnerHitsIT method testRandomParentChild.

public void testRandomParentChild() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("parent").addMapping("child1", "_parent", "type=parent").addMapping("child2", "_parent", "type=parent"));
    int numDocs = scaledRandomIntBetween(5, 50);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int child1 = 0;
    int child2 = 0;
    int[] child1InnerObjects = new int[numDocs];
    int[] child2InnerObjects = new int[numDocs];
    for (int parent = 0; parent < numDocs; parent++) {
        String parentId = String.format(Locale.ENGLISH, "%03d", parent);
        requestBuilders.add(client().prepareIndex("idx", "parent", parentId).setSource("{}", XContentType.JSON));
        int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        int limit = child1 + numChildDocs;
        for (; child1 < limit; child1++) {
            requestBuilders.add(client().prepareIndex("idx", "child1", String.format(Locale.ENGLISH, "%04d", child1)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
        numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        limit = child2 + numChildDocs;
        for (; child2 < limit; child2++) {
            requestBuilders.add(client().prepareIndex("idx", "child2", String.format(Locale.ENGLISH, "%04d", child2)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).setTypes("parent").addSort("_uid", SortOrder.ASC).setQuery(boolQuery).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    int offset1 = 0;
    int offset2 = 0;
    for (int parent = 0; parent < numDocs; parent++) {
        SearchHit searchHit = searchResponse.getHits().getAt(parent);
        assertThat(searchHit.getType(), equalTo("parent"));
        assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) child1InnerObjects[parent]));
        for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child1"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset1 += child1InnerObjects[parent];
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) child2InnerObjects[parent]));
        for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child2"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset2 += child2InnerObjects[parent];
    }
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchHits(org.elasticsearch.search.SearchHits) 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