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