Lucene.net Textfield未被索引

发布于 2025-01-30 12:24:36 字数 1883 浏览 2 评论 0 原文

使用.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");
}
}

Using .NET 6.0 and Lucene.NET-4.8.0-beta00016 from NuGet

I am having an issue implementing the quickstart example from the website. When using TextField in a document, the field is not indexed. The search later in the BuildIndex method retrieves no results. If TextField is changed to StringField, the example works and the search returns a valid result.

Why does StringField work and TextField doesn't? I read that StringField is not analyzed but TextField is, so perhaps it's something to do with the 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-02-06 12:24:36

StandardAnalyzer 包括

但是,当您构建查询时,您使用的文本是“ Apache”而不是“ Apache”,因此不会产生任何命中。

// Parse the user's query text
Query query = new TermQuery(new Term("title", "Apache"));

选项1

较低的搜索词。

// Parse the user's query text
Query query = new TermQuery(new Term("title", "Apache".ToLowerInvariant()));

选项2

使用a 使用相同分析仪,您可以用来构建索引。

QueryParser parser = new QueryParser(luceneVersion, "title", _standardAnalyzer);
Query query = parser.Parse("Apache");

The Lucene.Net.QueryParser package contains several实现(上面的示例使用 lucene.net.queryparsers.classic.queryparser )。

StandardAnalyzer includes a LowerCaseFilter, 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.

// Parse the user's query text
Query query = new TermQuery(new Term("title", "Apache"));

Option 1

Lowercase your search term.

// Parse the user's query text
Query query = new TermQuery(new Term("title", "Apache".ToLowerInvariant()));

Option 2

Use a QueryParser with the same analyzer you use to build the index.

QueryParser parser = new QueryParser(luceneVersion, "title", _standardAnalyzer);
Query query = parser.Parse("Apache");

The Lucene.Net.QueryParser package contains several implementations (the above example uses the Lucene.Net.QueryParsers.Classic.QueryParser).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文