BooleanQuery无法使用Lucene.net 3.0.3

时间:2015-12-17 03:36:20

标签: c# lucene.net booleanquery

为什么第一个查询有效,但第二个查询在Lucene.net上使用BooleanQuery时没有?

以下是对SHOPPING字段进行索引的示例:

  

出售鞋子

     

鞋子和配饰的讨价还价。

     

买便宜的鞋子,Sneekers和高跟鞋!

First Query(效果很好,它返回所有三个索引)。

Query query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_30, 
    "Shoes", new String[] { "SHOPPING" }, new Occur[] { Occur.SHOULD }, 
     new SimpleAnalyzer()
);
hits = indexSearcher.search(query);

第二个查询(不返回任何条件,条件是:使用'鞋'返回所有条款但没有'附件')。

Query query1 = new TermQuery(new Term("SHOPPING", "shoes"));
Query query2 = new TermQuery(new Term("SHOPPING", "accessories"));

BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.Add(query1, Occur.SHOULD);
booleanQuery.Add(query2, Occur.MUST_NOT);

hits = indexSearcher.search(booleanQuery);

2 个答案:

答案 0 :(得分:1)

我想我明白了。假设将MultiFieldQueryParser和BooleanQuery结合起来就可以了。

    Query query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_30, "Shoes", new String[] { "SHOPPING" }, new SimpleAnalyzer());

    Query queryOrig = parser.Parse("shoes");
    Query queryOrig2 = parser.Parse("accessories");

    var booleanQuery= new BooleanQuery();
    booleanQuery.Add(queryOrig, Occur.MUST);
    booleanQuery.Add(queryOrig2, Occur.MUST_NOT);

    hits = indexSearcher.search(booleanQuery);

答案 1 :(得分:0)

继我对你之前的答案的评论......这里是几个单元测试的代码,显示了一个有效的例子。第一个测试使用解析器。第二个是Query对象版本

[TestClass]
public class UnitTest6
{
    [TestMethod]
    public void TestShopping()
    {
        var writer = CreateIndex();
        Add(writer, "Shoes for sale");
        Add(writer, "Great bargain on shoes and accessories.");
        Add(writer, "Buy cheap Shoes, Sneekers and Heels!");
        writer.Flush(true, true, true);

        var searcher = new IndexSearcher(writer.GetReader());
        var result1 = Search(searcher, "shoes NOT accessories");

        Assert.AreEqual(2, result1.Count);
        Assert.IsTrue(result1.Contains("Shoes for sale"));
        Assert.IsTrue(result1.Contains("Buy cheap Shoes, Sneekers and Heels!"));

        writer.Dispose();
    }
    [TestMethod]
    public void TestShopping2()
    {
        var writer = CreateIndex();
        Add(writer, "Shoes for sale");
        Add(writer, "Great bargain on shoes and accessories.");
        Add(writer, "Buy cheap Shoes, Sneekers and Heels!");
        writer.Flush(true, true, true);

        var searcher = new IndexSearcher(writer.GetReader());

        var query1 = new TermQuery(new Term("shopping", "shoes"));
        var query2 = new TermQuery(new Term("shopping", "accessories"));

        var booleanQuery = new BooleanQuery();
        booleanQuery.Add(query1, Occur.SHOULD);
        booleanQuery.Add(query2, Occur.MUST_NOT);

        var result1 = Search(searcher, booleanQuery);

        Assert.AreEqual(2, result1.Count);
        Assert.IsTrue(result1.Contains("Shoes for sale"));
        Assert.IsTrue(result1.Contains("Buy cheap Shoes, Sneekers and Heels!"));

        writer.Dispose();
    }

    private void Test(IndexSearcher searcher, string query)
    {
        var result = Search(searcher, query);
        Console.WriteLine(string.Join("\n", result));
        Assert.AreEqual(1, result.Count);
    }

    private List<string> Search(IndexSearcher searcher, string expr)
    {
        var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
        var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "shopping", analyzer);
        var query = queryParser.Parse(expr);
        return Search(searcher, query);
    }
    private List<string> Search(IndexSearcher searcher, Query query)
    {
        var collector = TopScoreDocCollector.Create(1000, true);

        searcher.Search(query, collector);

        var result = new List<string>();
        var matches = collector.TopDocs().ScoreDocs;
        foreach (var item in matches)
        {
            var id = item.Doc;
            var doc = searcher.Doc(id);
            result.Add(doc.GetField("shopping").StringValue);
        }
        return result;
    }

    IndexWriter CreateIndex()
    {
        var directory = new RAMDirectory();

        var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
        var writer = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(1000));

        return writer;
    }
    void Add(IndexWriter writer, string text)
    {
        var document = new Document();
        document.Add(new Field("shopping", text, Field.Store.YES, Field.Index.ANALYZED));

        writer.AddDocument(document);
    }
}