private Similarity.SimWeight buildSimWeight(SpanQuery query, IndexSearcher searcher, Map<Term, TermContext> termContexts, float boost) throws IOException { if (termContexts == null || termContexts.size() == 0 || query.getField() == null) return null; TermStatistics[] termStats = new TermStatistics[termContexts.size()]; int i = 0; for (Term term : termContexts.keySet()) { termStats[i] = searcher.termStatistics(term, termContexts.get(term)); i++; } CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField()); return similarity.computeWeight(boost, collectionStats, termStats); }
@Override protected Similarity.SimWeight getStats(IndexSearcher searcher) throws IOException { final IndexReaderContext context = searcher.getTopReaderContext(); // compute idf ArrayList<TermStatistics> allTermStats = new ArrayList<>(); for(final Term[] terms: termArrays) { for (Term term: terms) { TermContext ts = termStates.get(term); if (ts == null) { ts = TermContext.build(context, term); termStates.put(term, ts); } if (needsScores) { TermStatistics termStatistics = searcher.termStatistics(term, ts); if (termStatistics != null) { allTermStats.add(termStatistics); } } } } if (allTermStats.isEmpty()) { return null; // none of the terms were found, we won't use sim at all } else { return similarity.computeWeight( boost, searcher.collectionStatistics(field), allTermStats.toArray(new TermStatistics[allTermStats.size()])); } }
@Override protected Similarity.SimWeight getStats(IndexSearcher searcher) throws IOException { final int[] positions = PhraseQuery.this.getPositions(); if (positions.length < 2) { throw new IllegalStateException("PhraseWeight does not support less than 2 terms, call rewrite first"); } else if (positions[0] != 0) { throw new IllegalStateException("PhraseWeight requires that the first position is 0, call rewrite first"); } final IndexReaderContext context = searcher.getTopReaderContext(); states = new TermContext[terms.length]; TermStatistics termStats[] = new TermStatistics[terms.length]; int termUpTo = 0; for (int i = 0; i < terms.length; i++) { final Term term = terms[i]; states[i] = TermContext.build(context, term); if (needsScores) { TermStatistics termStatistics = searcher.termStatistics(term, states[i]); if (termStatistics != null) { termStats[termUpTo++] = termStatistics; } } } if (termUpTo > 0) { return similarity.computeWeight(boost, searcher.collectionStatistics(field), ArrayUtil.copyOfSubArray(termStats, 0, termUpTo)); } else { return null; // no terms at all, we won't use similarity } }
SynonymWeight(Query query, IndexSearcher searcher, float boost) throws IOException { super(query); CollectionStatistics collectionStats = searcher.collectionStatistics(terms[0].field()); long docFreq = 0; long totalTermFreq = 0; termContexts = new TermContext[terms.length]; for (int i = 0; i < termContexts.length; i++) { termContexts[i] = TermContext.build(searcher.getTopReaderContext(), terms[i]); TermStatistics termStats = searcher.termStatistics(terms[i], termContexts[i]); docFreq = Math.max(termStats.docFreq(), docFreq); if (termStats.totalTermFreq() == -1) { totalTermFreq = -1; } else if (totalTermFreq != -1) { totalTermFreq += termStats.totalTermFreq(); } } TermStatistics pseudoStats = new TermStatistics(null, docFreq, totalTermFreq); this.similarity = searcher.getSimilarity(true); this.simWeight = similarity.computeWeight(boost, collectionStats, pseudoStats); }
public TermWeight(IndexSearcher searcher, boolean needsScores, float boost, TermContext termStates) throws IOException { super(TermQuery.this); if (needsScores && termStates == null) { throw new IllegalStateException("termStates are required when scores are needed"); } this.needsScores = needsScores; this.termStates = termStates; this.similarity = searcher.getSimilarity(needsScores); final CollectionStatistics collectionStats; final TermStatistics termStats; if (needsScores) { collectionStats = searcher.collectionStatistics(term.field()); termStats = searcher.termStatistics(term, termStates); } else { // we do not need the actual stats, use fake stats with docFreq=maxDoc and ttf=-1 final int maxDoc = searcher.getIndexReader().maxDoc(); collectionStats = new CollectionStatistics(term.field(), maxDoc, -1, -1, -1); termStats = new TermStatistics(term.bytes(), maxDoc, -1); } this.stats = similarity.computeWeight(boost, collectionStats, termStats); }
@Override public CollectionStatistics collectionStatistics(String field) throws IOException { if (aggregatedDfs == null) { // we are either executing the dfs phase or the search_type doesn't include the dfs phase. return super.collectionStatistics(field); } CollectionStatistics collectionStatistics = aggregatedDfs.fieldStatistics().get(field); if (collectionStatistics == null) { // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query return super.collectionStatistics(field); } return collectionStatistics; }
final CollectionStatistics collectionStats = searcher.collectionStatistics(term.field()); final TermStatistics termStats = searcher.termStatistics(term, termStates); final Similarity similarity = searcher.getSimilarity(needsScores);
public IndexField(String fieldName, LeafIndexLookup indexLookup) throws IOException { assert fieldName != null; this.fieldName = fieldName; assert indexLookup != null; this.indexLookup = indexLookup; fieldStats = this.indexLookup.getIndexSearcher().collectionStatistics(fieldName); }
@Override public CollectionStatistics collectionStatistics(String field) throws IOException { if (aggregatedDfs == null) { // we are either executing the dfs phase or the search_type doesn't include the dfs phase. return super.collectionStatistics(field); } CollectionStatistics collectionStatistics = aggregatedDfs.fieldStatistics().get(field); if (collectionStatistics == null) { // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query return super.collectionStatistics(field); } return collectionStatistics; }
@Override public CollectionStatistics collectionStatistics(String field) throws IOException { if (aggregatedDfs == null) { // we are either executing the dfs phase or the search_type doesn't include the dfs phase. return super.collectionStatistics(field); } CollectionStatistics collectionStatistics = aggregatedDfs.fieldStatistics().get(field); if (collectionStatistics == null) { // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query return super.collectionStatistics(field); } return collectionStatistics; }
@Override public CollectionStatistics collectionStatistics(String field) throws IOException { if (aggregatedDfs == null) { // we are either executing the dfs phase or the search_type doesn't include the dfs phase. return super.collectionStatistics(field); } CollectionStatistics collectionStatistics = aggregatedDfs.fieldStatistics().get(field); if (collectionStatistics == null) { // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query return super.collectionStatistics(field); } return collectionStatistics; }
private Similarity.SimWeight buildSimWeight(CustomSpanQuery query, IndexSearcher searcher, Map<Term, TermContext> termContexts) throws IOException { if (termContexts == null || termContexts.size() == 0 || query.getField() == null) return null; TermStatistics[] termStats = new TermStatistics[termContexts.size()]; int i = 0; for (Term term : termContexts.keySet()) { termStats[i] = searcher.termStatistics(term, termContexts.get(term)); i++; } CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField()); return similarity.computeWeight(collectionStats, termStats); }
private Similarity.SimWeight buildSimWeight(SpanQuery query, IndexSearcher searcher, Map<Term, TermContext> termContexts, float boost) throws IOException { if (termContexts == null || termContexts.size() == 0 || query.getField() == null) return null; TermStatistics[] termStats = new TermStatistics[termContexts.size()]; int i = 0; for (Term term : termContexts.keySet()) { termStats[i] = searcher.termStatistics(term, termContexts.get(term)); i++; } CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField()); return similarity.computeWeight(boost, collectionStats, termStats); }
private Similarity.SimWeight buildSimWeight(SpanQuery query, IndexSearcher searcher, Map<Term, TermContext> termContexts) throws IOException { if (termContexts == null || termContexts.size() == 0 || query.getField() == null) return null; TermStatistics[] termStats = new TermStatistics[termContexts.size()]; int i = 0; for (Term term : termContexts.keySet()) { termStats[i] = searcher.termStatistics(term, termContexts.get(term)); i++; } CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField()); return searcher.getSimilarity(true).computeWeight(collectionStats, termStats); }
private Similarity.SimWeight buildSimWeight(SpanQuery query, IndexSearcher searcher, Map<Term, TermContext> termContexts) throws IOException { if (termContexts == null || termContexts.size() == 0 || query.getField() == null) return null; TermStatistics[] termStats = new TermStatistics[termContexts.size()]; int i = 0; for (Term term : termContexts.keySet()) { termStats[i] = searcher.termStatistics(term, termContexts.get(term)); i++; } CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField()); return searcher.getSimilarity(true).computeWeight(collectionStats, termStats); }
public NodePhraseWeight(final IndexSearcher searcher) throws IOException { this.similarity = searcher.getSimilarity(); final IndexReaderContext context = searcher.getTopReaderContext(); states = new TermContext[terms.size()]; final TermStatistics termStats[] = new TermStatistics[terms.size()]; for (int i = 0; i < terms.size(); i++) { final Term term = terms.get(i); states[i] = TermContext.build(context, term, true); termStats[i] = searcher.termStatistics(term, states[i]); } stats = similarity.computeWeight(NodePhraseQuery.this.getBoost(), searcher.collectionStatistics(field), termStats); }
public NodePhraseWeight(final IndexSearcher searcher) throws IOException { this.similarity = searcher.getSimilarity(); final IndexReaderContext context = searcher.getTopReaderContext(); states = new TermContext[terms.size()]; final TermStatistics termStats[] = new TermStatistics[terms.size()]; for (int i = 0; i < terms.size(); i++) { final Term term = terms.get(i); states[i] = TermContext.build(context, term); termStats[i] = searcher.termStatistics(term, states[i]); } stats = similarity.computeWeight(NodePhraseQuery.this.getBoost(), searcher.collectionStatistics(field), termStats); }
public TermSpanWeight(final IndexSearcher searcher, final TermContext termStates) throws IOException { assert termStates != null : "TermContext must not be null"; this.termStates = termStates; this.similarity = searcher.getSimilarity(); this.stats = similarity.computeWeight( TermSpanQuery.this.getBoost(), searcher.collectionStatistics(term.field()), searcher.termStatistics(term, termStates)); }
public NodeTermWeight(final IndexSearcher searcher, final TermContext termStates) throws IOException { assert termStates != null : "TermContext must not be null"; this.termStates = termStates; this.similarity = searcher.getSimilarity(); this.stats = similarity.computeWeight( NodeTermQuery.this.getBoost(), searcher.collectionStatistics(term.field()), searcher.termStatistics(term, termStates)); }
public NodeTermWeight(final IndexSearcher searcher, final TermContext termStates) throws IOException { assert termStates != null : "TermContext must not be null"; this.termStates = termStates; this.similarity = searcher.getSimilarity(); this.stats = similarity.computeWeight( NodeTermQuery.this.getBoost(), searcher.collectionStatistics(term.field()), searcher.termStatistics(term, termStates)); }