use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.
the class CustomFieldQuery method flatten.
@Override
protected void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
if (sourceQuery instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) sourceQuery;
sourceQuery = bq.getQuery();
boost *= bq.getBoost();
flatten(sourceQuery, reader, flatQueries, boost);
} else if (sourceQuery instanceof SpanTermQuery) {
super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
} else if (sourceQuery instanceof ConstantScoreQuery) {
flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof FunctionScoreQuery) {
flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
} else if (sourceQuery instanceof MultiPhraseQuery) {
MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
} else if (sourceQuery instanceof BlendedTermQuery) {
final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
} else if (sourceQuery instanceof org.apache.lucene.queries.function.FunctionScoreQuery) {
org.apache.lucene.queries.function.FunctionScoreQuery funcScoreQuery = (org.apache.lucene.queries.function.FunctionScoreQuery) sourceQuery;
// flatten query with query boost
flatten(funcScoreQuery.getWrappedQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof SynonymQuery) {
// SynonymQuery should be handled by the parent class directly.
// This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
SynonymQuery synQuery = (SynonymQuery) sourceQuery;
for (Term term : synQuery.getTerms()) {
flatten(new TermQuery(term), reader, flatQueries, boost);
}
} else if (sourceQuery instanceof OpenSearchToParentBlockJoinQuery) {
Query childQuery = ((OpenSearchToParentBlockJoinQuery) sourceQuery).getChildQuery();
if (childQuery != null) {
flatten(childQuery, reader, flatQueries, boost);
}
} else {
super.flatten(sourceQuery, reader, flatQueries, boost);
}
}
use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testToParentBlockJoinQuery.
public void testToParentBlockJoinQuery() {
TermQuery termQuery = new TermQuery(new Term("field", "value"));
QueryBitSetProducer queryBitSetProducer = new QueryBitSetProducer(new TermQuery(new Term("_type", "child")));
OpenSearchToParentBlockJoinQuery query = new OpenSearchToParentBlockJoinQuery(termQuery, queryBitSetProducer, ScoreMode.None, "child");
Result result = analyze(query, Version.CURRENT);
assertFalse(result.verified);
assertThat(result.minimumShouldMatch, equalTo(1));
assertEquals(1, result.extractions.size());
assertNull(result.extractions.toArray(new QueryExtraction[0])[0].range);
assertEquals(new Term("field", "value"), result.extractions.toArray(new QueryExtraction[0])[0].term);
}
use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.
the class NestedQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
if (context.allowExpensiveQueries() == false) {
throw new OpenSearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
}
ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
if (nestedObjectMapper == null) {
if (ignoreUnmapped) {
return new MatchNoDocsQuery();
} else {
throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
}
}
if (!nestedObjectMapper.nested().isNested()) {
throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
}
final BitSetProducer parentFilter;
Query innerQuery;
ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
if (objectMapper == null) {
parentFilter = context.bitsetFilter(Queries.newNonNestedFilter(context.indexVersionCreated()));
} else {
parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
}
try {
context.nestedScope().nextLevel(nestedObjectMapper);
innerQuery = this.query.toQuery(context);
} finally {
context.nestedScope().previousLevel();
}
// in its child space
if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
}
return new OpenSearchToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.
the class QueryPhaseTests method testMaxScoreQueryVisitor.
public void testMaxScoreQueryVisitor() {
BitSetProducer producer = context -> new FixedBitSet(1);
Query query = new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested");
assertTrue(hasInfMaxScore(query));
query = new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.None, "nested");
assertFalse(hasInfMaxScore(query));
for (Occur occur : Occur.values()) {
query = new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build();
if (occur == Occur.MUST) {
assertTrue(hasInfMaxScore(query));
} else {
assertFalse(hasInfMaxScore(query));
}
query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), occur).build();
if (occur == Occur.MUST) {
assertTrue(hasInfMaxScore(query));
} else {
assertFalse(hasInfMaxScore(query));
}
query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), Occur.FILTER).build();
assertFalse(hasInfMaxScore(query));
query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new SpanTermQuery(new Term("field", "foo")), occur).add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), occur).build();
if (occur == Occur.MUST) {
assertTrue(hasInfMaxScore(query));
} else {
assertFalse(hasInfMaxScore(query));
}
}
}
Aggregations