如何使用 dotnet lucene 从单个文档搜索中检索字段和相应的值?

问题描述

我有一个 |分隔的 txt 文档,其中包含 ScenarioId、输入值、数据库值等字段 我正在索引这些值。

doc.Add(new Field("Database value",List.Databasevalue,Field.Store.YES,Field.Index.ANALYZED));
 var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30,"Database value",analyzer);
 Console.WriteLine("Text found: {0}" + Environment.NewLine,document.Get("Database value"));

场景ID|输入值|数据库值 1|阿克谢|阿克谢·库马尔 2|阿克萨斯|阿克萨斯 T 3|保罗|约翰保罗 4|亚伯拉罕|亚伯拉罕约瑟夫 5|莫里斯|莫里斯约翰逊

因为我的输入只有一个文档。我不在乎文件编号。如果有任何匹配,我需要从输入文件和相应的分数中检索相应的数据库值。我如何实现它? Document 里有个 Get 函数我不知道它是怎么工作的!有人可以帮助我,因为 dotnet lucene 的可用资源有限

解决方法

我仍然有点不清楚您需要什么,但这里有一些示例代码(以 xUnit 测试的形式)用于索引文档、执行搜索然后在 Lucene.NET 4.8 中读取文档。

[Fact]
        public void StandardAnalyzerExample() {

            Directory indexDir = new RAMDirectory();

            Analyzer standardAnalyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);

            IndexWriterConfig indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48,standardAnalyzer);
            indexConfig.UseCompoundFile = true;

            IndexWriter writer = new IndexWriter(indexDir,indexConfig);


            SearcherManager searcherManager = new SearcherManager(writer,applyAllDeletes: true,new SearchWarmer());

            Document doc = new Document();
            doc.Add(new StringField("examplePrimaryKey","001",Field.Store.YES));
            doc.Add(new TextField("exampleField","Unique gifts are great gifts.",Field.Store.YES));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new StringField("examplePrimaryKey","002","Everyone is gifted.","003","Gifts are meant to be shared.",Field.Store.YES));
            writer.AddDocument(doc);

            writer.Commit();

            searcherManager.MaybeRefreshBlocking();
            IndexSearcher indexSearcher = searcherManager.Acquire();
            try {
                QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48,"exampleField",standardAnalyzer);
                Query query = parser.Parse("everyone");

                TopDocs topDocs = indexSearcher.Search(query,int.MaxValue);

                int numMatchingDocs = topDocs.ScoreDocs.Length;
                Assert.Equal(1,numMatchingDocs);


                Document docRead = indexSearcher.Doc(topDocs.ScoreDocs[0].Doc);     //this is how you read back a doc
                string primaryKey = docRead.Get("examplePrimaryKey");
                Assert.Equal("002",primaryKey);

            } finally {
                searcherManager.Release(indexSearcher);
            }

        }

    }