Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene: how to get the score of a document

Tags:

lucene

I want to output the score of documents. The code I write for this is that:

IndexReader reader = IndexReader.open(FSDirectory.open(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new IKAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_31, "title",
            analyzer);
Query q = null;
q = parser.parse("MacOS");
TopDocs docs = searcher.search(q, 10);
ScoreDoc[] hits = docs.scoreDocs;
for(int i=0;i<hits.length;++i){
  System.out.println(hits[i].score);
}

but the output is NaN. I want to know how to get the score of the document.

like image 515
remy Avatar asked Mar 19 '12 13:03

remy


People also ask

Where can I find the scoring model for Lucene?

The authoritative document for scoring is found on the Lucene site here. Read that first. Lucene implements a variant of the Tf-Idf scoring model. That is documented here. The factors involved in Lucene's scoring algorithm are as follows: tf = term frequency in document = measure of how often a term appears in the document

How does Lucene determine relevancy of a document?

Lucene uses a combination of the Vector Space Model (VSM) of Information Retrieval and the Boolean model to determine how relevant a document is to a user's query. It assigns a default score between 0 and 1 to all search results, depending on multiple factors related to document relevancy.

How do I boost Lucene search results?

Lucene allows influencing search results by "boosting" in more than one level: Document level boosting - while indexing - by calling document.setBoost () before a document is added to the index.

How do I implement custom scoring in Sitefinity CMS Lucene?

To implement the custom Lucene scoring you need to plug in to the Sitefinity CMS LuceneSearchService and replace the default scoring algorithm with a custom one that inherits form the Lucene CustomScoreQuery class. To create a custom score query, you must start by adding a new class which inherits from the Lucene CustomScoreProvider.


2 Answers

additional to daulets answere you have to enable the scoring in the indexSearcher:

...
searcher.setDefaultFieldSortScoring(true, true);
...

I think thats what you meant remy, but that way it should be clearer :)

like image 88
jet Avatar answered Oct 19 '22 16:10

jet


        IndexReader reader = IndexReader.open(FSDirectory.open(indexDir));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new IKAnalyzer();
        QueryParser parser = new QueryParser(Version.LUCENE_31, "title", analyzer);
        Query q = null;
        q = parser.parse("MacOS");
        TopDocs docs = searcher.search(q, 10);
        ScoreDoc[] filterScoreDosArray = docs.topDocs().scoreDocs;
        for (int i = 0; i < filterScoreDosArray.length; ++i) {
            int docId = filterScoreDosArray[i].doc;
            Document d = is.doc(docId);
            System.out.println((i + 1) + ". " + d.get("docno")+" Score: "+ filterScoreDosArray[i].score);
        }

try this.

like image 42
Daulet Avatar answered Oct 19 '22 16:10

Daulet