为什么此Lucene.Net查询失败?

| 我正在尝试转换我的搜索功能,以允许涉及多个单词的模糊搜索。我现有的搜索代码如下:
        // 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);
这可以正常工作,并且如果我有一个名称字段为“我的名字是安德鲁”的实体,并且执行了“安德鲁名称”的搜索,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重写为小写,则您的代码对我有用。我假设您在建立索引时使用的是
StandardAnalyzer
,它将产生小写字母。 您需要1)通过同一个分析器传递令牌(以进行相同的处理),2)应用与分析器相同的逻辑,或3)使用与您进行的处理相匹配的分析器(
WhitespaceAnalyzer
)。     
您需要以下行:
var queryTerm = new Term(term);
看起来像这样:
var queryTerm = new Term(field, term);
现在,您正在搜索字段
term
(可能不存在)以查找空字符串(永远不会找到)。     

要回复问题请先登录注册