Search in sources :

Example 41 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class InnerHitsIT method testMatchesQueriesNestedInnerHits.

public void testMatchesQueriesNestedInnerHits() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setMapping(builder));
    ensureGreen();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = randomIntBetween(2, 35);
    requests.add(client().prepareIndex("test").setId("0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
    for (int i = 2; i < numDocs; i++) {
        requests.add(client().prepareIndex("test").setId(String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    }
    indexRandom(true, requests);
    waitForRelocation(ClusterHealthStatus.GREEN);
    QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
    query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)));
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertAllSuccessful(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocs));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits().value, equalTo(2L));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits().value, equalTo(1L));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
    for (int i = 2; i < numDocs; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits().value, equalTo(1L));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 42 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class NestedIT method testExtractInnerHitBuildersWithDuplicatePath.

public void testExtractInnerHitBuildersWithDuplicatePath() throws Exception {
    assertAcked(prepareCreate("idxnullhitnames").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).setMapping("categories", "type=keyword", "name", "type=text", "property", "type=nested"));
    ensureGreen("idxnullhitnames");
    SearchRequestBuilder searchRequestBuilder = client().prepareSearch("idxnullhitnames").setQuery(boolQuery().should(nestedQuery("property", termQuery("property.id", 1D), ScoreMode.None).innerHit(new InnerHitBuilder())).should(nestedQuery("property", termQuery("property.id", 1D), ScoreMode.None).innerHit(new InnerHitBuilder())).should(nestedQuery("property", termQuery("property.id", 1D), ScoreMode.None).innerHit(new InnerHitBuilder())));
    assertFailures(searchRequestBuilder, RestStatus.BAD_REQUEST, containsString("[inner_hits] already contains an entry for key [property]"));
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder)

Example 43 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class CCSDuelIT method testHasParentWithInnerHit.

public void testHasParentWithInnerHit() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder("question", QueryBuilders.matchQuery("tags", "xml"), true);
    hasParentQueryBuilder.innerHit(new InnerHitBuilder("inner"));
    sourceBuilder.query(hasParentQueryBuilder);
    searchRequest.source(sourceBuilder);
    duelSearch(searchRequest, CCSDuelIT::assertHits);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)43 SearchResponse (org.opensearch.action.search.SearchResponse)22 ArrayList (java.util.ArrayList)15 SearchHits (org.opensearch.search.SearchHits)14 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)13 QueryBuilder (org.opensearch.index.query.QueryBuilder)11 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)9 SearchHit (org.opensearch.search.SearchHit)9 OpenSearchAssertions.assertSearchHits (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)7 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)6 CollapseBuilder (org.opensearch.search.collapse.CollapseBuilder)5 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)5 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)5 OpenSearchAssertions.assertSearchHit (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit)5 TotalHits (org.apache.lucene.search.TotalHits)4 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)3 WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)3