J'ai un problème avec la fonction de notation de Lucene que je n'arrive pas à comprendre. Jusqu'à présent, j'ai pu écrire ce code pour le reproduire.Problème avec la notation Lucene
package lucenebug;
import java.util.Arrays;
import java.util.List;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
public class Test {
private static final String TMP_LUCENEBUG_INDEX = "/tmp/lucenebug_index";
public static void main(String[] args) throws Throwable {
SimpleAnalyzer analyzer = new SimpleAnalyzer();
IndexWriter w = new IndexWriter(TMP_LUCENEBUG_INDEX, analyzer, true);
List<String> names = Arrays
.asList(new String[] { "the rolling stones",
"rolling stones (karaoke)",
"the rolling stones tribute",
"rolling stones tribute band",
"karaoke - the rolling stones" });
try {
for (String name : names) {
System.out.println("#name: " + name);
Document doc = new Document();
doc.add(new Field("name", name, Field.Store.YES,
Field.Index.TOKENIZED));
w.addDocument(doc);
}
System.out.println("finished adding docs, total size: "
+ w.docCount());
} finally {
w.close();
}
IndexSearcher s = new IndexSearcher(TMP_LUCENEBUG_INDEX);
QueryParser p = new QueryParser("name", analyzer);
Query q = p.parse("name:(rolling stones)");
System.out.println("--------\nquery: " + q);
TopDocs topdocs = s.search(q, null, 10);
for (ScoreDoc sd : topdocs.scoreDocs) {
System.out.println("" + sd.score + "\t"
+ s.doc(sd.doc).getField("name").stringValue());
}
}
}
La sortie que je reçois de l'exécuter est:
finished adding docs, total size: 5
--------
query: name:rolling name:stones
0.578186 the rolling stones
0.578186 rolling stones (karaoke)
0.578186 the rolling stones tribute
0.578186 rolling stones tribute band
0.578186 karaoke - the rolling stones
Je ne peux pas comprendre pourquoi the rolling stones
a la même pertinence que the rolling stones tribute
. Selon documentation de lucene, plus un champ a de jetons, plus le facteur de normalisation devrait être petit, et donc the rolling stones tribute
devrait avoir un score inférieur à the rolling stones
.
Des idées?
Quelle version de Lucene utilisez-vous (vous liez le document API de 2.4)? Dans Lucene 2.9, les scores ne sont pas retournés par défaut, vous devez fournir un TopFieldCollector: http://www.gossamer-threads.com/lists/lucene/java-user/86309 – digitalarbeiter
@gossamer: J'ai exécuté le même code contre Lucene 2.3, 2.4 et 2.9. Mêmes résultats –