RavenDB:不能让使用SimpleAnalyzer的字段的多术语facet返回原始值
本文关键字:术语 facet 返回 原始 字段 RavenDB 不能 SimpleAnalyzer | 更新日期: 2023-09-27 18:05:22
我使用ravendb2客户端。
我希望我的用户能够按类别和返回产品搜索。对于类别名称为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);
,但在使用facetes时没有成功。
所以我的问题是,我如何让facet在使用SimpleAnalyzer
的字段上返回high heels
而不是high
和heels
?
我的代码如下:
我指数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");
}
感谢Ayende Rahien的建议。
原来你不能用我尝试的方法来做上面的事情。
在索引中必须有两个字段,一个用于索引,另一个用于存储原始值。因此,我将索引更改为:
public class ProductIndex : AbstractIndexCreationTask<Product,ProductIndex.ProductIndexItem>
{
public class ProductIndexItem
{
public string Name { get; set; }
public string CategoryName { get; set; }
public string CategoryNameAnalyzed { get; set; }
}
public ProductIndex()
{
Map = products => from product in products
from category in product.Categories
select new
{
product.Name,
CategoryName = category.Name,
CategoryNameAnalyzed = category.Name,
};
Stores.Add(x => x.CategoryName, FieldStorage.Yes);
Analyzers.Add(x => x.CategoryNameAnalyzed, "SimpleAnalyzer");
}
}
和我的查询改为:
var query = _session.Advanced.LuceneQuery<Product>("ProductIndex")
.WaitForNonStaleResults()
.Search("CategoryNameAnalyzed", "heels");
var products = query.ToList();
var facets = query.ToFacets("facets/ProdctFacets");
我希望这能帮助到别人。