为什么MultiFieldQueryParser比手动创建查询要慢得多?

时间:2011-12-14 22:22:11

标签: c# lucene.net

我使用Lucene.net索引产品目录。我使用ANTS Profiler分析我的搜索,我注意到使用MultiFieldQueryParser创建和解析查询的行为几乎与实际搜索一样长(大约100ms)。然后我尝试手动创建查询,这种情况发生得非常快(约1毫秒)。我宁愿不必手动解析,虽然它确实给了我相同的结果集,我担心我可能不会处理某些用例或输入(虽然输入来自网站上的文本搜索,用户不会知道关于Lucene的搜索语法的任何内容)。我的代码(使用两种方法)如下:

        IApplicationSettings settings = new ApplicationSettingService();
        FSDirectory directory = FSDirectory.Open(new DirectoryInfo(settings.GetSetting<string>("LuceneMainSearchDirectory")));
        RAMDirectory ramDir = new RAMDirectory(directory);
        _Searcher = new IndexSearcher(ramDir, true);        
        string[] searchFields = new string[] { "ProductName", "ProductLongDescription", "BrandName", "CategoryName" };

        //Add a wildcard character to end of search to give broader results 
        if (!searchTerm.EndsWith(" ")) { searchTerm = searchTerm + "*"; }


        //Use query parser...this  block typically takes about 100ms on my machine, roughly 40% on the constructor and 60% on the call to Parse
        MultiFieldQueryParser multiParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, searchFields, _analyzer);
        multiParser.SetDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = multiParser.Parse(searchTerm);



        //Manually create query....this block doesn't even take 1ms on my machine
        BooleanQuery booleanQuery = new BooleanQuery(true);
        var terms = searchTerm.Split(' ');
        foreach (string s in terms)
        {
            BooleanQuery subQuery = new BooleanQuery(true);
            if (!s.EndsWith("*"))
            {
                Query query1 = new TermQuery(new Term("ProductName", s));
                Query query2 = new TermQuery(new Term("ProductLongDescription", s));
                Query query3 = new TermQuery(new Term("BrandName", s));
                Query query4 = new TermQuery(new Term("CategoryName", s));
                subQuery.Add(query1, BooleanClause.Occur.SHOULD);
                subQuery.Add(query2, BooleanClause.Occur.SHOULD);
                subQuery.Add(query3, BooleanClause.Occur.SHOULD);
                subQuery.Add(query4, BooleanClause.Occur.SHOULD);
            }
            else
            {
                Query query1 = new WildcardQuery(new Term("ProductName", s));
                Query query2 = new WildcardQuery(new Term("ProductLongDescription", s));
                Query query3 = new WildcardQuery(new Term("BrandName", s));
                Query query4 = new WildcardQuery(new Term("CategoryName", s));
                subQuery.Add(query1, BooleanClause.Occur.SHOULD);
                subQuery.Add(query2, BooleanClause.Occur.SHOULD);
                subQuery.Add(query3, BooleanClause.Occur.SHOULD);
                subQuery.Add(query4, BooleanClause.Occur.SHOULD);
            }
            booleanQuery.Add(subQuery, BooleanClause.Occur.MUST);
        }


    //Run the search....results are the same for simple multiword text queries
        var result2 = _Searcher.Search(booleanQuery, null, maxResults);
        var result = _Searcher.Search(query, null, maxResults);

使用手动查询构建保存我的一个选项可能是共享MultiFieldQueryParser,但我收集它的Parse方法不是线程安全的(虽然我只读了与java版本相关的...如果我是的话请纠正我错误的假设)。

我做错了什么,或者这只是野兽的本性?

1 个答案:

答案 0 :(得分:5)

MultiFieldQueryParser只是在场景下使用多个常规QueryParsers,它会为每个要查询的字段创建一个。

QueryParser的创建费用高于仅手动创建Query的费用,这是正常的。

它处理这里记录的复杂查询语法:Apache Lucene - Query Parser Syntax

它还将使用您指定的Analyzer处理搜索查询。如果您在索引时使用Analyzer,则必须在搜索代码中使用相同的Analyzer /逻辑。如果你不这样做,你最终会错过结果。

如果您使用空白分析器进行索引,那么手动构建BooleanQuery的代码就可以了。