Java lucene索引搜索缺少命中率
我索引了一个大的数据库概览(只是文本字段),用户必须能够在其中进行搜索(在下面的indexFields方法中)。以前的搜索是在数据库中使用ILIKE查询完成的,但速度很慢,所以现在搜索是在索引上完成的。Hovewer,当我比较db查询的搜索结果和索引搜索的结果时,索引搜索的结果总是少得多。 我不确定我是否在索引或搜索过程中出错。在我看来,这一切似乎都有道理。有什么想法吗 这是代码。感谢所有建议Java lucene索引搜索缺少命中率,java,indexing,full-text-search,lucene,Java,Indexing,Full Text Search,Lucene,我索引了一个大的数据库概览(只是文本字段),用户必须能够在其中进行搜索(在下面的indexFields方法中)。以前的搜索是在数据库中使用ILIKE查询完成的,但速度很慢,所以现在搜索是在索引上完成的。Hovewer,当我比较db查询的搜索结果和索引搜索的结果时,索引搜索的结果总是少得多。 我不确定我是否在索引或搜索过程中出错。在我看来,这一切似乎都有道理。有什么想法吗 这是代码。感谢所有建议 // INDEXING StandardAnalyzer analyzer = new Standa
// INDEXING
StandardAnalyzer analyzer = new StandardAnalyzer(
Version.LUCENE_CURRENT, stopSet); // stop set is empty
IndexWriter writer = new IndexWriter(INDEX_DIR, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
indexFields(writer);
writer.optimize();
writer.commit();
writer.close();
analyzer.close();
private void索引字段(IndexWriter){
DetachedCriteria=DetachedCriteria
.forClass(活动类);
整数计数=0;
int max=50000;
布尔existMoreToIndex=true;
列表结果=新建ArrayList();
while(existMoreToIndex){
试一试{
结果=activitService.listPaged(计数,最大值);
if(result.size()
//搜索
索引中的公共列表搜索活动(字符串搜索条件){
Set stopSet=new HashSet();//为空,因为我们不想删除停止字
Version Version=Version.LUCENE\u当前版本;
字符串[]字段={
“字段1”、“字段2”};
试一试{
File tempFile=新文件(“C://testindex”);
目录索引_DIR=新的SimpleFSDirectory(tempFile);
Searcher Searcher=newindexsearcher(INDEX_DIR,true);
QueryParser parser=新的多字段QueryParser(版本,字段,新的StandardAnalyzer(
版本,停止集);
Query=parser.parse(searchCriteria);
TopDocs TopDocs=searcher.search(查询,500);
ScoreDoc[]点击次数=topDocs.scoreDocs;
//在这里我总是得到较小的点击长度
searcher.close();
}捕获(例外e){
e、 printStackTrace();
}
}
分析仪很可能正在做一些您不期望的事情
使用打开索引,您可以看到(分析的)索引文档的外观,以及解析的查询-应该让您看到哪里出了问题
另外,你能举一个
searchCriteria
?和相应的SQL查询的例子吗?没有这个例子,很难知道索引是否正确。你可能也不需要使用MultiFieldQueryParser
,这是非常低效的。我正在使用MultiFieldQueryParser(我猜你错过了:))!搜索条件只是简单的字符串,如“hotel”“hot”我正在使用MultiFieldQueryParser,因为我不想搜索每个字段separately@Julia,这就是我的观点,如果你使用MultiFieldQueryParser,你是在单独搜索字段,它只是提供一些语法上的糖分。如果你想让你的关键字在任何字段中匹配,最好在到单个字段。如果尚未打印topDocs.totalHits,则打印该数字。该数字将为您提供与查询匹配的文档总数。@Shashikant Kore:我已经这样做了,并且看到该数字是正确的,这就是我发布该问题的原因。
private void indexFields(IndexWriter writer) {
DetachedCriteria criteria = DetachedCriteria
.forClass(Activit.class);
int count = 0;
int max = 50000;
boolean existMoreToIndex = true;
List<Activit> result = new ArrayList<Activit>();
while (existMoreToIndex) {
try {
result = activitService.listPaged(count, max);
if (result.size() < max)
existMoreToIndex = false;
if (result.size() == 0)
return;
for (Activit ao : result) {
Document doc = new Document();
doc.add(new Field("id", String.valueOf(ao.getId()),
Field.Store.YES, Field.Index.ANALYZED));
if(ao.getActivitOwner()!=null)
doc.add(new Field("field1", ao.getActivityOwner(),Field.Store.YES, Field.Index.ANALYZED));
if(ao.getActivitResponsible() != null)
doc.add(new Field("field2", ao.getActivityResponsible(), Field.Store.YES,Field.Index.ANALYZED));
try {
writer.addDocument(doc);
} catch (CorruptIndexException e) {
e.printStackTrace();
}
count += max;
//SEARCH
public List<Activit> searchActivitiesInIndex(String searchCriteria) {
Set<String> stopSet = new HashSet<String>(); // empty because we do not want to remove stop words
Version version = Version.LUCENE_CURRENT;
String[] fields = {
"field1", "field2"};
try {
File tempFile = new File("C://testindex");
Directory INDEX_DIR = new SimpleFSDirectory(tempFile);
Searcher searcher = new IndexSearcher(INDEX_DIR, true);
QueryParser parser = new MultiFieldQueryParser(version, fields, new StandardAnalyzer(
version, stopSet));
Query query = parser.parse(searchCriteria);
TopDocs topDocs = searcher.search(query, 500);
ScoreDoc[] hits = topDocs.scoreDocs;
//here i always get smaller hits lenght
searcher.close();
} catch (Exception e) {
e.printStackTrace();
}
}