forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
188 additions
and
8 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
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
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
48 changes: 48 additions & 0 deletions
48
server/src/main/java/org/elasticsearch/script/TermsStatsReader.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,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script; | ||
|
||
import org.apache.lucene.index.PostingsEnum; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.index.TermStates; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class TermsStatsReader { | ||
private final Map<Term, TermStates> termStates; | ||
|
||
private final Map<Term, PostingsEnum> postingsEnums; | ||
|
||
public TermsStatsReader(Map<Term, TermStates> termStates, Map<Term, PostingsEnum> postingsEnums) { | ||
this.termStates = termStates; | ||
this.postingsEnums = postingsEnums; | ||
} | ||
|
||
public Map<Term, Integer> docFrequencies() { | ||
return termStates.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().docFreq())); | ||
} | ||
|
||
public Map<Term, Integer> termFrequencies(int docId) throws IOException { | ||
Map<Term, Integer> termFreqs = new HashMap<>(); | ||
|
||
for (Term term: postingsEnums.keySet()) { | ||
PostingsEnum postingsEnum = postingsEnums.get(term); | ||
if (postingsEnum.advance(docId) == docId){ | ||
termFreqs.put(term, postingsEnum.freq()); | ||
} else { | ||
termFreqs.put(term, 0); | ||
} | ||
} | ||
|
||
return termFreqs; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
server/src/main/java/org/elasticsearch/script/TermsStatsReaderProvider.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,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute; | ||
import org.apache.lucene.index.IndexReaderContext; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.PostingsEnum; | ||
import org.apache.lucene.index.ReaderUtil; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.index.TermStates; | ||
import org.apache.lucene.index.TermsEnum; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class TermsStatsReaderProvider { | ||
|
||
private final SearchLookup searchLookup; | ||
|
||
private final LeafReaderContext leafReaderContext; | ||
|
||
private final Map<CacheKey, TermsStatsReader> termsStatsReaderCache = new HashMap<>(); | ||
|
||
public TermsStatsReaderProvider(SearchLookup searchLookup, LeafReaderContext leafReaderContext) { | ||
this.searchLookup = searchLookup; | ||
this.leafReaderContext = leafReaderContext; | ||
} | ||
|
||
public TermsStatsReader termStatsReader(String fieldName, String query) throws IOException { | ||
CacheKey cacheKey = new CacheKey(fieldName, query); | ||
|
||
if (termsStatsReaderCache.containsKey(cacheKey) == false) { | ||
termsStatsReaderCache.put(cacheKey, buildTermStatsReader(fieldName, query)); | ||
} | ||
|
||
return termsStatsReaderCache.get(cacheKey); | ||
} | ||
|
||
private TermsStatsReader buildTermStatsReader(String fieldName, String query) throws IOException { | ||
|
||
IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(leafReaderContext); | ||
|
||
final Map<Term, TermStates> termStates = new HashMap<>(); | ||
final Map<Term, PostingsEnum> postingsEnums = new HashMap<>(); | ||
|
||
try( | ||
Analyzer analyzer = searchLookup.fieldType(fieldName).getTextSearchInfo().searchAnalyzer(); | ||
TokenStream ts = analyzer.tokenStream(fieldName, query); | ||
) { | ||
TermToBytesRefAttribute termAttr = ts.getAttribute(TermToBytesRefAttribute.class); | ||
ts.reset(); | ||
while (ts.incrementToken()) { | ||
Term term = new Term(fieldName, termAttr.getBytesRef()); | ||
TermStates termContext = searchLookup.getTermStates(term); | ||
termStates.put(term, termContext); | ||
|
||
TermsEnum termsEnum = leafReaderContext.reader().terms(term.field()).iterator(); | ||
termsEnum.seekExact(term.bytes(), termContext.get(leafReaderContext)); | ||
postingsEnums.put(term, termsEnum.postings(null, PostingsEnum.ALL)); | ||
} | ||
} | ||
|
||
return new TermsStatsReader(termStates, postingsEnums); | ||
} | ||
|
||
private record CacheKey(String fieldName, String query) { } | ||
} |
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