簡體   English   中英

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

[英]How to add special characters in lucene search ? c#

我在搜索引擎中使用來自 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

默認分析器使用完全匹配查詢跳過特殊字符,該查詢將考慮您使用的特殊字符。 https://lucenenet.apache.org/docs/3.0.3/d5/d58/class_lucene_1_1_net_1_1_search_1_1_phrase_query.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM