I am trying to do a relational search with Lucene.NET 4.8 (actually I compiled it using the latest sources) by following this post. I reference Lucene.Net
, Lucene.Net.Analysis.Common
, Lucene.Net.Grouping
, Lucene.Net.Join
, and Lucene.Net.QueryParser
.
The problem is: I do not get any results. In my example below I consider blog
the parent
while comments
are the children
. I want to find a blog which contains first
and which has a comment containing like
(which is the one with Id
1).
How to fix this sample code?
static void BlockJoinQueryTest(string dbFolder)
{
var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
var config = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer);
config.SetOpenMode(IndexWriterConfig.OpenMode_e.CREATE_OR_APPEND);
var indexPathBlog = dbFolder + "\\blog_db";
if (System.IO.Directory.Exists(indexPathBlog))
{
System.IO.Directory.Delete(indexPathBlog, true);
}
System.IO.Directory.CreateDirectory(indexPathBlog);
var indexDirectoryBlog = FSDirectory.Open(new System.IO.DirectoryInfo(indexPathBlog));
var indexWriterBlog = new IndexWriter(indexDirectoryBlog, config);
Document comment = new Document();
comment.Add(new TextField("BlogId", "1", Field.Store.YES));
comment.Add(new TextField("CommentContent", "I like your first blog!", Field.Store.YES));
comment.Add(new TextField("Type", "comment", Field.Store.YES));
comment.Add(new TextField("Note", "child", Field.Store.YES));
indexWriterBlog.AddDocument(comment);
comment = new Document();
comment.Add(new TextField("BlogId", "1", Field.Store.YES));
comment.Add(new TextField("CommentContent", "Not that great.", Field.Store.YES));
comment.Add(new TextField("Type", "comment", Field.Store.YES));
comment.Add(new TextField("Note", "child", Field.Store.YES));
indexWriterBlog.AddDocument(comment);
Document blog = new Document();
blog.Add(new TextField("Id", "1", Field.Store.YES));
blog.Add(new TextField("BlogContent", "Content of first blog", Field.Store.YES));
blog.Add(new TextField("Type", "blog", Field.Store.YES));
blog.Add(new TextField("Note", "parent", Field.Store.YES));
indexWriterBlog.AddDocument(blog);
blog = new Document();
blog.Add(new TextField("Id", "2", Field.Store.YES));
blog.Add(new TextField("BlogContent", "This is the second blog!", Field.Store.YES));
blog.Add(new TextField("Type", "blog", Field.Store.YES));
blog.Add(new TextField("Note", "parent", Field.Store.YES));
indexWriterBlog.AddDocument(blog);
indexWriterBlog.Commit();
var searcher = new IndexSearcher(DirectoryReader.Open(indexDirectoryBlog));
Console.WriteLine("Begin content enumeration:");
for (int i = 0; i < searcher.IndexReader.MaxDoc; i++)
{
var doc = searcher.IndexReader.Document(i);
Console.WriteLine("Document " + i + ": " + doc.ToString());
}
Console.WriteLine("End content enumeration.");
Filter blogs = new CachingWrapperFilter(
new QueryWrapperFilter(
new TermQuery(
new Term("Type", "blog"))));
BooleanQuery commentQuery = new BooleanQuery();
commentQuery.Add(new TermQuery(new Term("CommentContent", "like")), BooleanClause.Occur.MUST);
//commentQuery.Add(new TermQuery(new Term("BlogId", "1")), BooleanClause.Occur.MUST);
var commentJoinQuery = new ToParentBlockJoinQuery(
commentQuery,
blogs,
ScoreMode.None);
BooleanQuery query = new BooleanQuery();
query.Add(new TermQuery(new Term("BlogContent", "first")), BooleanClause.Occur.MUST);
query.Add(commentQuery, BooleanClause.Occur.MUST);
var c = new ToParentBlockJoinCollector(
Sort.RELEVANCE, // sort
10, // numHits
true, // trackScores
false // trackMaxScore
);
searcher.Search(query, c);
int maxDocsPerGroup = 10;
var hits = c.GetTopGroups(
commentJoinQuery,
Sort.INDEXORDER,
0, // offset
maxDocsPerGroup, // maxDocsPerGroup
0, // withinGroupOffset
true // fillSortFields
);
if (hits != null)
{
Console.WriteLine("Found " + hits.TotalGroupCount + " groups:");
for (int i = 0; i < hits.TotalGroupCount; i++)
{
var group = hits.Groups[i];
Console.WriteLine("Group " + i + ": " + group.ToString());
for (int j = 0; j < group.TotalHits && j < maxDocsPerGroup; j++)
{
Document doc = searcher.Doc(group.ScoreDocs[j].Doc);
Console.WriteLine("Hit " + i + ": " + doc.ToString());
}
}
}
else
{
Console.WriteLine("No hits.");
}
Console.WriteLine("Done.");