一、lucene是什麼?
最受歡迎的java開源全文搜索引擎開發工具包。 提供了完整的查詢引擎和索引引擎, 部分文本分詞引擎。 lucene的目的是爲軟件開發人員提供一個簡單易用的工具包, 以方便在目標系統中實現全文檢索功能, 或者是以此爲基礎建立起完整的全文檢索引擎。
二、lucene代碼示例:
package com.javaxiaobang.es.lucene;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
import com.javaxiaobang.es.constant.EsConstants;
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;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 功 能:lucene
* 作 者:java瀟邦
* 時 間:2020/5/4
*/
public class LuceneMain {
/**
* 源碼地址:https://gitee.com/sunrisexq/es
*/
public static void main(String[] args) {
//1、創建索引
createIndex(EsConstants.INDEX_DATA_DIR);
//2、添加索引文檔
addIndexDoc(EsConstants.INDEX_DATA_DIR, EsConstants.JSON_CONTENT);
//3、查詢內容
query(EsConstants.INDEX_DATA_DIR, "歌手","杰倫");
}
/**
* 創建索引
*
* @param indexDir 索引存放位置
*/
public static void createIndex(String indexDir) {
IndexWriter writer = null;
try {
//獲取目錄
Directory directory = FSDirectory.open(Paths.get(indexDir));
//設置分詞器
Analyzer analyzer = new StandardAnalyzer();
//準備config
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//創建lucene實例
writer = new IndexWriter(directory, indexWriterConfig);
System.out.println("索引創建成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(writer);
}
}
/**
* 添加索引文檔
*
* @param indexDir 索引存放位置
* @param jsonContent json內容
*/
public static void addIndexDoc(String indexDir, String jsonContent) {
IndexWriter writer = null;
try {
//獲取目錄
Directory directory = FSDirectory.open(Paths.get(indexDir));
//設置分詞器
Analyzer analyzer = new StandardAnalyzer();
//準備config
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//創建lucene實例
writer = new IndexWriter(directory, indexWriterConfig);
Document document = jsonToDoc(jsonContent);
writer.addDocument(document);
System.out.println("索引文檔添加成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(writer);
}
}
/**
* json內容轉document文檔
*
* @param jsonContent json內容
* @return
*/
public static Document jsonToDoc(String jsonContent) {
Document document = new Document();
JSONObject jsonObj = JSONObject.parseObject(jsonContent);
Set<Map.Entry<String, Object>> entrySet = jsonObj.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
document.add(new TextField(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString(), Field.Store.YES));
}
return document;
}
/**
* 查詢文檔
* @param indexDir 索引存放位置
* @param queryParam 查詢條件
* @param queryContent 查詢單詞內容
* @return
*/
public static String query(String indexDir, String queryParam, String queryContent) {
StringBuilder result = new StringBuilder();
IndexReader reader = null;
try {
//獲取目錄
Directory directory = FSDirectory.open(Paths.get((indexDir)));
//獲取reader
reader = DirectoryReader.open(directory);
//獲取索引實例
IndexSearcher searcher = new IndexSearcher(reader);
//設置分詞器
Analyzer analyzer = new StandardAnalyzer();
//創建解析器
QueryParser queryParser = new QueryParser(queryParam, analyzer);
Query query = queryParser.parse(queryContent);
TopDocs topDocs = searcher.search(query, 10);
System.out.println("topDocs內容:" + JSON.toJSONString(topDocs));
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
//拿到文檔實例
Document document = searcher.doc(scoreDoc.doc);
//獲取所有文檔字段
List<IndexableField> fieldList = document.getFields();
//處理文檔字段
for (IndexableField field:fieldList){
result.append(field.name());
result.append(":");
result.append(field.stringValue());
result.append(",\r\n");
}
}
System.out.println("查詢結果:"+result);
} catch (Exception e) {
e.printStackTrace();
}finally {
IOUtils.close(reader);
}
return result.toString();
}
}
三、源碼地址:
https://gitee.com/sunrisexq/es