use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class ExpandSearchPhaseTests method testSkipExpandCollapseNoHits.
public void testSkipExpandCollapseNoHits() throws IOException {
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
fail("expand should not try to send empty multi search request");
}
};
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
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 testFailOneItemFailsEntirePhase.
public void testFailOneItemFailsEntirePhase() throws IOException {
AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", Collections.emptyMap(), Collections.emptyMap()), new SearchHit(3, "ID", Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F);
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
String collapseValue = randomBoolean() ? null : "boom";
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
assertTrue(executedMultiSearch.compareAndSet(false, true));
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(searchResponse, null) }, 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 SearchHit(2, "ID2", 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();
assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
assertNotNull(mockSearchPhaseContext.phaseFailure.get());
assertNull(mockSearchPhaseContext.searchResponse.get());
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class CollapseBuilderTests method randomCollapseBuilder.
public static CollapseBuilder randomCollapseBuilder(boolean multiInnerHits) {
CollapseBuilder builder = new CollapseBuilder(randomAlphaOfLength(10));
builder.setMaxConcurrentGroupRequests(randomIntBetween(1, 48));
int numInnerHits = randomIntBetween(0, multiInnerHits ? 5 : 1);
if (numInnerHits == 1) {
InnerHitBuilder innerHit = InnerHitBuilderTests.randomInnerHits();
builder.setInnerHits(innerHit);
} else if (numInnerHits > 1) {
List<InnerHitBuilder> innerHits = new ArrayList<>(numInnerHits);
for (int i = 0; i < numInnerHits; i++) {
innerHits.add(InnerHitBuilderTests.randomInnerHits());
}
builder.setInnerHits(innerHits);
}
return builder;
}
use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.
the class HasChildQueryBuilder method fromXContent.
public static HasChildQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String childType = null;
ScoreMode scoreMode = ScoreMode.None;
int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
String queryName = null;
InnerHitBuilder innerHitBuilder = null;
String currentFieldName = null;
XContentParser.Token token;
QueryBuilder iqb = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
iqb = parseInnerQueryBuilder(parser);
} else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
innerHitBuilder = InnerHitBuilder.fromXContent(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
childType = parser.text();
} else if (SCORE_MODE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
scoreMode = NestedQueryBuilder.parseScoreMode(parser.text());
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (MIN_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
minChildren = parser.intValue(true);
} else if (MAX_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
maxChildren = parser.intValue(true);
} else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
ignoreUnmapped = parser.booleanValue();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
}
}
}
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, scoreMode);
hasChildQueryBuilder.minMaxChildren(minChildren, maxChildren);
hasChildQueryBuilder.queryName(queryName);
hasChildQueryBuilder.boost(boost);
hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
if (innerHitBuilder != null) {
hasChildQueryBuilder.innerHit(innerHitBuilder);
}
return hasChildQueryBuilder;
}
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