【发布时间】:2014-11-17 16:54:39
【问题描述】:
假设我对特定照片上的 cmets 进行索引,如下所示。
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter indexWriter = new IndexWriter(indexDir, config);
Document doc1 = new Document()
doc1.addField(new Field("photoId", "12345.jpg", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
doc1.addField(new Field("body", "photo of cats skating", Field.Store.YES, Field.Index.ANALYZED));
Document doc2 = new Document()
doc2.addField(new Field("photoId", "12345.jpg", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
doc2.addField(new Field("body", "skating cats are fun to look at", Field.Store.YES, Field.Index.ANALYZED));
Document doc3 = new Document()
doc3.addField(new Field("photoId", "6789.jpg", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
doc3.addField(new Field("name", "two dogs skating like pros", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.addDocuments(Arrays.asList(new Document[]{doc1, doc2, doc3}));
我想在cmets中查询,根据body内容返回照片。
如果我查询skating dogs and cats,所有三个文档都会返回。
我想要的是返回doc3 和doc1 or doc2。即根据字段photoId 的值返回唯一文档。一旦 12345.jpg 匹配了一张,就忽略其余的,因为我们只想要这张照片。我该如何做到这一点?
我的搜索基本是这样的
String[] fields = {"body", "any_other_relevant_field"};
Query query = new MultiFieldQueryParser(Version.LUCENE_36, fields, analyzer).parse("skating dogs and cats");
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
IndexSearcher searcher = searcherManager.acquire();
searcher.search(query, null, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// The rest seems to be the normal yada yada
【问题讨论】:
标签: lucene