Search in sources :

Example 1 with ChildrenAggregationBuilder

use of org.opensearch.join.aggregations.ChildrenAggregationBuilder in project OpenSearch by opensearch-project.

the class SearchIT method testSearchWithParentJoin.

public void testSearchWithParentJoin() throws IOException {
    final String indexName = "child_example";
    Request createIndex = new Request(HttpPut.METHOD_NAME, "/" + indexName);
    createIndex.setJsonEntity("{\n" + "    \"mappings\": {\n" + "        \"properties\" : {\n" + "            \"qa_join_field\" : {\n" + "                \"type\" : \"join\",\n" + "                \"relations\" : { \"question\" : \"answer\" }\n" + "            }\n" + "        }\n" + "    }" + "}");
    client().performRequest(createIndex);
    Request questionDoc = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/1");
    questionDoc.setJsonEntity("{\n" + "    \"body\": \"<p>I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" + "    \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" + "    \"tags\": [\n" + "        \"windows-server-2003\",\n" + "        \"windows-server-2008\",\n" + "        \"file-transfer\"\n" + "    ],\n" + "    \"qa_join_field\" : \"question\"\n" + "}");
    client().performRequest(questionDoc);
    Request answerDoc1 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/2");
    answerDoc1.addParameter("routing", "1");
    answerDoc1.setJsonEntity("{\n" + "    \"owner\": {\n" + "        \"location\": \"Norfolk, United Kingdom\",\n" + "        \"display_name\": \"Sam\",\n" + "        \"id\": 48\n" + "    },\n" + "    \"body\": \"<p>Unfortunately you're pretty much limited to FTP...\",\n" + "    \"qa_join_field\" : {\n" + "        \"name\" : \"answer\",\n" + "        \"parent\" : \"1\"\n" + "    },\n" + "    \"creation_date\": \"2009-05-04T13:45:37.030\"\n" + "}");
    client().performRequest(answerDoc1);
    Request answerDoc2 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/3");
    answerDoc2.addParameter("routing", "1");
    answerDoc2.setJsonEntity("{\n" + "    \"owner\": {\n" + "        \"location\": \"Norfolk, United Kingdom\",\n" + "        \"display_name\": \"Troll\",\n" + "        \"id\": 49\n" + "    },\n" + "    \"body\": \"<p>Use Linux...\",\n" + "    \"qa_join_field\" : {\n" + "        \"name\" : \"answer\",\n" + "        \"parent\" : \"1\"\n" + "    },\n" + "    \"creation_date\": \"2009-05-05T13:45:37.030\"\n" + "}");
    client().performRequest(answerDoc2);
    client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh"));
    TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names").userValueTypeHint(ValueType.STRING).field("owner.display_name.keyword").size(10);
    ChildrenAggregationBuilder childrenAgg = new ChildrenAggregationBuilder("to-answers", "answer").subAggregation(leafTermAgg);
    TermsAggregationBuilder termsAgg = new TermsAggregationBuilder("top-tags").userValueTypeHint(ValueType.STRING).field("tags.keyword").size(10).subAggregation(childrenAgg);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0).aggregation(termsAgg);
    SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
    assertSearchHeader(searchResponse);
    assertNull(searchResponse.getSuggest());
    assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
    assertEquals(3, searchResponse.getHits().getTotalHits().value);
    assertEquals(0, searchResponse.getHits().getHits().length);
    assertEquals(Float.NaN, searchResponse.getHits().getMaxScore(), 0f);
    assertEquals(1, searchResponse.getAggregations().asList().size());
    Terms terms = searchResponse.getAggregations().get("top-tags");
    assertEquals(0, terms.getDocCountError());
    assertEquals(0, terms.getSumOfOtherDocCounts());
    assertEquals(3, terms.getBuckets().size());
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket.getKeyAsString(), either(equalTo("file-transfer")).or(equalTo("windows-server-2003")).or(equalTo("windows-server-2008")));
        assertEquals(1, bucket.getDocCount());
        assertEquals(1, bucket.getAggregations().asList().size());
        Children children = bucket.getAggregations().get("to-answers");
        assertEquals(2, children.getDocCount());
        assertEquals(1, children.getAggregations().asList().size());
        Terms leafTerms = children.getAggregations().get("top-names");
        assertEquals(0, leafTerms.getDocCountError());
        assertEquals(0, leafTerms.getSumOfOtherDocCounts());
        assertEquals(2, leafTerms.getBuckets().size());
        assertEquals(2, leafTerms.getBuckets().size());
        Terms.Bucket sam = leafTerms.getBucketByKey("Sam");
        assertEquals(1, sam.getDocCount());
        Terms.Bucket troll = leafTerms.getBucketByKey("Troll");
        assertEquals(1, troll.getDocCount());
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) RareTermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.RareTermsAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) CountRequest(org.opensearch.client.core.CountRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) RareTerms(org.opensearch.search.aggregations.bucket.terms.RareTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Matchers.containsString(org.hamcrest.Matchers.containsString) Children(org.opensearch.join.aggregations.Children) ChildrenAggregationBuilder(org.opensearch.join.aggregations.ChildrenAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)1 ExplainRequest (org.opensearch.action.explain.ExplainRequest)1 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)1 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)1 CountRequest (org.opensearch.client.core.CountRequest)1 Children (org.opensearch.join.aggregations.Children)1 ChildrenAggregationBuilder (org.opensearch.join.aggregations.ChildrenAggregationBuilder)1 MultiSearchTemplateRequest (org.opensearch.script.mustache.MultiSearchTemplateRequest)1 SearchTemplateRequest (org.opensearch.script.mustache.SearchTemplateRequest)1 RareTerms (org.opensearch.search.aggregations.bucket.terms.RareTerms)1 RareTermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.RareTermsAggregationBuilder)1 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)1 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1