Skip to content
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

Add inner hits support to hybrid query #776

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Optimize parameter parsing in text chunking processor ([#733](https://github.com/opensearch-project/neural-search/pull/733))
- Use lazy initialization for priority queue of hits and scores to improve latencies by 20% ([#746](https://github.com/opensearch-project/neural-search/pull/746))
- Optimize max score calculation in the Query Phase of the Hybrid Search ([765](https://github.com/opensearch-project/neural-search/pull/765))
- Add inner hits support to hybrid query ([#776](https://github.com/opensearch-project/neural-search/pull/776))
### Bug Fixes
- Total hit count fix in Hybrid Query ([756](https://github.com/opensearch-project/neural-search/pull/756))
- Fix map type validation issue in multiple pipeline processors ([#661](https://github.com/opensearch-project/neural-search/pull/661))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

Expand All @@ -23,6 +24,7 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.query.AbstractQueryBuilder;
import org.opensearch.index.query.InnerHitContextBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.QueryShardContext;
Expand Down Expand Up @@ -310,4 +312,18 @@ public void visit(QueryBuilderVisitor visitor) {
subQueryBuilder.visit(subVisitor);
}
}

/**
* Extracts the inner hits from the query tree
* @param innerHits
*/
@Override
protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) {
if (Objects.isNull(queries)) {
return;
}
for (QueryBuilder query : queries) {
InnerHitContextBuilder.extractInnerHits(query, innerHits);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.opensearch.neuralsearch.query.NeuralQueryBuilder.MODEL_ID_FIELD;
import static org.opensearch.neuralsearch.query.NeuralQueryBuilder.QUERY_TEXT_FIELD;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -42,7 +43,10 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.mapper.TextFieldMapper;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.InnerHitContextBuilder;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.index.query.MatchQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.index.query.QueryShardContext;
Expand Down Expand Up @@ -712,13 +716,28 @@ public void testBoost_whenDefaultBoostSet_thenBuildSuccessfully() {
assertNotNull(hybridQueryBuilder);
}

public void testVisit() {
public void testVisit_whenMultipleSubqueries_thenSuccessful() {
HybridQueryBuilder hybridQueryBuilder = new HybridQueryBuilder().add(new NeuralQueryBuilder()).add(new NeuralSparseQueryBuilder());
List<QueryBuilder> visitedQueries = new ArrayList<>();
hybridQueryBuilder.visit(createTestVisitor(visitedQueries));
assertEquals(3, visitedQueries.size());
}

public void testInnerHits_whenSingleSubquery_thenSuccessful() {
HybridQueryBuilder hybridQueryBuilderNeuralQuery = new HybridQueryBuilder().add(new NeuralQueryBuilder());
Map<String, InnerHitContextBuilder> innerHitsBuilderDefault = new HashMap<>();
hybridQueryBuilderNeuralQuery.extractInnerHitBuilders(innerHitsBuilderDefault);
assertNotNull(innerHitsBuilderDefault);
martin-gaievski marked this conversation as resolved.
Show resolved Hide resolved
}

public void testInnerHits_whenMultipleSubqueries_thenSuccessful() {
HybridQueryBuilder hybridQueryBuilder = new HybridQueryBuilder().add(new BoolQueryBuilder())
.add(new MatchQueryBuilder(TEXT_FIELD_NAME, TERM_QUERY_TEXT));
Map<String, InnerHitContextBuilder> innerHitsBuilder = new HashMap<>();
hybridQueryBuilder.extractInnerHitBuilders(innerHitsBuilder);
assertNotNull(innerHitsBuilder);
}

private Map<String, Object> getInnerMap(Object innerObject, String queryName, String fieldName) {
if (!(innerObject instanceof Map)) {
fail("field name does not map to nested object");
Expand Down
Loading
Loading