【问题标题】:How do I perform an AND search in Lucene.net when multiple words are used in a search?当搜索中使用多个单词时,如何在 Lucene.net 中执行 AND 搜索?
【发布时间】:2011-08-20 12:47:18
【问题描述】:

我正在使用 Lucene.net 来尝试了解如何在我的应用程序中实现它。

我有以下代码

            .....
            // Add 2 documents
            var doc1 = new Document();
            var doc2 = new Document();

            doc1.Add(new Field("id", "doc1", Field.Store.YES, Field.Index.ANALYZED));
            doc1.Add(new Field("content", "This is my first document", Field.Store.YES, Field.Index.ANALYZED));
            doc2.Add(new Field("id", "doc2", Field.Store.YES, Field.Index.ANALYZED));
            doc2.Add(new Field("content", "The big red fox jumped", Field.Store.YES, Field.Index.ANALYZED));

            writer.AddDocument(doc1);
            writer.AddDocument(doc2);

            writer.Optimize();
            writer.Close();

            // Search for doc2
            var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "content", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
            var query = parser.Parse("big abcdefg test1234");
            var searcher = new IndexSearcher(indexDirectory, true);
            var hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length());

            var document = hits.Doc(0);

            Assert.AreEqual("doc2", document.Get("id"));
            Assert.AreEqual("The big red fox jumped", document.Get("content"));

这个测试通过了,这让我有点沮丧。我认为这意味着 Lucene.Net 使用 OR 来搜索术语而不是 AND,但我找不到任何有关如何实际执行 AND 搜索的信息。

我想要的最终结果是,如果有人搜索“Matthew Anderson”,我不希望它显示引用“Matthew Doe”的文档,因为这与任何方式、形状或形式都不相关.

【问题讨论】:

    标签: c# search lucene lucene.net


    【解决方案1】:

    A.如果您要求所有单词都在文档中,但不要求单词是连续的并且按照您指定的顺序:查询

    +big +red
    

    匹配

    * the big red fox jumped
    * the red big fox jumped
    * the big fast red fox jumped
    

    但不匹配

    * the small red fox jumped
    

    B.如果你想匹配一个短语(即所有单词都需要;单词必须是连续的并且按照指定的顺序):查询

    +"big red"
    

    匹配

    * the big red fox jumped
    

    但不匹配

    * the red big fox jumped
    * the big fast red fox jumped
    * the small red fox jumped
    

    【讨论】:

    • 好吧,这是有道理的。将此标记为答案,因为您也给了我连续匹配的示例。我想我需要编写一个字符串解析器来将用户输入转换为正确的 lucene 查询字符串,这应该很有趣。
    • 不好意思问了这么久。如果我想返回数据,如果有单词 foudn 称为 Red but big 不存在,或者 big 存在但 red 不存在,那么查询应该是什么样子。
    • @Thomas 查询语法支持通过括号对子句进行分组。所以,这应该工作:(+red -big) (+big -red)
    • 我们如何为此添加“包含”查询,例如“+“big red”
    【解决方案2】:

    当您的查询是 var query = parser.Parse("+big +abcdefg +test1234"); 时,您会得到什么这应该导致解析器要求所有术语都出现在匹配的文档中。另一种可能性是以编程方式构造查询。

    BooleanQuery query = new BooleanQuery();
    query.add(new BooleanClause(new TermQuery(new Term("field", "big"))), Occur.MUST);
    query.add(new BooleanClause(new TermQuery(new Term("field", "abcdefg"))), Occur.MUST);
    query.add(new BooleanClause(new TermQuery(new Term("field", "test1234"))), Occur.MUST);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多