我正在使用 RavenDB 2 客户端。
我希望我的用户能够按类别搜索并返回产品。对于类别名称,High Heels
我希望用户能够搜索heels
并获取具有类别的产品High Heels
。
我在索引中设置我的CategoryName
字段如下:
Analyzers.Add(x => x.CategoryName, "SimpleAnalyzer");
当我想展示CategoryName
. 它不是返回high heels
命中 1,而是返回high
命中 1 和heels
命中 1。
我理解它为什么这样做,并且我尝试过使用:
Stores.Add(x => x.CategoryName, FieldStorage.Yes);
但是将它与刻面一起使用时没有成功。
所以我的问题是,如何让方面返回high heels
而不是在使用high
的heels
字段上SimpleAnalyzer
?
我的代码如下:
我的索引
public class ProductIndex : AbstractIndexCreationTask<Product,ProductIndex.ProductIndexItem>
{
public class ProductIndexItem
{
public string Name { get; set; }
public string CategoryName { get; set; }
}
public ProductIndex()
{
Map = products => from product in products
from category in product.Categories
select new
{
product.Name,
CategoryName = category.Name
};
Stores.Add(x => x.CategoryName, FieldStorage.Yes);
Analyzers.Add(x => x.CategoryName, "SimpleAnalyzer");
}
}
我的测试
[Test]
public void MultiTermCategoryTest()
{
var product = new Product
{
Name = "MyProductName",
Categories = new List<Category>
{
new Category
{
Name = "High Heels",
}
}
};
_session.Store(product);
_session.SaveChanges();
var query = _session.Advanced.LuceneQuery<Product>("ProductIndex")
.WaitForNonStaleResults()
.Search("CategoryName", "heels");
var products = query.ToList();
var facets = query.SelectFields<Facet>("CategoryName").ToFacets("facets/ProdctFacets");
// Check that product has been returned
Assert.That(products.Count, Is.EqualTo(1), "Product count is incorrect.");
// Check that facet has been returned
Assert.That(facets.Results.Count, Is.EqualTo(1), "Facet Results count is incorrect");
var facetResult = facets.Results.FirstOrDefault();
// Check that factes are what I want
Assert.That(facetResult.Key, Is.EqualTo("CategoryName"));
// ** Fails here returning a count of 2**
Assert.That(facetResult.Value.Values.Count, Is.EqualTo(1), "Facet.Value.Values count is incorrect");
}