Search in sources :

Example 1 with DefaultSimilarity

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;
}
Also used : Terms(org.apache.lucene.index.Terms) DefaultSimilarity(org.apache.lucene.search.similarities.DefaultSimilarity) TermVectorResponse(org.elasticsearch.action.termvector.TermVectorResponse) StopWatch(org.apache.commons.lang3.time.StopWatch) TermsEnum(org.apache.lucene.index.TermsEnum)

Aggregations

StopWatch (org.apache.commons.lang3.time.StopWatch)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 DefaultSimilarity (org.apache.lucene.search.similarities.DefaultSimilarity)1 TermVectorResponse (org.elasticsearch.action.termvector.TermVectorResponse)1