0

我正在使用 PerFieldAnalyzer 将 ExactTitle 设置为使用新的 LowercaseKeywordAnalyzer:

private Analyzer GetDefaultAnalyzer()
    {
        var perFieldAnalyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_30));
        perFieldAnalyzer.AddAnalyzer(ReportFields.ExactTitle, new LowercaseKeywordAnalyzer());
...

我正在构建索引,在字段中添加:

var exactTitleField = new Field(ReportFields.ExactTitle, report.PortalReportTitle, Field.Store.NO,
                                            Field.Index.NOT_ANALYZED);
            exactTitleField.Boost = 10.0f;
            reportDoc.Add(exactTitleField);

当我使用 2 字示例“test abc”对其进行搜索时,当我对它进行 TermQuery 搜索时找不到它:

var term = new Term(exactField, "test abc");
var exactQuery = new TermQuery(term);
query.Add(exactQuery,Occur.SHOULD);
var hits = searcher.Search(query, null, HitsLimit, Sort.RELEVANCE);

如果我搜索“Test Abc”,它会起作用。我该怎么做才能使这种不区分大小写的关键字/术语搜索工作?

这是分析器:

public class LowercaseKeywordAnalyzer : Analyzer
{

    public override TokenStream TokenStream(string fieldName, System.IO.TextReader reader)
    {
        TokenStream tokenStream = new KeywordTokenizer(reader);
        tokenStream = new LowerCaseFilter(tokenStream);
        return tokenStream;
    }
}

提前致谢。

4

1 回答 1

2

当您指定Field.Index.NOT_ANALYZED创建字段的时间时,分析器将不会运行并且文本不会转换为小写

切换它Field.Index.ANALYZED

编辑

也许您的索引代码存在问题,并且不要忘记,如果您在索引时小写,您还将在搜索时将输入小写以获得匹配。

由于您手动构建查询,因此您需要自己处理它。理想情况下,您希望在搜索时Analyzer构建您的字符串之前运行相同的字符串。Term

我已经提供了一个快速的小示例,可以完成您似乎正在尝试做的事情,也许它会帮助您找出问题所在。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            RAMDirectory dir = new RAMDirectory();
            var perFieldAnalyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
            perFieldAnalyzer.AddAnalyzer("ExactTitle", new LowercaseKeywordAnalyzer());

            IndexWriter indexWriter = new IndexWriter(dir, perFieldAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            Document reportDoc = new Document();
            Field exactTitleField = new Field("ExactTitle", 
                                                "Test Abc", 
                                                Field.Store.NO,
                                                Field.Index.ANALYZED);

            reportDoc.Add(exactTitleField);
            indexWriter.AddDocument(reportDoc);
            indexWriter.Commit();

            IndexSearcher searcher = new IndexSearcher(indexWriter.GetReader());

            var term = new Term("ExactTitle", "test abc"); //note: for this to work this way you need to always lower case the search too
            var exactQuery = new TermQuery(term);

            var hits = searcher.Search(exactQuery, null, 25, Sort.RELEVANCE);
            Console.WriteLine(hits.TotalHits); // prints "1"
            Console.ReadLine();

            indexWriter.Close();

        }

        public class LowercaseKeywordAnalyzer : Analyzer
        {

            public override TokenStream TokenStream(string fieldName, System.IO.TextReader reader)
            {
                TokenStream tokenStream = new KeywordTokenizer(reader);
                tokenStream = new LowerCaseFilter(tokenStream);
                return tokenStream;
            }
        }
    }
}
于 2013-03-18T19:09:43.627 回答