use of org.apache.lucene.search.similarities.DefaultSimilarity in project pyramid by cheng-li.
the class ESIndex method getTermStats.
/**
* df is from one shard!!!
* @param id
* @return term statistics from one doc
* @throws IOException
*/
public Set<TermStat> getTermStats(String field, String id) throws IOException {
StopWatch stopWatch = null;
if (logger.isDebugEnabled()) {
stopWatch = new StopWatch();
stopWatch.start();
}
TermVectorResponse response = client.prepareTermVector(indexName, documentType, id).setOffsets(false).setPositions(false).setFieldStatistics(false).setTermStatistics(true).setSelectedFields(field).execute().actionGet();
Terms terms = response.getFields().terms(field);
Set<TermStat> set = new HashSet<>();
// if the field is empty, terms==null
if (terms == null) {
return set;
}
TermsEnum iterator = terms.iterator(null);
for (int i = 0; i < terms.size(); i++) {
String term = iterator.next().utf8ToString();
int tf = iterator.docsAndPositions(null, null).freq();
int df = iterator.docFreq();
DefaultSimilarity defaultSimilarity = new DefaultSimilarity();
/**
* from lucene
*/
/**
* tf is just tf, not square root of tf as in lucene
*/
/** Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. */
float tfidf = tf * defaultSimilarity.idf(df, this.numDocs);
TermStat termStat = new TermStat(term);
termStat.setTf(tf).setDf(df).setTfidf(tfidf);
set.add(termStat);
}
if (logger.isDebugEnabled()) {
logger.debug("time spent on getNgramInfos for " + id + " = " + stopWatch);
}
return set;
}
Aggregations