我有3个产品名称,它们分别是:
- Bounty Select-A-Size白色纸巾12兆卷
- Bounty Select-A-Size纸巾(12卷)
- Bounty Select-A-Size白色纸巾12兆卷
如您所见,第一个和第三个术语除了“白色”一词的位置不同外,其他都相同。第二个术语缺少“白色”和“兆”这两个词
现在,当我运行以下代码时:
public static void main(String[] args) throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
// 1. create the index
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Bounty Select-A-Size White Paper Towels 12 Mega Rolls");
addDoc(w, "Bounty Select-A-Size Paper Towels (12 rolls)");
addDoc(w, "Bounty Select-A-Size Paper Towels White 12 Mega Rolls");
w.close();
// 2. query
String querystr = "Bounty Select-A-Size White Paper Towels 12 Mega Rolls";
Query q = new QueryParser("title", analyzer).parse(querystr);
// 3. search
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
ScoreDoc[] hits = searcher.search(q, 4).scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title") + "\t score " + hits[i].score);
}
reader.close();
}
private static void addDoc(IndexWriter w, String title) throws IOException {
Document doc = new Document();
doc.add(new TextField("title", title, Field.Store.YES));
w.addDocument(doc);
}
结果如下:
1. Bounty Select-A-Size White Paper Towels 12 Mega Rolls score 0.7363191
2. Bounty Select-A-Size Paper Towels White 12 Mega Rolls score 0.7363191
3. Bounty Select-A-Size Paper Towels (12 rolls) score 0.42395753
到目前为止,一切都很好,前两个术语具有相同的构成,因此它们得分相同。
然而,当我将要搜索的术语数量扩展到更多(使用相同的代码,但不是静态输入3个,而是从文件中获取了大约5000个),得分发生了变化。
1. Bounty Select-A-Size White Paper Towels 12 Mega Rolls 4.1677103
2. Bounty Select-A-Size Paper Towels (12 rolls) 4.1677103
3. Bounty Select-A-Size Paper Towels White 12 Mega Rolls 2.874553
我的问题是:
当数据集发生变化时,得分是否可能以这种方式改变?
如果可以,那么如何实现?
如果不行,那么我知道我的代码有错误...