-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix script score queries not getting cached
Signed-off-by: Junqiu Lei <[email protected]>
- Loading branch information
1 parent
feb7968
commit 818715c
Showing
8 changed files
with
235 additions
and
70 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
69 changes: 69 additions & 0 deletions
69
src/main/java/org/opensearch/knn/plugin/script/KNNScoreScriptLeafFactory.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,69 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.script; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.opensearch.knn.plugin.stats.KNNCounter; | ||
import org.opensearch.script.ScoreScript; | ||
import org.opensearch.search.lookup.SearchLookup; | ||
|
||
public class KNNScoreScriptLeafFactory implements ScoreScript.LeafFactory { | ||
private final Map<String, Object> params; | ||
private final SearchLookup lookup; | ||
private String similaritySpace; | ||
private String field; | ||
private Object query; | ||
private KNNScoringSpace knnScoringSpace; | ||
|
||
private IndexSearcher searcher; | ||
|
||
public KNNScoreScriptLeafFactory(Map<String, Object> params, SearchLookup lookup, IndexSearcher searcher) { | ||
KNNCounter.SCRIPT_QUERY_REQUESTS.increment(); | ||
this.params = params; | ||
this.lookup = lookup; | ||
this.field = getValue(params, "field").toString(); | ||
this.similaritySpace = getValue(params, "space_type").toString(); | ||
this.query = getValue(params, "query_value"); | ||
this.searcher = searcher; | ||
|
||
this.knnScoringSpace = KNNScoringSpaceFactory.create( | ||
this.similaritySpace, | ||
this.query, | ||
lookup.doc().mapperService().fieldType(this.field) | ||
); | ||
} | ||
|
||
private Object getValue(Map<String, Object> params, String fieldName) { | ||
final Object value = params.get(fieldName); | ||
if (value != null) return value; | ||
|
||
KNNCounter.SCRIPT_QUERY_ERRORS.increment(); | ||
throw new IllegalArgumentException("Missing parameter [" + fieldName + "]"); | ||
} | ||
|
||
@Override | ||
public boolean needs_score() { | ||
return false; | ||
} | ||
|
||
/** | ||
* For each segment, supply the KNNScoreScript that should be used to re-score the documents returned from the | ||
* query. Because the method to score the documents was set during factory construction, the scripts are agnostic of | ||
* the similarity space. The KNNScoringSpace will return the correct script, given the query, the field type, and | ||
* the similarity space. | ||
* | ||
* @param ctx LeafReaderContext for the segment | ||
* @return ScoreScript to be executed | ||
*/ | ||
@Override | ||
public ScoreScript newInstance(LeafReaderContext ctx) throws IOException { | ||
return knnScoringSpace.getScoreScript(params, field, lookup, ctx, this.searcher); | ||
} | ||
} |
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
Oops, something went wrong.