-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Louis Chu <clingzhi@amazon.com>
- Loading branch information
Showing
4 changed files
with
84 additions
and
48 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
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
56 changes: 56 additions & 0 deletions
56
server/src/main/java/org/opensearch/search/lookup/LeafTermFrequencyLookup.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,56 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.lookup; | ||
|
||
import org.apache.lucene.search.IndexSearcher; | ||
import org.opensearch.index.query.functionscore.TermFrequencyFunction; | ||
import org.opensearch.index.query.functionscore.TermFrequencyFunctionFactory; | ||
import org.opensearch.index.query.functionscore.TermFrequencyFunctionFactory.TermFrequencyFunctionName; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* Looks up term frequency per-segment | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class LeafTermFrequencyLookup { | ||
|
||
private final IndexSearcher indexSearcher; | ||
private final LeafSearchLookup leafLookup; | ||
private final Map<String, TermFrequencyFunction> termFreqCache; | ||
|
||
public LeafTermFrequencyLookup(IndexSearcher indexSearcher, LeafSearchLookup leafLookup) { | ||
this.indexSearcher = indexSearcher; | ||
this.leafLookup = leafLookup; | ||
this.termFreqCache = new HashMap<>(); | ||
} | ||
|
||
public Object getTermFrequency(TermFrequencyFunctionName functionName, String field, String val, int docId) throws IOException { | ||
String cacheKey = (val == null) | ||
? String.format(Locale.ROOT, "%s-%s", functionName, field) | ||
: String.format(Locale.ROOT, "%s-%s-%s", functionName, field, val); | ||
|
||
if (!termFreqCache.containsKey(cacheKey)) { | ||
TermFrequencyFunction termFrequencyFunction = TermFrequencyFunctionFactory.createFunction( | ||
functionName, | ||
field, | ||
val, | ||
leafLookup.ctx, | ||
indexSearcher | ||
); | ||
termFreqCache.put(cacheKey, termFrequencyFunction); | ||
} | ||
|
||
return termFreqCache.get(cacheKey).execute(docId); | ||
} | ||
} |