整体架构
常用field
常用query
1、引入maven支持
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.4.0</version>
</dependency>
2、添加索引库、文档库
/创建分词器
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter writer=null;
Path path=new File("C:\\Users\\star\\Desktop\\leceneData").toPath();
try {
Directory directory = FSDirectory.open(path);
//创建IndexWriter
writer=new IndexWriter(directory,indexWriterConfig);
} catch (IOException e) {
e.printStackTrace();
}
List<GoodsEntity> goodsEntityList=goodsBLL.selectList(null);
for (GoodsEntity goodsEntity : goodsEntityList) {
Document document=new Document();
Field id=new NumericDocValuesField("id",goodsEntity.getId().longValue());
Field name=new TextField("name",goodsEntity.getName(), Field.Store.YES);
Field price=new NumericDocValuesField("price",goodsEntity.getPrice().longValue());
document.add(id);
document.add(name);
document.add(price);
try {
writer.addDocument(document);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
3、根据Query查询索引库得到文档库
/**
* 通过query查询
* @param query query子类对象
* @param length 每页数量
* @return
*/
public static Map<String,Object> queryLucene(Query query,int length){
Map<String,Object> rtnMap=new HashMap<>();
List<Document> documentList=new ArrayList<>();
try {
//lucene存储的位置
Path path=new File("C:\\Users\\star\\Desktop\\leceneData").toPath();
//存储在文件中(可选内存)
Directory directory = FSDirectory.open(path);
//打开查询流
IndexReader reader= DirectoryReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
//获取排序后的数据
TopDocs topDocs = searcher.search(query,length);
//总记录数
long count=topDocs.totalHits;
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
for (ScoreDoc scoreDoc : Arrays.asList(scoreDocs)) {
//获取document
int docId = scoreDoc.doc;
Document document=searcher.doc(docId);
documentList.add(document);
}
rtnMap.put("totalCount",count);
rtnMap.put("data",documentList);
} catch (IOException e) {
e.printStackTrace();
}
return rtnMap;
}
4、创建Query、QueryParser查询索引库得到文档库
public static void query(){
Query query=new TermQuery(new Term("name","联"));
//Query query = FloatPoint.newRangeQuery("price",0F,200F);
//Query query = IntPoint.newRangeQuery("id",1,100);
BooleanQuery.Builder builder=new BooleanQuery.Builder();
builder.add(query, BooleanClause.Occur.MUST);
BooleanQuery booleanQuery=builder.build();
Map<String,Object> dataMap=queryLucene(booleanQuery,1,10);
showQueryResult(dataMap);
}
public static void queryParser(){
//Analyzer analyzer = new StandardAnalyzer();
Analyzer analyzer = new IKAnalyzer();
//设置field的boost值,默认1.0f (即相关度排序)
Map<String,Float> boostMap=new HashMap<>();
boostMap.put("name",200f);
QueryParser queryParser= new MultiFieldQueryParser(new String[]{"name","price"},analyzer,boostMap);
Query query=null;
try {
query=queryParser.parse("联想");
} catch (ParseException e) {
e.printStackTrace();
}
Map<String,Object> dataMap=queryLucene(query,1,10);
showQueryResult(dataMap);
}