如题,lucene+IK分词实现检索功能,目的是比数据库查询快的多,但是问题来了,如:我输入“小米”查询企业信息列表,返回了**小米**XXXXX公司、**小米**XXXXX服务公司。。。。。,我输入“小米科技”,返回了**小米**XXXXX公司、**小米**XXXXX服务公司、XXX百度**科技**XXXX公司,输入越精确分词返回结果集越多,想想觉得不太合理,希望将分词后返回包含所有分词后小词条的结果,但是没找到相应的办法,不知道有没坛友遇到类似问题有解决经验的,求指引!
PropKit.use(ConstantConfig.CONFIG_PROPERTIES);
Directory dir = FSDirectory.open(Paths.get(PropKit.get(ConstantConfig.LUCENCE_PATH)));// 打开索引目录
IndexSearcher isearcher = new IndexSearcher(DirectoryReader.open(dir));// search对象
IKAnalyzer analyzer = new IKAnalyzer(false);// IK分词器
// KeywordAnalyzer analyzer = new KeywordAnalyzer();
// StandardAnalyzer analyzer = new StandardAnalyzer();
List fieldNameList = new ArrayList();
List fieldValueList = new ArrayList();
fieldNameList.add(ConstantField.STATUS_CODE);
fieldValueList.add(StatusCode.STATUS_NORMAL.getValue());
fieldNameList.add("spName");
fieldValueList.add("小米科技");
// 分页查询
TopFieldCollector c = TopFieldCollector.create(new Sort(SortField.FIELD_SCORE), 50, false, false, false);
if (fieldNameList.size() < 1) {
isearcher.search(new MatchAllDocsQuery(), c);
} else {
String[] queries = new String[fieldNameList.size()];
String[] fields = new String[fieldNameList.size()];
BooleanClause.Occur[] clauses = new BooleanClause.Occur[fieldNameList.size()];
for (int i = 0; i < fieldNameList.size(); i++) {
queries[i] = fieldValueList.get(i);
fields[i] = fieldNameList.get(i);
clauses[i] = BooleanClause.Occur.MUST;
}
//多字段查询
QueryParser parser=new MultiFieldQueryParser(new String[]{""}, analyzer);
Query query = MultiFieldQueryParser.parse(queries, fields, clauses, analyzer);
isearcher.search(query, c);
}
System.out.println(c.getTotalHits());
ScoreDoc[] hits = c.topDocs(0, 50).scoreDocs;
List> dataList = new ArrayList>();
Map entity;
for (int i = 0; i < hits.length; i++) {
Document doc = isearcher.doc(hits[i].doc);
entity = new HashMap();
entity.put("spName", doc.get("spName"));
dataList.add(entity);
}
System.out.println(JSON.toJSON(dataList).toString());