Index retrieval code

Source: Internet
Author: User
Introduction to common classes
Directory specifies the Directory where the index is located
FSDirectory file system stored on disk
RAMDirectory is stored in a directory in the memory for testing and other purposes.
For example, Directory directory = FSDirectory. open (new File ("filePath "));

IndexReader reads indexes and initializes indexes based on Directory classes.
For example, IndexReader reader = DirectoryReader. open (directory );

IndexSearcher is used for index retrieval and is initialized by the IndexReader class. Search by Query class.
For example, IndexSearcher searcher = new IndexSearcher (reader );

Analyzer splitter
For example, Analyzer analyzer = new StandardAnalyzer ();

The Term description searches for the Field and content of a word ).
For example, Term term = new Term ("field", "content ");

Query class, abstract class
TermQuery is the simplest and most basic Query used to Query words that are not sharded. Initialize by Term class
For example, Query query = new TermQuery (term );

TopDocs stores the query results of the IndexSearcher class
For example, TopDocs topDocs = searcher. search (query, 10 );

ScoreDoc [] array is used to store the document information in the query results
For example, ScoreDoc [] scoreDocs = topdocs. scoreDocs;

The score attribute in ScoreDoc indicates the degree of correlation. The value range is [0, 1]. The larger the score, the more relevant the score is.

Overview:

Directory directory = FSDirectory. open (new File ("filePath"); IndexReader reader = DirectoryReader. open (directory); IndexSearcher searcher = new IndexSearcher (reader); Analyzer analyzer = new StandardAnalyzer (); Term term = new Term ("field", "content "); query query = new TermQuery (term); TopDocs topDocs = searcher. search (query, 10); ScoreDoc [] hits = topDocs. scoreDocs; for (ScoreDoc hit: hits?system.out.println(hit.doc + hit. score );}


Index retrieval code

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.