2.1 全文检索和以前高级查询的比较
1.高级查询
缺点:1.like让数据库索引失效
2.每次查询都是查询数据库 ,如果访问的人比较多,压力也是比较大
2.全文检索框架:Apache - Lucene
优点:
1.可以相关度排序
2.可以对摘要进行截取
3.关键字高亮显示
2.2 Lucene测试
1.引入jar包
lucene-analyzers-common-5.5.0.jar
lucene-core-5.5.0.jar
lucene-queryparser-5.5.0.jar
2.创建索引,搜索索引
package cn.itsource.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.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.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";
//用来存放索引文件
private String path ="D:\\lucene";
//创建索引
//步骤:
//1、把文本内容转换为Document对象
//2、准备IndexWriter(索引写入器)
//3、通过IndexWriter,把Document添加到缓冲区并提交