OUr团队只是将lucene从2.3升级到3.0,我们对文件的setboost和getboost感到困惑。我们想要的只是在将每个文档添加到索引时为每个文档设置一个提升,然后在搜索它时,响应中的文档应根据我设置的提升而具有不同的顺序。但似乎订单根本没有改变,即使搜索响应中每个文档的提升仍然是1.0。有人会给我一些打击吗?以下是我们的代码:
String[] a = new String[] { "schindler", "spielberg", "shawshank", "solace", "sorcerer", "stone", "soap",
"salesman", "save" };
List<String> strings = Arrays.asList(a);
AutoCompleteIndex index = new Index();
IndexWriter writer = new IndexWriter(index.getDirectory(), AnalyzerFactory.createAnalyzer("en_US"), true,
MaxFieldLength.LIMITED);
float i = 1f;
for (String string : strings) {
Document doc = new Document();
Field f = new Field(AutoCompleteIndexFactory.QUERYTEXTFIELD, string, Field.Store.YES,
Field.Index.NOT_ANALYZED);
doc.setBoost(i);
doc.add(f);
writer.addDocument(doc);
i += 2f;
}
writer.close();
IndexReader reader2 = IndexReader.open(index.getDirectory());
for (int j = 0; j < reader2.maxDoc(); j++) {
if (reader2.isDeleted(j)) {
continue;
}
Document doc = reader2.document(j);
Field f = doc.getField(AutoCompleteIndexFactory.QUERYTEXTFIELD);
System.out.println(f.stringValue() + ":" + f.getBoost() + ", docBoost:" + doc.getBoost());
doc.setBoost(j);
}
感谢您的回答。我已根据您的建议更新了代码,但似乎仍然无效。似乎结果的顺序没有被提升改变,并且每个搜索结果的得分是相同的(1.0)。请检查下面的代码:
public void testScore()抛出异常{ String [] a = new String [] {“schindler”,“spielberg”,“shawshank”,“solace”,“巫师”,“石头”,“肥皂”, “推销员”,“保存”}; List strings = Arrays.asList(a); AutoCompleteIndex index = new Index(); IndexWriter writer = new IndexWriter(index.getDirectory(),AnalyzerFactory.createAnalyzer(“en_US”),true, MaxFieldLength.LIMITED);
float i = 1f;
for (String string : strings) {
Document doc = new Document();
doc.add(new Field(AutoCompleteIndexFactory.QUERYTEXTFIELD, string, Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.setBoost(i);
// System.out.println(doc.getBoost());
i += 2f;
writer.addDocument(doc);
}
writer.close();
BooleanQuery
.setMaxClauseCount(BooleanQuery.getMaxClauseCount() < getMaxQueryTextEntry() ? getMaxQueryTextEntry()
: BooleanQuery.getMaxClauseCount());
Term searchTerm = new Term(AutoCompleteIndexFactory.QUERYTEXTFIELD, "s");
PrefixQuery query = new PrefixQuery(searchTerm);
IndexSearcher searcher = new IndexSearcher(index.getDirectory());
TopDocs docs = searcher.search(query, 10);
ScoreDoc[] hits = docs.scoreDocs;
for (ScoreDoc hit2 : hits) {
String hit = searcher.doc(hit2.doc).get(AutoCompleteIndexFactory.QUERYTEXTFIELD);
System.out.println(hit + " score:" + hit2.score);
System.out.println(searcher.explain(query, hit2.doc));
}
}
输出是:
2010年6月17日下午4:12:18 信息:
schindler得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
斯皮尔伯格得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNormshawshank得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
安慰得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
巫师得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
石头得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm肥皂得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
推销员得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
保存得分:1.0 1.0 =(MATCH)ConstantScoreQuery(querytexts:s *),产品: 1.0 =提升 1.0 = queryNorm
答案 0 :(得分:0)
文档提升意味着在搜索时生效,而不是在您按顺序检索索引中的文档时生效,就像在代码示例中一样。 尝试进行以下实验:
文档提升的含义是:当所有其他评分因子相等时,具有更高提升的文档获得更高的分数。请参阅Lucene的scoring documentation和explain() function。