c# - 如何在 lucene 搜索中添加特殊字符? C#

标签 c# .net lucene

我在我的搜索引擎中使用来自 lucene 的标准分析器来搜索德语单词这是我的代码:

private IList<Document> GetFromLucene(string terme, string FieldName)
    {
        TopDocs hits;
        CustomAnalyzer standardAnalyzer = new CustomAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
        List<Document> matches = new List<Document>();
        IndexSearcher indexSearcher = new IndexSearcher(FSDirectory.Open(new System.IO.DirectoryInfo(MainDoc + DocIndex)), true);

        if (terme.Contains(" "))
        {
            BooleanQuery finalQuery = new BooleanQuery();
            string[] terms = terme.Split(' ');

            #region AND
            QueryParser queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, FieldName, standardAnalyzer)
            {
                DefaultOperator = QueryParser.Operator.AND
            };
            #endregion

            #region Contains
            Query querys = queryParser.Parse("" + terme + "*");
            finalQuery.Add(querys, Occur.MUST);
            #endregion

            hits = indexSearcher.Search(finalQuery, int.MaxValue);
        }
        else
        {
            WildcardQuery query;
            query = new WildcardQuery(new Term(FieldName, "*" + terme + "*"));
            hits = indexSearcher.Search(query, int.MaxValue);
        }


        matches = hits.ScoreDocs.Select(scoreDoc => indexSearcher.Doc(scoreDoc.Doc)).ToList();

        return matches;
    }

它似乎没有找到包含“ü”和“ä”的单词。 我怎样才能做到这一点?

最佳答案

Lucene 使用所谓的分析器类来检查文本中的索引术语并生成标记流。要实现不区分重音的搜索,您可以将 Lucene 使用的默认分析器替换为将重音字符替换为相应的非重音字符的分析器。 Sitefinity CMS 有一个示例:https://www.progress.com/documentation/sitefinity-cms/for-developers-search-with-accented-characters

关于c# - 如何在 lucene 搜索中添加特殊字符? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72561514/

相关文章:

c# - DataGridTextColumn DateTime 绑定(bind)日期格式

.net - 如何先用 Entity Framework 数据库获取数据库信息?

c# - 意外 - 无法比较类型为 'System.Int32[]' 的元素。仅支持原始类型、枚举类型和实体类型

c# - 写入后是否必须优化lucene索引?

c# - Blazor 将多个选择绑定(bind)到一个值

c# - 我如何在没有打开图像 ui 的情况下在 C# 中的 Web 浏览器控件中插入图像?

c# - 按配置文件属性搜索用户

c# - 本地对象的垃圾收集

hibernate 搜索和关系

date - 使用 Lucene.Net 在两个日期之间搜索