-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding aggregations in hybrid query #630
Merged
martin-gaievski
merged 2 commits into
opensearch-project:main
from
martin-gaievski:aggregations_in_hybrid_query
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,19 +8,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | |
### Enhancements | ||
### Bug Fixes | ||
- Fix async actions are left in neural_sparse query ([#438](https://github.com/opensearch-project/neural-search/pull/438)) | ||
- Fixed exception for case when Hybrid query being wrapped into bool query ([#490](https://github.com/opensearch-project/neural-search/pull/490)) | ||
- Hybrid query and nested type fields ([#498](https://github.com/opensearch-project/neural-search/pull/498)) | ||
- Fix typo for sparse encoding processor factory([#578](https://github.com/opensearch-project/neural-search/pull/578)) | ||
- Add non-null check for queryBuilder in NeuralQueryEnricherProcessor ([#615](https://github.com/opensearch-project/neural-search/pull/615)) | ||
### Infrastructure | ||
### Documentation | ||
### Maintenance | ||
- Added support for jdk-21 ([#500](https://github.com/opensearch-project/neural-search/pull/500))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as 2 lines above, this is part of 2.12 |
||
### Refactoring | ||
|
||
## [Unreleased 2.x](https://github.com/opensearch-project/neural-search/compare/2.12...2.x) | ||
### Features | ||
### Enhancements | ||
- Adding aggregations in hybrid query ([#630](https://github.com/opensearch-project/neural-search/pull/630)) | ||
### Bug Fixes | ||
- Fix runtime exceptions in hybrid query for case when sub-query scorer return TwoPhase iterator that is incompatible with DISI iterator ([#624](https://github.com/opensearch-project/neural-search/pull/624)) | ||
### Infrastructure | ||
|
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 { | ||
martin-gaievski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ public void testCombination_whenMultipleSubqueriesResultsAndDefaultMethod_thenSc | |
assertNotNull(queryTopDocs); | ||
assertEquals(3, queryTopDocs.size()); | ||
|
||
assertEquals(3, queryTopDocs.get(0).getScoreDocs().size()); | ||
assertEquals(5, queryTopDocs.get(0).getScoreDocs().size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is needed because of the change in a way we count total hits |
||
assertEquals(.5, queryTopDocs.get(0).getScoreDocs().get(0).score, DELTA_FOR_SCORE_ASSERTION); | ||
assertEquals(1, queryTopDocs.get(0).getScoreDocs().get(0).doc); | ||
assertEquals(.5, queryTopDocs.get(0).getScoreDocs().get(1).score, DELTA_FOR_SCORE_ASSERTION); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those PRs were released in 2.12, doing cleanup