forked from opensearch-project/neural-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding aggregations in hybrid query (opensearch-project#630)
* Adding aggregations in hybrid query Signed-off-by: Martin Gaievski <[email protected]> (cherry picked from commit f04c058)
- Loading branch information
1 parent
6a68b94
commit d3ce13c
Showing
12 changed files
with
1,044 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/org/opensearch/neuralsearch/util/HybridQueryUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.neuralsearch.util; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.lucene.search.BooleanClause; | ||
import org.apache.lucene.search.BooleanQuery; | ||
import org.apache.lucene.search.FieldExistsQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.opensearch.index.mapper.SeqNoFieldMapper; | ||
import org.opensearch.index.search.NestedHelper; | ||
import org.opensearch.neuralsearch.query.HybridQuery; | ||
import org.opensearch.search.internal.SearchContext; | ||
|
||
/** | ||
* Utility class for anything related to hybrid query | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class HybridQueryUtil { | ||
|
||
public static boolean isHybridQuery(final Query query, final SearchContext searchContext) { | ||
if (query instanceof HybridQuery) { | ||
return true; | ||
} else if (isWrappedHybridQuery(query) && hasNestedFieldOrNestedDocs(query, searchContext)) { | ||
/* Checking if this is a hybrid query that is wrapped into a Bool query by core Opensearch code | ||
https://github.com/opensearch-project/OpenSearch/blob/main/server/src/main/java/org/opensearch/search/DefaultSearchContext.java#L367-L370. | ||
main reason for that is performance optimization, at time of writing we are ok with loosing on performance if that's unblocks | ||
hybrid query for indexes with nested field types. | ||
in such case we consider query a valid hybrid query. Later in the code we will extract it and execute as a main query for | ||
this search request. | ||
below is sample structure of such query: | ||
Boolean { | ||
should: { | ||
hybrid: { | ||
sub_query1 {} | ||
sub_query2 {} | ||
} | ||
} | ||
filter: { | ||
exists: { | ||
field: "_primary_term" | ||
} | ||
} | ||
} | ||
TODO Need to add logic for passing hybrid sub-queries through the same logic in core to ensure there is no latency regression */ | ||
// we have already checked if query in instance of Boolean in higher level else if condition | ||
return ((BooleanQuery) query).clauses() | ||
.stream() | ||
.filter(clause -> clause.getQuery() instanceof HybridQuery == false) | ||
.allMatch(clause -> { | ||
return clause.getOccur() == BooleanClause.Occur.FILTER | ||
&& clause.getQuery() instanceof FieldExistsQuery | ||
&& SeqNoFieldMapper.PRIMARY_TERM_NAME.equals(((FieldExistsQuery) clause.getQuery()).getField()); | ||
}); | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean hasNestedFieldOrNestedDocs(final Query query, final SearchContext searchContext) { | ||
return searchContext.mapperService().hasNested() && new NestedHelper(searchContext.mapperService()).mightMatchNestedDocs(query); | ||
} | ||
|
||
private static boolean isWrappedHybridQuery(final Query query) { | ||
return query instanceof BooleanQuery | ||
&& ((BooleanQuery) query).clauses().stream().anyMatch(clauseQuery -> clauseQuery.getQuery() instanceof HybridQuery); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.