【问题标题】:Is it possible to find exact matches only when searching for a phrase in Lucene.net?只有在 Lucene.net 中搜索短语时才能找到完全匹配?
【发布时间】:2012-10-13 12:27:43
【问题描述】:

我知道已经有人问过类似的问题,但我找不到任何适合我正在寻找的答案。

基本上,我想搜索短语并仅返回具有该确切短语的匹配而不是部分匹配。

例如如果我搜索“这是”,则文档具有“这是一个短语”应该返回匹配项。

以此为例:Exact Phrase search using Lucene?

"foo bar" 不应返回命中,因为它只是部分匹配。我正在寻找的完整匹配将是“foo bar baz”。

这是代码,感谢 WhiteFang34 在上面的链接中发布此代码(我只是转换为 c#):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Analysis;
using Lucene.Net.Store;
using Lucene.Net.Index;

namespace LuceneStatic
{
    public static class LuceneStatic
    {
        public static void LucenePhraseQuery()
        {
            // setup Lucene to use an in-memory index
            Lucene.Net.Store.Directory directory = new RAMDirectory();
            Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            var mlf = Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED;
            IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);

            // index a few documents
            writer.AddDocument(createDocument("1", "foo bar baz"));
            writer.AddDocument(createDocument("2", "red green blue"));
            writer.AddDocument(createDocument("3", "test foo bar test"));
            writer.Close();

            // search for documents that have "foo bar" in them
            String sentence = "foo bar";
            IndexSearcher searcher = new IndexSearcher(directory, true);
            PhraseQuery query = new PhraseQuery();
            string[] words = sentence.Split(' ');
            foreach (var word in words)
            {
                query.Add(new Term("contents", word));
            }

            // display search results
            List<string> results = new List<string>();
            TopDocs topDocs = searcher.Search(query, 10);
            foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
            {
                Document doc = searcher.Doc(scoreDoc.doc);
                results.Add(doc.Get("contents"));
            }
        }

        private static Document createDocument(string id, string content)
        {
            Document doc = new Document();
            doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("contents", content, Field.Store.YES, Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS));
            return doc;
        }
    }
}

我已经使用差异分析器和不同的方法来解决这个问题,但我无法获得所需的结果。我需要匹配完整的短语“foo bar baz”,但“foo bar”应该返回任何命中。

【问题讨论】:

    标签: c# lucene.net phrase


    【解决方案1】:

    在创建字段时使用Field.Index.NOT_ANALYZED 参数索引您的数据。这将导致整个值被索引为单个 Term

    然后您可以使用简单的 TermQuery 对其进行搜索。

    https://lucene.apache.org/core/old_versioned_docs/versions/3_0_1/api/all/org/apache/lucene/document/Field.Index.html#NOT_ANALYZED

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多