在我之前的问题旁边。我现在想执行 lucene 查询搜索。
这是我的模型:
public class User
{
public string Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class Book
{
public string Id { get; set; }
public string Title { get; set; }
}
public class BookFavorite
{
public string Id { get; set; }
public string UserId { get; set; }
public string BookId { get; set; }
}
所以,我想搜索书籍的“标题”字段,但只能在收藏夹中的书籍中搜索。所以我需要在 Book 和 BookFavorite 和索引标题字段之间建立一种连接。
我已经尝试过使用 MultiMap Index,但我发现很难让它工作。
有什么帮助吗?谢谢。
编辑:MultiMap 索引
public class BookFavoriteSearchIndex : AbstractMultiMapIndexCreationTask<BookFavoriteSearchIndex.ReduceResult>
{
public class ReduceResult
{
public string BookId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Abstract { get; set; }
public string Tag { get; set; }
}
public BookFavoriteSearchIndex()
{
AddMap<Book>(books => from book in books
from tag in book.Tags
select new
{
BookId = book.Id,
Title = book.Title,
Description = book.Description,
Abstract = book.Abstract,
Tag = tag
});
AddMap<BookFavorite>(bfs => from bf in bfs
select new
{
BookId = bf.BookId,
Title = (string)null,
Description = (string)null,
Abstract = (string)null,
Tag = (string)null
});
Reduce = results => from result in results
group result by result.BookId
into g
select new
{
BookId = g.Key,
Title = g.Select(x => x.Title).FirstOrDefault(x => x != null),
Description = g.Select(x => x.Description).FirstOrDefault(x => x != null),
Abstract = g.Select(x => x.Abstract).FirstOrDefault(x => x != null),
Tag = g.Select(x => x.Tag).FirstOrDefault(x => x != null),
};
}
}