验证中...
Snippet 1 Snippet 2
组合搜索条件
Raw Copy
/**
* 组合查询(跨索引,跨字段(模糊))逻辑关系 f1 or f2 or f3
* @return
* 创建时间:2018年5月17日
* @throws IOException
* @throws ParseException
*
*/
public static List<Map<String,Object>> searchList(Analyzer an,String q,Set<String> f,int c,String ...path) throws IOException, ParseException{
IndexReader[] irs = new IndexReader[path.length];
try {
for(int i=0;i<path.length;i++){//与多个位置的所有建立连接
Directory d = FSDirectory.open(Paths.get(path[i]));
IndexReader ir = DirectoryReader.open(d);
irs[i] = ir;
}
MultiReader mr = new MultiReader(irs);//合并多个索引
Builder b = new Builder();
for(String s:f){//添加多个搜索字段
Query query = new QueryParser(s, an).parse(q);
b.add(query,Occur.SHOULD);
//逻辑关系配置:Occur.MUST (与) 取查询并集;Occur.SHOULD (或) 取查询交集;Occur.MUST_NOT (不包含) 排除
}
return searchList(mr, b.build(), c);
} finally{
for(IndexReader i:irs){
if(i!=null){
i.close();
}
}
}
}
搜索并将搜索结果解析成List<Map<String,Object>>
Raw Copy
private static List<Map<String,Object>> searchList(IndexReader ir,Query query,int c) throws IOException{
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
IndexSearcher is = new IndexSearcher(ir);
TopDocs hits = is.search(query, c);
for(ScoreDoc sd:hits.scoreDocs){
if(sd.score>0.1){
Map<String,Object> map = new LinkedHashMap<String, Object>();
Document doc = is.doc(sd.doc);
for(IndexableField field:doc.getFields()){
map.put(field.name(), field.stringValue());
}
list.add(map);
}
}
return list;
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search