导包
<dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.0</version>
</dependency>
</dependencies>
首先来一个Test测试 Hello Word
-
创建一个文件 存放索引 随意 我起的index
package cn.zx.lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import java.nio.file.Paths;
public class TestLucene {
//创建索引的数据 现在写死,以后根据实际应用场景
String doc1 = "hello world";
String doc2 = "hello java world";
String doc3 = "hello lucene world";
//创建索引之后存放的位置 考你新建文件的路径 鼠标新建文件Copy Path
private String path ="D:\\zx1124\\IDEA1\\rpms\\basic-lucene\\index";
//创建索引
@Test
public void testCreateIndex() throws Exception{
//得到写入索引的位置
Directory d = FSDirectory.open(Paths.get(path));
//创建分词器
Analyzer analyzer = new SimpleAnalyzer();
//创建一个索引写入器的配置对象
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
//得到IndexWirter
IndexWriter indexWriter = new IndexWriter(d,conf);
//理解看成数据库一行数据
Document document1 = new Document();
document1.add(new TextField("title",doc1, Field.Store.YES));
document1.add(new TextField("content",doc1,Field.Store.YES));
Document document2 = new Document();
document2.add(new TextField("title",doc2, Field.Store.YES));
document2.add(new TextField("content",doc2,Field.Store.YES));
Document document3 = new Document();
document3.add(new TextField("title",doc3, Field.Store.YES));
document3.add(new TextField("content",doc3,Field.Store.YES));
//保存到index去
indexWriter.addDocument(document1);
indexWriter.addDocument(document2);
indexWriter.addDocument(document3);
indexWriter.commit();
indexWriter.close();
}
}
-
运行之后
-看不懂没有关系,可以用工具打开查看
- 我们这里演示用另一种方法,用代码查看
//通过代码操作索引
@Test
public void testSearchIndex() throws Exception{
//要检索的内容
String keyword = "luence";
//要检索的字段
String f = "content";
//创建一个普通的分词器
Analyzer analyzer =new SimpleAnalyzer();
//得到查询的解析器
QueryParser parser = new QueryParser(f,analyzer);
Query query = parser.parse("content:"+keyword);
//得到索引的目录
Directory d = FSDirectory.open(Paths.get(path));
//打开目录,放到IndexReader里面
IndexReader r = DirectoryReader.open(d);
//得到IndexSearch
IndexSearcher indexSearcher = new IndexSearcher(r);
//放入这个里面搜索 query 搜索前十条
TopDocs topDocs = indexSearcher.search(query, 10);
//得到命中数
System.out.println("总的命中数:"+topDocs.totalHits);
//得到所有命中的数据
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//scoreDoc.iter 循环快捷键
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
System.out.println("=============================");
System.out.println("title:"+document.get("title"));
System.out.println("content:"+document.get("content"));
}
}
-
运行结果
-
把字段换一个 再看一下 比如:String keyword = "hello";
高亮显示
加入依赖
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.5.0</version>
</dependency>
Test
//高亮显示
//通过代码操作索引
@Test
public void testHighLighter() throws Exception{
//要检索的内容
String keyword = "hello";
//要检索的字段
String f = "content";
//创建一个普通的分词器
Analyzer analyzer =new SimpleAnalyzer();
//得到查询的解析器
QueryParser parser = new QueryParser(f,analyzer);
Query query = parser.parse("content:"+keyword);
//得到索引的目录
Directory d = FSDirectory.open(Paths.get(path));
//打开目录,放到IndexReader里面
IndexReader r = DirectoryReader.open(d);
//得到IndexSearch
IndexSearcher indexSearcher = new IndexSearcher(r);
//放入这个里面搜索 query 搜索前十条
TopDocs topDocs = indexSearcher.search(query, 10);
/**自定义标注高亮文本标签*/
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color=\"red\">","</span>");
/**创建QueryScorer*/
QueryScorer scorer=new QueryScorer(query);
/**创建Fragmenter*/
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
Highlighter highlight=new Highlighter(formatter,scorer);
/**设置到对象里面*/
highlight.setTextFragmenter(fragmenter);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {//获取查找的文档的属性数据
int docID=scoreDoc.doc;
Document document =indexSearcher.doc(docID);
//得到字段
String value=document.get("content");
if (value != null) {
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(value));
String value1 = highlight.getBestFragment(tokenStream, value);
System.out.println("高亮显示的结果"+value1);
}
}
}
-
测试结果
网友评论