0
我有一個新聞文章的索引,我保存標題,鏈接,新聞的描述..有時它可能從相同的鏈接發佈相同的新聞與不同的新聞來源不同的標題。它不希望完全相同的描述文章被添加兩次..如何查找文檔是否已經存在?如何檢查文檔是否存在於lucene索引中?
我有一個新聞文章的索引,我保存標題,鏈接,新聞的描述..有時它可能從相同的鏈接發佈相同的新聞與不同的新聞來源不同的標題。它不希望完全相同的描述文章被添加兩次..如何查找文檔是否已經存在?如何檢查文檔是否存在於lucene索引中?
我假設你正在使用Java。 假設您的鏈接以StringField的形式保存在索引中(因此您使用的任何分析器都不會將鏈接拆分爲多個術語),您可以使用TermQuery。
TopDocs results = searcher.search(new TermQuery(new Term("link", "http://example.com")), 1);
if (results.totalHits == 0){
Document doc = new Document();
// create your document here with your fields
// link field should be stored as a StringField
doc.add(new StringField("link", "http://example.com", Stored.YES));
writer.addDocument(doc);
}
請注意,StringFields存儲完全相同,因此您可能希望在搜索/索引時轉換爲小寫字母。
如果你想確保不超過1場已經存在,那麼你就可以運行它使用Occur.SHOULD條件BooleanQuery:
BooleanQuery matchingQuery = new BooleanQuery();
matchingQuery.add(new TermQuery(new Term("link", "http://example.com")), Occur.SHOULD);
matchingQuery.add(new TermQuery(new Term("description", "the unique description of the article")), Occur.SHOULD);
TopDocs results = searcher.search(matchingQuery, 1);
if (results.totalHits == 0){
Document doc = new Document();
// create your document here with your fields
// link field should be stored as a StringField
doc.add(new StringField("link", "http://example.com", Stored.YES));
doc.add(new StringField("description", "the unique description of the article", Stored.YES));
// note if you need the description to be tokenized, you need to add another TextField to the document with a different field name
doc.add(new TextField("descriptionText", "the unique description of the article", Stored.NO));
writer.addDocument(doc);
}
我仍然有問題..似乎相同的文章在不同的報紙上發佈不同的鏈接..所以除了鏈接,我還需要另一個領域作爲主要領域..爲了避免多個搜索結果在不同的報紙網站上的同一篇文章..任何建議? – Sneha
你怎麼知道每篇文章都是一樣的?說明?如果是這樣,那麼你可以使用描述作爲唯一標識字段。 – user1071777
是通過描述..但我想避免2種重複在我的索引中。文章具有相同的描述和相同的鏈接..因爲他們最終指向相同的數據..所以如何有2個唯一的標識符? – Sneha