我正在尝试将 elasticsearch 实现到我的网店中,但在使用过滤器时遇到了一些麻烦。过滤是动态完成的。
示例:
我首先展示所有已编入索引的产品。所以没有应用过滤器。访问者可以选择自己的过滤器,例如:颜色、尺寸、品牌、类型、类别......
但我现在不知道如何使用 elasticsearch 和 NEST 构建搜索结果。
这是我没有过滤的解决方案:
var query = ElasticClient.Search<Product>(s => s
.From(from)
.Size(size)
);
我还有一个关于索引集合<> 或列表<> 的问题。我不得不在这些集合上使用 JsonIgnore。我也可以为它们编制索引吗?
这是我的课:
/// <summary>
/// Represents a product
/// </summary>
public partial class Product {
private ICollection<ProductCategory> _productCategories;
private ICollection<ProductManufacturer> _productManufacturers;
private ICollection<ProductPicture> _productPictures;
/// <summary>
/// Gets or sets the name
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// Gets or sets the short description
/// </summary>
public virtual string ShortDescription { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the entity is published
/// </summary>
public virtual bool Published { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the entity has been deleted
/// </summary>
public virtual bool Deleted { get; set; }
/// <summary>
/// Gets or sets the date and time of product creation
/// </summary>
public virtual DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the date and time of product update
/// </summary>
public virtual DateTime UpdatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the collection of ProductCategory
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductCategory> ProductCategories
{
get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
protected set { _productCategories = value; }
}
/// <summary>
/// Gets or sets the collection of ProductManufacturer
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductManufacturer> ProductManufacturers
{
get { return _productManufacturers ?? (_productManufacturers = new List<ProductManufacturer>()); }
protected set { _productManufacturers = value; }
}
/// <summary>
/// Gets or sets the collection of ProductPicture
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductPicture> ProductPictures
{
get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); }
protected set { _productPictures = value; }
}
}
有没有人可以帮助我?
最佳答案
请务必在此处阅读有关编写查询的整个文档:http://nest.azurewebsites.net/nest/writing-queries.html
下面是从那里粘贴的摘录。
无条件查询
编写复杂的 bool 查询是一回事,但更常见的情况是您不想根据用户输入来决定如何进行查询。
public class UserInput
{
public string Name { get; set; }
public string FirstName { get; set; }
public int? LOC { get; set; }
}
然后
.Query(q=> {
QueryDescriptor<ElasticSearch> query = null;
if (!string.IsNullOrEmpty(userInput.Name))
query &= q.Term(p=>p.Name, userInput.Name);
if (!string.IsNullOrEmpty(userInput.FirstName))
query &= q
.Term("followers.firstName", userInput.FirstName);
if (userInput.LOC.HasValue)
query &= q.Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc.Value))
return query;
})
这很快又变得乏味和冗长。因此 nest 允许您将之前的查询写成:
.Query(q=>
q.Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc))
)
如果任何查询会导致空查询,它们将不会被发送到 elasticsearch。
因此,如果除 userInput.Loc 之外的 userInput 上的所有项均为 null(或空字符串),它甚至不会将范围查询包装在 bool 查询中,而只会发出普通范围查询。
如果它们全部为空,将导致 match_all 查询。
这种无条件行为默认开启,但可以像这样关闭:
var result = client.Search<ElasticSearchProject>(s=>s
.From(0)
.Size(10)
.Strict() //disable conditionlessqueries by default
///EXAMPLE HERE
);
但是查询本身可以选择重新加入或退出。
.Query(q=>
q.Strict().Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Strict(false).Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc))
)
在此示例中,如果 userInput.Name 为 null 或为空,则会导致 DslException。无论 SearchDescriptor 是否使用 .Strict() ,范围查询都将使用无条件逻辑。
另外值得注意的是无条件查询逻辑传播:
q.Strict().Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Filtered(fq => fq
.Query(qff =>
qff.Terms(p => p.Country, userInput.Countries)
&& qff.Terms(p => p.Loc, userInput.Loc)
)
)
如果 userInput.Countries 和 userInput.Loc 均为 null 或为空,则不会发出整个过滤查询。
关于c# - NEST C# - elasticsearch - 电子商务过滤器组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15767656/