I have heard of Lucene for a long time, but I have never been able to study it. I have been idle for the past few days. I spent an afternoon studying Lucene and wrote a simple Demo, the general feeling is that it is easy to use. before using it, you must first understand the following key classes:
Document
Document is used to describe a Document. The Document here can refer to an HTML page, an email, or a text file. A Document object consists of multiple Field objects. You can think of a Document object as a record in the database, and each Field object is a record Field.
Field
The Field object is used to describe a certain attribute of a document. For example, the title and content of an email can be described by two Field objects.
Analyzer
Before a document is indexed, you must first perform word segmentation on the document content, which is done by Analyzer. The Analyzer class is an abstract class that has multiple implementations. Select the appropriate Analyzer for different languages and applications. Analyzer submits the segmented content to IndexWriter to create an index.
IndexWriter
IndexWriter is a core class used by Lucene to create indexes. It is used to add Document objects to indexes.
Directory
This class represents the storage location of Lucene indexes. This is an abstract class. It currently has two implementations. The first is FSDirectory, which represents the location of an index stored in the file system. The second is RAMDirectory, which indicates the location of an index stored in the memory.
Query
This is an abstract class. It has multiple implementations, such as TermQuery, BooleanQuery, and PrefixQuery. This class aims to encapsulate the Query string entered by the user into a Query that Lucene can recognize.
IndexSearcher
IndexSearcher is used to search for a created index. It can only open one index in read-only mode, so multiple IndexSearcher instances can operate on one index.
Hits
Hits is used to save search results.
After understanding these classes, I wrote a simple demo, including index construction and index search. If you want to perform complicated search in the future, you can scale it according to this demo. Isn't it easy.
BuildIndexer (Analyzer analyzer, Directory directory, List <Item> IndexWriter iwriter = IndexWriter (directory, Document doc [] = (I = 0; I <items. size (); I ++ doc [I] = Item item = java. lang. reflect. field [] fields = String fieldName = String getMethodName = "get" + Object obj = doc [I]. add (}}list <Item> DirectoryReader ireader = List <Item> result = ArrayList <Item> ireader = IndexSearcher isearcher = java. lang. reflect. field [] fields = Item. length = String [] multiFields = (I = 0; I <length; I ++ multiFields [I] = MultiFieldQueryParser parser = Query query = ScoreDoc [] hits = isearcher. search (query, 10 (I = 0; I
Item. java
.id = .title = .content = .id = .title = .content = StringBuilder sb = sb.append("[id=").append(id).append(",title=" .append(",content=").append(content).append("]" }