use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class CollapseBuilderTests method testBuild.
public void testBuild() throws IOException {
Directory dir = new ByteBuffersDirectory();
try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
writer.commit();
}
QueryShardContext shardContext = mock(QueryShardContext.class);
try (IndexReader reader = DirectoryReader.open(dir)) {
when(shardContext.getIndexReader()).thenReturn(reader);
MappedFieldType numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG);
when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
CollapseBuilder builder = new CollapseBuilder("field");
CollapseContext collapseContext = builder.build(shardContext);
assertEquals(collapseContext.getFieldType(), numberFieldType);
numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, true, false, false, false, null, Collections.emptyMap());
when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, false, false, true, false, null, Collections.emptyMap());
when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
builder.setInnerHits(new InnerHitBuilder());
exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`");
MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field");
when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
CollapseBuilder kbuilder = new CollapseBuilder("field");
collapseContext = kbuilder.build(shardContext);
assertEquals(collapseContext.getFieldType(), keywordFieldType);
keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", true, false, Collections.emptyMap());
when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
exc = expectThrows(IllegalArgumentException.class, () -> kbuilder.build(shardContext));
assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", false, true, Collections.emptyMap());
when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
kbuilder.setInnerHits(new InnerHitBuilder());
exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`");
}
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class CollapseBuilder method innerToXContent.
private void innerToXContent(XContentBuilder builder) throws IOException {
builder.field(FIELD_FIELD.getPreferredName(), field);
if (maxConcurrentGroupRequests > 0) {
builder.field(MAX_CONCURRENT_GROUP_REQUESTS_FIELD.getPreferredName(), maxConcurrentGroupRequests);
}
if (innerHits.isEmpty() == false) {
if (innerHits.size() == 1) {
builder.field(INNER_HITS_FIELD.getPreferredName(), innerHits.get(0));
} else {
builder.startArray(INNER_HITS_FIELD.getPreferredName());
for (InnerHitBuilder innerHit : innerHits) {
innerHit.toXContent(builder, ToXContent.EMPTY_PARAMS);
}
builder.endArray();
}
}
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class ExpandSearchPhaseTests method testExpandRequestOptions.
public void testExpandRequestOptions() throws IOException {
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
boolean version = randomBoolean();
final boolean seqNoAndTerm = randomBoolean();
mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
final QueryBuilder postFilter = QueryBuilders.existsQuery("foo");
assertTrue(request.requests().stream().allMatch((r) -> "foo".equals(r.preference())));
assertTrue(request.requests().stream().allMatch((r) -> "baz".equals(r.routing())));
assertTrue(request.requests().stream().allMatch((r) -> version == r.source().version()));
assertTrue(request.requests().stream().allMatch((r) -> seqNoAndTerm == r.source().seqNoAndPrimaryTerm()));
assertTrue(request.requests().stream().allMatch((r) -> postFilter.equals(r.source().postFilter())));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().fetchSource() == false));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().includes().length == 0));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().excludes().length == 0));
}
};
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz").setVersion(version).setSeqNoAndPrimaryTerm(seqNoAndTerm))).fetchSource(false).postFilter(QueryBuilders.existsQuery("foo"))).preference("foobar").routing("baz");
SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, internalSearchResponse, null);
phase.run();
mockSearchPhaseContext.assertNoFailure();
assertNotNull(mockSearchPhaseContext.searchResponse.get());
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class ExpandSearchPhaseTests method testCollapseSingleHit.
public void testCollapseSingleHit() throws IOException {
final int iters = randomIntBetween(5, 10);
for (int i = 0; i < iters; i++) {
final int numInnerHits = randomIntBetween(1, 5);
List<SearchHits> collapsedHits = new ArrayList<>(numInnerHits);
for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(innerHitNum, "ID", Collections.emptyMap(), Collections.emptyMap()), new SearchHit(innerHitNum + 1, "ID", Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), 1.0F);
collapsedHits.add(hits);
}
AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
QueryBuilder originalQuery = randomBoolean() ? null : QueryBuilders.termQuery("foo", "bar");
final MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
String collapseValue = randomBoolean() ? null : "boom";
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(IntStream.range(0, numInnerHits).mapToObj(hitNum -> new InnerHitBuilder().setName("innerHit" + hitNum)).collect(Collectors.toList()))));
mockSearchPhaseContext.getRequest().source().query(originalQuery);
mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
assertTrue(executedMultiSearch.compareAndSet(false, true));
assertEquals(numInnerHits, 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());
List<MultiSearchResponse.Item> mSearchResponses = new ArrayList<>(numInnerHits);
for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits.get(innerHitNum), null, null, null, false, null, 1);
mockSearchPhaseContext.sendSearchResponse(internalSearchResponse, null);
mSearchResponses.add(new MultiSearchResponse.Item(mockSearchPhaseContext.searchResponse.get(), null));
}
listener.onResponse(new MultiSearchResponse(mSearchResponses.toArray(new MultiSearchResponse.Item[0]), randomIntBetween(1, 10000)));
}
};
SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", Collections.singletonMap("someField", new DocumentField("someField", Collections.singletonList(collapseValue))), Collections.emptyMap()) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, internalSearchResponse, null);
phase.run();
mockSearchPhaseContext.assertNoFailure();
SearchResponse theResponse = mockSearchPhaseContext.searchResponse.get();
assertNotNull(theResponse);
assertEquals(numInnerHits, theResponse.getHits().getHits()[0].getInnerHits().size());
for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
assertSame(theResponse.getHits().getHits()[0].getInnerHits().get("innerHit" + innerHitNum), collapsedHits.get(innerHitNum));
}
assertTrue(executedMultiSearch.get());
}
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class HasChildQueryBuilderTests method testFromJson.
public void testFromJson() throws IOException {
String query = "{\n" + " \"has_child\" : {\n" + " \"query\" : {\n" + " \"range\" : {\n" + " \"mapped_string\" : {\n" + " \"from\" : \"agJhRET\",\n" + " \"to\" : \"zvqIq\",\n" + " \"include_lower\" : true,\n" + " \"include_upper\" : true,\n" + " \"boost\" : 1.0\n" + " }\n" + " }\n" + " },\n" + " \"type\" : \"child\",\n" + " \"score_mode\" : \"avg\",\n" + " \"min_children\" : 883170873,\n" + " \"max_children\" : 1217235442,\n" + " \"ignore_unmapped\" : false,\n" + " \"boost\" : 2.0,\n" + " \"_name\" : \"WNzYMJKRwePuRBh\",\n" + " \"inner_hits\" : {\n" + " \"name\" : \"inner_hits_name\",\n" + " \"ignore_unmapped\" : false,\n" + " \"from\" : 0,\n" + " \"size\" : 100,\n" + " \"version\" : false,\n" + " \"seq_no_primary_term\" : false,\n" + " \"explain\" : false,\n" + " \"track_scores\" : false,\n" + " \"sort\" : [ {\n" + " \"mapped_string\" : {\n" + " \"order\" : \"asc\"\n" + " }\n" + " } ]\n" + " }\n" + " }\n" + "}";
HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query);
checkGeneratedJson(query, queryBuilder);
assertEquals(query, queryBuilder.maxChildren(), 1217235442);
assertEquals(query, queryBuilder.minChildren(), 883170873);
assertEquals(query, queryBuilder.boost(), 2.0f, 0.0f);
assertEquals(query, queryBuilder.queryName(), "WNzYMJKRwePuRBh");
assertEquals(query, queryBuilder.childType(), "child");
assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg);
assertNotNull(query, queryBuilder.innerHit());
InnerHitBuilder expected = new InnerHitBuilder("child").setName("inner_hits_name").setSize(100).addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC));
assertEquals(query, queryBuilder.innerHit(), expected);
}
Aggregations