如何使用 dotnet lucene 从单个文档搜索中检索字段和相应的值?
How to retrieve the fields and respective values from a single document search using 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 测试的形式)用于索引文档、执行搜索然后在 [=13= 中读取文档] 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", Field.Store.YES));
doc.Add(new TextField("exampleField", "Everyone is gifted.", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("examplePrimaryKey", "003", Field.Store.YES));
doc.Add(new TextField("exampleField", "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);
}
}
}
我有一个|分隔的 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 测试的形式)用于索引文档、执行搜索然后在 [=13= 中读取文档] 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", Field.Store.YES));
doc.Add(new TextField("exampleField", "Everyone is gifted.", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("examplePrimaryKey", "003", Field.Store.YES));
doc.Add(new TextField("exampleField", "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);
}
}
}