首先,我为我的英语感到抱歉,如果我能非常简单地说出我的问题,我会很高兴。
我花了很多时间来解决多地图索引和实时投影问题。我在 stackoverflow、google、ayende 博客等上读了太多……但是无法解决我的问题。
我想要什么: 我有一个应用程序,想要一个类似推特的搜索,它是推特搜索框从多个来源搜索,例如推特内容、用户名和主题标签。当我得到结果时,我想将结果和形状索引结果的转换应用到 FullSearchResult 模型中。我也想找到找到结果的地方。在帖子中,在用户中还是在标签中?
问题: 我有 3 种类型的文档(帖子、用户、标签)和多图索引。当我使用 TransformResults 创建我的多图索引时,我得到的所有结果都是空值。(我通过全文搜索使用多图索引查询我的文档)。
我的文档
public class Post
{
public string Id { get; set; }
public long SqlDbId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string ContentAsHtml { get; set; }
public Status Status { get; set; }
public DenormalizedUser User { get; set; }
public DenormalizedTagCollection Tags { get; set; }
}
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
}
public class Tag
{
public string Id { get; set; }
public long SqlDbId { get; set; }
public string TagName { get; set; }
public DenormalizedUser TagInserterDenormalizedUser { get; set; }
public bool IsSystemTag { get; set; }
public Status Status { get; set; }
}
public class FullSearchIndex : AbstractMultiMapIndexCreationTask<FullSearchResult>
{
public FullSearchIndex()
{
AddMap<Post>(posts => from post in posts
let tags = post.Tags
where post.Status == Status.Active
select new
{
UserId = post.User != null ? post.User.Id.ToString() : (string)null,
PostId = post.Id,
TagIds = tags != null ? tags.Select(tag => tag.Id).ToArray() : new string[0],
SearchQuery = new object[]
{
post.Title,
post.Content,
post.Tags != null ? tags.Select(x => x.TagName).ToArray() : new string[0]
},
Source = SearchResultSource.ResultIsFromPost
});
AddMap<User>(users => from user in users
select new
{
UserId = user.Id,
PostId = (string)null,
TagIds = new string[0],
SearchQuery = new object[]
{
user.Name,
user.Surname
},
Source = SearchResultSource.ResultIsFromUser
});
AddMap<Tag>(tags => from tag in tags
where tag.Status == Status.Active
select new
{
UserId = (string)null,
PostId = (string)null,
TagIds = new string[] { tag.Id },
SearchQuery = new object[]
{
tag.TagName
},
Source = SearchResultSource.ResultIsFromTag
});
Index(searchResult => searchResult.SearchQuery, FieldIndexing.Analyzed);
TransformResults = (clientSideDatabase, results) =>
from result in results
let post = clientSideDatabase.Load<Post>(result.PostId)
let tags = clientSideDatabase.Load<Tag>(result.TagIds)
let user = clientSideDatabase.Load<User>(result.UserId)
select new
{
PostId = post != null ? post.Id : (string)null,
PostTitle = post != null ? post.Title : (string)null,
PostContent = post != null ? post.Content : (string)null,
PostTags = tags != null ? tags.Select(x => x.TagName).ToArray() : (string[])null,
UserId = user != null ? user.Id : (string)null,
UserName = user != null ? user.Name : (string)null,
UserSurname = user != null ? user.Surname : (string)null,
UserEmail = user != null ? user.Email : (string)null,
UserMobileNumber = user != null ? user.MobileNumber : (string)null
};
}
}
当我使用多图索引和 lucene 搜索进行查询时,我有 4 个结果。但是,所有值都为空
query = "Tag50";
session.Query<FullSearchResult, FullSearchIndex>()
.Search(resultItem => resultItem.SearchQuery, query)
.As<FullSearchResultViewModel>()
.ToList();