1.maven依賴
<dependencies>
<!-- lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>${lucene-version}</version>
</dependency>
<!-- lucene-highlighter -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>${lucene-version}</version>
</dependency>
<!-- lucene-analyzers-smartcn -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>${lucene-version}</version>
</dependency>
</dependencies>
這些依賴中已經包含對核心包等必要jar的依賴
2.demo
public static void main(String[] args) {
// Analyzer類負責分析一個文件,並從將被索引的文本獲取令牌/字。不加分析完成後,IndexWriter不能創建索引。
Analyzer analyzer = new StandardAnalyzer();
// 此類充當創造/在索引過程中更新指標的核心組成部分
IndexWriterConfig config = new IndexWriterConfig(analyzer);
// 模式:創建或追加
config.setOpenMode(OpenMode.CREATE_OR_APPEND);
// Directory類表示索引的存儲位置,並通常是文件的列表。這些文件被稱爲索引文件。索引文件通常創建一次,然後用於讀操作或可以被刪除。
Directory directory = null;
IndexWriter writer = null;
try {
// 創建
directory = FSDirectory.open(Paths.get("E:\\document\\【進階之路】\\技術\\lucene\\index"));
writer = new IndexWriter(directory, config);
} catch (IOException e) {
e.printStackTrace();
}
// 文檔表示一個虛擬文檔與字段,其中字段是可包含在物理文檔的內容,它的元數據等的對象。Analyzer只能理解文檔。
Document doc1 = new Document();
/*
* YES:
* Store the original field value in the index.
* This is useful for short texts like a document's title which should be displayed with the results.
* The value is stored in its original form, i.e. no analyzer is used before it is stored
* NO:
* Do not store the field value in the index.
*/
doc1.add(new StringField("id", "a", Store.YES));
doc1.add(new TextField("content", "lucene學習案例", Store.YES));
// 數值類型的添加需要對應的Point類加上StoredField
doc1.add(new IntPoint("num", 1));
doc1.add(new StoredField("num", 1));
try {
writer.addDocument(doc1);
} catch (IOException e) {
e.printStackTrace();
}
Document doc2 = new Document();
doc2.add(new StringField("id", "c", Store.YES));
doc2.add(new TextField("content", "Lucene案例開發", Store.YES));
doc2.add(new IntPoint("num", 2));
doc2.add(new StoredField("num", 2));
try {
writer.addDocument(doc2);
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.commit();
// 關閉資源
writer.close();
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3.導包
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
4.查看,結果查詢,使用luke工具
附luke地址: