【问题标题】:Lucene 3.6.2 embedded how to limit returned document by field valueLucene 3.6.2 嵌入如何按字段值限制返回的文档
【发布时间】: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,所有三个文档都会返回。 我想要的是返回doc3doc1 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


    【解决方案1】:

    您可以建立索引,使每个文档对应一张照片。在这种情况下,您必须为每个评论添加“正文”字段到文档:

    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));
    doc1.addField(new Field("body", "skating cats are fun to look at", 
                            Field.Store.YES, Field.Index.ANALYZED));
    

    在文档中有多个同名的字段绝对没问题。实际上,这与在构建“body”字段值之前连接这些字符串几乎相同。

    明显的缺点是,如果没有满足查询的评论,但您会在结果中得到该照片,因为它的连接 cmets 满足该查询,因此您可能会得到奇怪的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多