安装luncene ,为全文搜寻功能,
Lucene的最主要工作是替文件的每一个字作索引,
索引让搜寻的效率比传统的逐字比较大大提高,
Lucen提供一组解读,过滤,分析文件,编排和使用索引的API,
它的强大之处除了高效和简单外,是最重要的是使使用者可以随时应自己需要自订其功能。
转载需注明:https://blog.csdn.net/juan1997/article/details/90636914
1、首先往pom.xml引入包
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>7.6.0</version>
</dependency>
2、为某一个需要搜索的模块,建立搜素方法。(该类名叫IndexDao)
public String indexPath ="";//索引目录路径
// 中文分词器
public Analyzer analyzer = new SmartChineseAnalyzer();
//创建索引。
public void create(Document document) throws IOException {
// 设置索引的分词器
IndexWriterConfig config = new IndexWriterConfig(analyzer);
// 索引目录
Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
System.out.println("项目索引被添加");
}
//删除索引
public void delete(Term term) throws IOException {
// 设置索引的分词器
IndexWriterConfig config = new IndexWriterConfig(analyzer);
// 索引目录
Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.deleteDocuments(term);
indexWriter.commit();
indexWriter.close();
System.out.println("删除索引");
}
//更新索引
public void update(Term term, Document document) throws IOException {
// 设置索引的分词器
IndexWriterConfig config = new IndexWriterConfig(analyzer);
// 索引目录
Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.updateDocument(term, document);
indexWriter.commit();
indexWriter.close();
System.out.println("更新索引");
}
//查询索引
public List<OrderEntity> search(Query query) throws Exception {
// 设置索引的分词器
//IndexWriterConfig config = new IndexWriterConfig();
// 索引目录
Directory directory = FSDirectory.open(Paths.get(new File(indexPath).getPath()));
// IndexSearcher是用来搜索用的
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 得到满足条件的前1000行
TopDocs topDocs = indexSearcher.search(query, 10000);
// 文档集合
List<OrderEntity> recordList = new ArrayList<OrderEntity>();
// ============高亮和截取某个字段的文本摘要设置=============
// 设置环绕的首尾字符串
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>");
// 语法高亮显示设置
Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
// 100是是表示摘要的字数
highlighter.setTextFragmenter(new SimpleFragmenter(100));
// ===================================================
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docSn = scoreDoc.doc; // 文档内部编号
Document doc = indexSearcher.doc(docSn); // 根据编号取出相应的文档
String scheduleSch = doc.get("orderCode");
TokenStream tream = analyzer.tokenStream("orderCode", new StringReader(scheduleSch));
String searchResultScheduleSch = highlighter.getBestFragment(tream, scheduleSch);
// 如果内容 不包含关键字,会返回null,就截取前100个字符
if (searchResultScheduleSch == null) {
int minLen = scheduleSch.length() >= 100 ? 100 : scheduleSch.length();
searchResultScheduleSch = scheduleSch.substring(0, minLen);
}
//在这里,你可以自定义返回list列表,可以获得的数据,前提是你往里面假如索引。如下例子:就是只做参考。
OrderEntity orderEntity = new OrderEntity();
orderEntity.setOrderId(Long.valueOf(doc.get("scheduleId")));
orderEntity.setOrderCode(searchResultScheduleSch);
recordList.add(orderEntity);
}
indexReader.close();
return recordList;
}
三、引入索引的方法。
// 创建索引
Document document = new Document();
document.add(new TextField("orderId", String.valueOf(orderEntity.getOrderId()), Store.YES));//在这里,你可以自行定义。就是这种方法。
document.add(new TextField("orderCode", orderEntity.getOrderCode(), Store.YES));
indexDao.create(document);
// 更新索引
Document document = new Document();
Term term = new Term("orderId", String.valueOf(orderEntity.getOrderId()));
document.add(new TextField("orderId", String.valueOf(orderEntity.getOrderId()), Store.YES));
document.add(new TextField("orderCode", orderEntity.getOrderCode(), Store.YES));
indexDao.update(term, document);
// 删除索引
Term term = new Term("orderId", String.valueOf(orderId));
indexDao.delete(term);
Analyzer analyzer = new SmartChineseAnalyzer();// 中文分词器
QueryParser queryParser = new MultiFieldQueryParser(new String[] { "orderId", "orderCode" }, analyzer);
Query query = queryParser.parse(queryStr);
List<OrderEntity> list = indexDao.search(query);
//这就可以用了。就像ordereId,orderCode 修改为自己需要的属性名就行了。