Use score for natural sorting in Lucene

Source: Internet
Author: User

/*
* Here is the correlation ranking using Lucene
* Use score for natural sorting
* Correlation ranking is one of the simplest sorting methods.
* The so-called correlation is actually the score of the document
* The score of the document is different for each search,
* It needs to be determined based on the keywords found.
*
*TF Entry Frequency
*IDF Reverse Document Frequency
Incentive factors for *boost field
*lengthnum length Factor

*/




Result diagram:




Package score;


Import java.io.IOException;


Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.document.Field.Index;
Import Org.apache.lucene.index.IndexWriter;
Import Org.apache.lucene.index.Term;
Import org.apache.lucene.search.Hits;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.TermQuery;


public class Native_score {


Public Native_score (String Index_store_path)
{
try{
IndexWriter writer = new IndexWriter (Index_store_path, New StandardAnalyzer (), true);
Writer.setusecompoundfile (FALSE);


Create 3 of documents
Document Doc1 = new document ();
Document DOC2 = new document ();
Document DOC3 = new document ();



Field f1 = new Field ("BookName", "ab BC", Field.Store.YES, index.tokenized);
Field F2 = new Field ("BookName", "AB BC CD", Field.Store.YES, index.tokenized);
Field F3 = new Field ("BookName", "AB", Field.Store.YES, index.tokenized);

Doc1.add (F1);
Doc2.add (F2);
Doc3.add (F3);

Writer.adddocument (Doc1);
Writer.adddocument (DOC2);
Writer.adddocument (DOC3);

Writer.close ();
Indexsearcher searcher = new Indexsearcher (Index_store_path);
Termquery q = new Termquery (New term ("BookName", "BC"));

Hits Hits = Searcher.search (q);
for (int i = 0; i < hits.length (); i++) {
Document doc = Hits.doc (i);
The document to which it is matched
System.out.println (Doc.get ("bookname") + "\t\t");

Score from the document
System.out.println (Hits.score (i));

Here's how this score is going to be understood by explaining the method
System.out.println (Searcher.explain (q, Hits.id (i)). ToString ());

}


}catch (IOException e) {
E.printstacktrace ();
}

}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Native_score NV = new Native_score ("E:\\lucene project \ \ Index file");
}


}

Use score for natural sorting in Lucene

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.