我之前的question旁边。我现在想执行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的字段“Title”,但仅限于收藏中的书籍。所以我需要在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),
};
}
}
答案 0 :(得分:0)
图书标题经常不会改变,所以为什么不在Title
中加入BookFavourite
,如下所示:
public class BookFavorite
{
public string Id { get; set; }
public string UserId { get; set; }
public string BookTitle { get; set; }
}
然后,您只需查询BookTitle
个对象中的BookFavourite
。
您将复制数据,但数据不会更改,因此您无需担心更新数据。
更新(根据额外信息)
如果在IsFavourite
课程中存储Book
字段,您可以在查询中使用该字段吗?你只需要保持这个字段,因为书是“有利的”。
public class Book
{
public string Id { get; set; }
public string Title { get; set; }
public bool IsFavourite { get; set; }
}
答案 1 :(得分:-1)
我相信这个答案可以解决你在我问过的类似问题中将Book和BookFavorite联系起来的问题: Link
编辑:它假设您的Book和BookFavorite包含在个别列表中