lucene实现过程
全文检索: 用于了解底层的实现过程
mian方法包裹{
//使用lucene包装好的File方法FSDirectory方法,在磁盘中创建一个存储的地址
FSDirectory d=FSDirectory.open(new File(“d:adc/”).toPath);
//构建中文分词器,使用lucene的方法IndexWriterConfig构造分词器
IndexWriterConfig cfg=new IndexWriterConfig (new SmartChineseAnalyzer)
//创建索引的输出工具 参数是文件存储地址,用于持久保持,cfg是中文分词器
IndexWriter w =new IndexWriter (d,cfg)
//创建文档,将数据存入文档中
Document doc =new Document ();
doc.add(new LongPoint(“id”),long.parseLong(存入的值));
doc.add(new TextField(“变量名”,存入的值,是否进行摘要))
//持久化保存
w.addDocument(doc);
//销毁io流
w.close
}
代码
package test;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
public class Test1 {
String[] a = {
"3, 华为 - 华为电脑, 爆款",
"4, 华为手机, 旗舰",
"5, 联想 - Thinkpad, 商务本",
"6, 联想手机, 自拍神器"
};
@Test
public void test1() throws Exception {
//存储索引文件的路径
File path = new File("d:/abc/");
//FSDirectory lucene 自己包装的类
FSDirectory d = FSDirectory.open(path.toPath());
//lucene提供的中文分词器
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
//通过配置对象来指定分词器
IndexWriterConfig cfg = new IndexWriterConfig(analyzer);
//索引输出工具
IndexWriter writer = new IndexWriter(d, cfg);
for (int i = 0; i < a.length; i++) {
String[] strs = a[i].split(",");
//创建文档,文档中包含的是要索引的字段
Document doc = new Document();
doc.add(new LongPoint("id", Long.parseLong(strs[0])));
//进行摘要存储
doc.add(new StoredField("id", Long.parseLong(strs[0])));
//TextField 方法第三个参数用于声明是否需要进行摘要存储
doc.add(new TextField("title", strs[1], Store.YES));
doc.add(new TextField("sellPoint", strs[2], Store.YES));
//将文档写入磁盘索引文件
writer.addDocument(doc);
}
writer.close();
}
读取索引信息
代码
@Test
public void test2() throws IOException {
//文件夹
FSDirectory d = FSDirectory.open(new File("d:/abc/").toPath());
//读取索引的工具//查询工具
IndexSearcher searcher = new IndexSearcher( DirectoryReader.open(d));
//分装查询关键词
TermQuery q = new TermQuery(new Term("title", "华为"));
//用查询工具查询
TopDocs docs = searcher.search(q, 20);
//遍历查询到的结果文档并显示
for (ScoreDoc scoreDoc : docs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(scoreDoc.score);
System.out.println(doc.get("title"));
System.out.println(doc.get("sellPoint"));
System.out.println("--------------");
}
}
}