我有一个索引:
public class FreeSearchIndex :
AbstractIndexCreationTask<Post, FreeSearchIndex.Result>
{
public FreeSearchIndex()
{
Map = posts => from post in posts
select new
{
Query = post.Tags.Concat(new[]
{
post.Title,
post.Body
}),
DatePosted = post.Date
};
Index(x => x.Query, FieldIndexing.Analyzed);
}
public class Result
{
public object[] Query { get; set; }
public DateTime DatePosted { get; set; }
}
}
并有方法:
public List<Post> Filter(string freeSearch)
{
IQueryable<Post> posts;
var posts = documentSession
.Query<FreeSearchIndex.Result, FreeSearchIndex>()
.Where(x => x.Query == (object)freeSearch)
.OrderByDescending(x => x.DatePosted)
.As<Post>();
return posts.ToList();
}
并进行单元测试:
[SetUp]
public void Setup()
{
store = new EmbeddableDocumentStore
{
RunInMemory = true
};
store.Initialize();
session = store.OpenSession();
IndexCreation.CreateIndexes(typeof(FreeSearchIndex).Assembly, store);
}
[Test]
public void GivenFreeSearchPhrase_Filter_ShouldOutputFilteredPostsByGivenPhrase()
{
session.Store(new Post { Body = "universal answer to everything is 42" });
session.SaveChanges();
var posts = Filter("everything");
Assert.AreEqual(1, posts.Count);
}
它失败了,因为查询返回 0 个帖子。我该如何解决这个问题?我应该检查生成的查询,是否可以检查它如何索引存储中的字段(在内存中)?