Skip to content

Commit

Permalink
[Backport 1.x]Fix Graph Filter Error in Search (#5665) (#5912)
Browse files Browse the repository at this point in the history
* Fix Graph Filter Error in Search (#5665)

* fix graph filter out of bound error

Signed-off-by: Mingshi Liu <[email protected]>

* add changelog

Signed-off-by: Mingshi Liu <[email protected]>

* run gradle spotlessApply

Signed-off-by: Mingshi Liu <[email protected]>

* reproduce error in unit test

Signed-off-by: Mingshi Liu <[email protected]>

* format to pass spotlessApply

Signed-off-by: Mingshi Liu <[email protected]>

* organize package

Signed-off-by: Mingshi Liu <[email protected]>

Signed-off-by: Mingshi Liu <[email protected]>
(cherry picked from commit 6a7a9a1)

* organize package pass compile test

Signed-off-by: Mingshi Liu <[email protected]>

Signed-off-by: Mingshi Liu <[email protected]>
  • Loading branch information
mingshl authored Jan 18, 2023
1 parent 39389e8 commit d1935d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- `opensearch-service.bat start` and `opensearch-service.bat manager` failing to run ([#4289](https://github.com/opensearch-project/OpenSearch/pull/4289))
- PR reference to checkout code for changelog verifier ([#4296](https://github.com/opensearch-project/OpenSearch/pull/4296))
- `opensearch.bat` and `opensearch-service.bat install` failing to run, missing logs directory ([#4305](https://github.com/opensearch-project/OpenSearch/pull/4305))
- Fix graph filter error in search ([#5665](https://github.com/opensearch-project/OpenSearch/pull/5665))
### Security
## [1.x]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.opensearch.index.mapper.TextFieldMapper;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.query.support.QueryParsers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -743,6 +742,14 @@ private Query analyzeGraphBoolean(String field, TokenStream source, BooleanClaus
lastState = end;
final Query queryPos;
boolean usePrefix = isPrefix && end == -1;
/**
* check if the GraphTokenStreamFiniteStrings graph is empty
* return empty BooleanQuery result
*/
Iterator<TokenStream> graphIt = graph.getFiniteStrings();
if (!graphIt.hasNext()) {
return builder.build();
}
if (graph.hasSidePath(start)) {
final Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
Iterator<Query> queries = new Iterator<Query>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
package org.opensearch.index.query;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CannedBinaryTokenStream;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.CannedBinaryTokenStream;;
import org.apache.lucene.analysis.MockSynonymAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.ExtendedCommonTermsQuery;
Expand All @@ -52,6 +53,7 @@
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings;
import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.opensearch.common.ParsingException;
import org.opensearch.common.Strings;
Expand All @@ -72,6 +74,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Iterator;

import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -326,6 +329,20 @@ public void testFuzzinessOnNonStringField() throws Exception {
query.toQuery(context); // no exception
}

public void testMatchQueryWithNoSidePath() throws Exception {
QueryShardContext testContext = createShardContext();
final MatchQuery testMatchQuery = new MatchQuery(testContext);
MockGraphAnalyzer mockAnalyzer = new MockGraphAnalyzer(createGiantGraphWithNoSide());
testMatchQuery.setAnalyzer(mockAnalyzer);
testMatchQuery.setAutoGenerateSynonymsPhraseQuery(true);
GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(mockAnalyzer.getTokenStream());
Iterator<TokenStream> graphIt = graph.getFiniteStrings();
assertEquals(graphIt.hasNext(), false);
String testField = "Gas Lift Storage Bed Frame with Arched Bed Head in King";
String testValue = "head board, bed head, bedhead, headboard";
testMatchQuery.parse(Type.BOOLEAN, testField, testValue); // no exception
}

public void testExactOnUnsupportedField() throws Exception {
MatchQueryBuilder query = new MatchQueryBuilder(GEO_POINT_FIELD_NAME, "2,3");
QueryShardContext context = createShardContext();
Expand Down Expand Up @@ -543,12 +560,18 @@ private static class MockGraphAnalyzer extends Analyzer {

MockGraphAnalyzer(CannedBinaryTokenStream.BinaryToken[] tokens) {
this.tokenStream = new CannedBinaryTokenStream(tokens);

}

@Override
protected TokenStreamComponents createComponents(String fieldName) {
return new TokenStreamComponents(r -> {}, tokenStream);
}

public CannedBinaryTokenStream getTokenStream() {
return this.tokenStream;
}

}

/**
Expand All @@ -571,6 +594,14 @@ private static CannedBinaryTokenStream.BinaryToken[] createGiantGraph(int numPos
return tokens.toArray(new CannedBinaryTokenStream.BinaryToken[0]);
}

/**
* Creates a graph token stream with no side path.
**/
private static CannedBinaryTokenStream.BinaryToken[] createGiantGraphWithNoSide() {
List<CannedBinaryTokenStream.BinaryToken> tokens = new ArrayList<>();
return tokens.toArray(new CannedBinaryTokenStream.BinaryToken[0]);
}

/**
* Creates a graph token stream with {@link BooleanQuery#getMaxClauseCount()}
* expansions at the last position.
Expand Down

0 comments on commit d1935d9

Please sign in to comment.