Lucene.net Textfield未被索引
使用.NET 6.0和Nuget的Lucene.net-4.8.0-Beta00016,
我正在实施 QuickStart示例来自网站。在文档中使用TextField时,该字段未索引。稍后在BuildIndex方法中搜索未检索任何结果。如果TextField更改为StringField,则该示例有效,搜索将返回有效的结果。
为什么StringField工作和Textfield不工作?我读到Stringfield尚未分析,但是Textfield是,所以也许与Standardanalyzer有关?
public class LuceneFullTextSearchService {
private readonly IndexWriter _writer;
private readonly Analyzer _standardAnalyzer;
public LuceneFullTextSearchService(string indexName)
{
// Compatibility version
const LuceneVersion luceneVersion = LuceneVersion.LUCENE_48;
string indexPath = Path.Combine(Environment.CurrentDirectory, indexName);
Directory indexDir = FSDirectory.Open(indexPath);
// Create an analyzer to process the text
_standardAnalyzer = new StandardAnalyzer(luceneVersion);
// Create an index writer
IndexWriterConfig indexConfig = new IndexWriterConfig(luceneVersion, _standardAnalyzer)
{
OpenMode = OpenMode.CREATE_OR_APPEND,
};
_writer = new IndexWriter(indexDir, indexConfig);
}
public void BuildIndex(string searchPath)
{
Document doc = new Document();
TextField docText = new TextField("title", "Apache", Field.Store.YES);
doc.Add(docText);
_writer.AddDocument(doc);
//Flush and commit the index data to the directory
_writer.Commit();
// Parse the user's query text
Query query = new TermQuery(new Term("title", "Apache"));
// Search
using DirectoryReader reader = _writer.GetReader(applyAllDeletes: true);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.Search(query, n: 2);
// Show results
Document resultDoc = searcher.Doc(topDocs.ScoreDocs[0].Doc);
string title = resultDoc.Get("title");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StandardAnalyzer 包括
但是,当您构建查询时,您使用的文本是“ Apache”而不是“ Apache”,因此不会产生任何命中。
选项1
较低的搜索词。
选项2
使用a 使用相同分析仪,您可以用来构建索引。
The
Lucene.Net.QueryParser
package contains several实现(上面的示例使用lucene.net.queryparsers.classic.queryparser
)。StandardAnalyzer
includes aLowerCaseFilter
, so your text is stored in the index as lower-case.However, when you build your query, the text you use is "Apache" rather than "apache", so it doesn't produce any hits.
Option 1
Lowercase your search term.
Option 2
Use a
QueryParser
with the same analyzer you use to build the index.The
Lucene.Net.QueryParser
package contains several implementations (the above example uses theLucene.Net.QueryParsers.Classic.QueryParser
).