1

我正在尝试转换我的搜索功能以允许涉及多个单词的模糊搜索。我现有的搜索代码如下所示:

        // Split the search into seperate queries per word, and combine them into one major query
        var finalQuery = new BooleanQuery();

        string[] terms = searchString.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string term in terms)
        {
            // Setup the fields to search
            string[] searchfields = new string[] 
            {
                // Various strings denoting the document fields available
            };

            var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, searchfields, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
            finalQuery.Add(parser.Parse(term), BooleanClause.Occur.MUST);
        }

        // Perform the search
        var directory = FSDirectory.Open(new DirectoryInfo(LuceneIndexBaseDirectory));
        var searcher = new IndexSearcher(directory, true);
        var hits = searcher.Search(finalQuery, MAX_RESULTS);

这可以正常工作,如果我有一个名称字段为“我的名字是 Andrew”的实体,并且我执行搜索“Andrew Name”,Lucene 会正确找到正确的文档。现在我想启用模糊搜索,以便正确找到“Anderw Name”。我更改了方法以使用以下代码:

        const int MAX_RESULTS = 10000;
        const float MIN_SIMILARITY = 0.5f;
        const int PREFIX_LENGTH = 3;

        if (string.IsNullOrWhiteSpace(searchString))
            throw new ArgumentException("Provided search string is empty");

        // Split the search into seperate queries per word, and combine them into one major query
        var finalQuery = new BooleanQuery();

        string[] terms = searchString.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string term in terms)
        {
            // Setup the fields to search
            string[] searchfields = new string[] 
            {
                // Strings denoting document field names here
            };

            // Create a subquery where the term must match at least one of the fields
            var subquery = new BooleanQuery();
            foreach (string field in searchfields)
            {
                var queryTerm = new Term(field, term);
                var fuzzyQuery = new FuzzyQuery(queryTerm, MIN_SIMILARITY, PREFIX_LENGTH);
                subquery.Add(fuzzyQuery, BooleanClause.Occur.SHOULD);
            }

            // Add the subquery to the final query, but make at least one subquery match must be found
            finalQuery.Add(subquery, BooleanClause.Occur.MUST);
        }

        // Perform the search
        var directory = FSDirectory.Open(new DirectoryInfo(LuceneIndexBaseDirectory));
        var searcher = new IndexSearcher(directory, true);
        var hits = searcher.Search(finalQuery, MAX_RESULTS);

不幸的是,使用此代码,如果我提交搜索查询“Andrew Name”(与以前相同),我得到的结果为零。

核心思想是所有术语必须至少在一个文档字段中找到,但每个术语可以驻留在不同的字段中。有谁知道为什么我重写的查询失败了?


最终编辑:好吧,事实证明我把这个复杂化了很多,而且没有必要改变我的第一种方法。恢复到第一个代码片段后,我通过更改启用了模糊搜索

finalQuery.Add(parser.Parse(term), BooleanClause.Occur.MUST);

finalQuery.Add(parser.Parse(term.Replace("~", "") + "~"), BooleanClause.Occur.MUST);
4

2 回答 2

3

如果我将 重写searchString为小写,您的代码对我有用。我假设您正在使用StandardAnalyzerwhen 索引,它会生成小写术语。

您需要 1) 通过相同的分析器传递您的令牌(以启用相同的处理),2) 应用与分析器相同的逻辑或 3) 使用与您执行的处理匹配的分析器 ( WhitespaceAnalyzer)。

于 2011-05-26T03:53:47.627 回答
1

你想要这条线:

var queryTerm = new Term(term);

看起来像这样:

var queryTerm = new Term(field, term);

现在,您正在搜索term空字符串(永远不会找到)的字段(可能不存在)。

于 2011-05-25T17:57:58.183 回答