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"));
}
}
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]"));
}
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);
}
Aggregations