我有多个索引,可以独立搜索每个索引,但我希望对所有索引进行整合搜索。我看过几篇推荐使用多重阅读器的帖子,但我无法得到我想要的结果。有关详细信息,请参阅下面的单元测试。
[Test]
public void Given_Muliple_Documents_With_SameId_Diff_Attribute_Expect_Matches()
{
//Arrange
var source1 = new[] {new Stub("1", "Car"), new Stub("2", "Bike"), new Stub("3", "Car")};
var source2 = new[] { new Stub("1", "Ford"), new Stub("3", "Ducati"), new Stub("4", "Honda") };
var idField = "Id";
var searchField = "FullText";
var directory1 = CreateRAMDirectory(source1, idField, searchField);
var directory2 = CreateRAMDirectory(source2, idField, searchField);
//Act
var reader = IndexReader.Open(directory1, true);
var reader2 = IndexReader.Open(directory2, true);
var results = new List<string>();
using (IndexSearcher searcher = new IndexSearcher(new MultiReader(reader, reader2)))
{
using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
{
var queryParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new[] { "FullText" }, analyzer)
{
AllowLeadingWildcard = true
};
var query = queryParser.Parse("CAR AND Ford");
var collector = TopScoreDocCollector.Create(1000, true);
searcher.Search(query, collector);
var matches = collector.TopDocs().ScoreDocs;
foreach (var item in matches)
{
var id = item.Doc;
var doc = searcher.Doc(id);
var row = doc.Get("Id");
results.Add(row);
}
}
}
//Assert
CollectionAssert.AreEquivalent(new[] {"1"}, results.Distinct().ToArray());
}
以下存根和帮助器方法定义
public class Stub
{
public Stub(string id, string attrib)
{
Id = id;
Attrib = attrib;
}
public string Id { get; }
public string Attrib { get; }
}
private static RAMDirectory CreateRAMDirectory(Stub[] source1, string idField, string searchField)
{
var directory = new RAMDirectory();
using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
using (var writer = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(1000)))
{
foreach (var row in source1)
{
var document = new Document();
document.Add(new Field(idField, row.Id, Field.Store.YES, Field.Index.NO));
document.Add(new Field(searchField, $"{row.Attrib}", Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(document);
}
writer.Optimize();
writer.Flush(true, true, true);
}
return directory;
}