From b8f794b936577b2f4c23658d99eea6765ad97613 Mon Sep 17 00:00:00 2001 From: Martin Gaievski Date: Tue, 28 Nov 2023 17:26:22 -0800 Subject: [PATCH] Ading comments, minor refactoring Signed-off-by: Martin Gaievski --- .../query/HybridQueryPhaseSearcher.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/neuralsearch/search/query/HybridQueryPhaseSearcher.java b/src/main/java/org/opensearch/neuralsearch/search/query/HybridQueryPhaseSearcher.java index 7b9224c39..6c4568936 100644 --- a/src/main/java/org/opensearch/neuralsearch/search/query/HybridQueryPhaseSearcher.java +++ b/src/main/java/org/opensearch/neuralsearch/search/query/HybridQueryPhaseSearcher.java @@ -79,8 +79,31 @@ private boolean isHybridQuery(final Query query, final SearchContext searchConte if (query instanceof HybridQuery) { return true; } else if (hasNestedFieldOrNestedDocs(query, searchContext) && mightBeWrappedHybridQuery(query)) { - BooleanQuery booleanQuery = (BooleanQuery) query; - return booleanQuery.clauses() + // 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" + // } + // } + // } + if (query instanceof BooleanQuery == false) { + return false; + } + return ((BooleanQuery) query).clauses() .stream() .filter(clause -> clause.getQuery() instanceof HybridQuery == false) .allMatch(