我使用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;
}
}
提前致谢。
答案 0 :(得分: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;
}
}
}
}