![](/img/trans.png)
[英]C# Nest, Elastic Search: update and add to a field that is a list
[英]How to apply "in list" operator with Nest (C#) library using the elastic search?
我有一个要查询的ElasticSearch服务器。 我想使用NEST库在 SQL 服务器中复制in
运算符的行为。
这是Product
class 的简短版本,我用它来搜索请求中Type
字段的位置request.Types
public class Product
{
[Text(Name = "types")]
public string[] Types { get; set; }
[Text(Name = "price")]
public double Price { get; set; }
// ...
}
这是我使用 Nest 库的查询请求
var properties = await client.SearchAsync<Product>(x =>
{
x = x.Query(q =>
{
if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
{
q.Range(r =>
{
if (request.MinPrice.HasValue)
{
r = r.Field(x => x.Price).GreaterThanOrEquals((double)request.MinPrice.Value);
}
if (request.MaxPrice.HasValue)
{
r = r.Field(x => x.Price).LessThanOrEquals((double)request.MaxPrice.Value);
}
return r;
});
}
if (request.Types != null && request.Types.Length > 0)
{
// Here I need to query the Type field where field in request.Types
}
return q;
});
return x;
});
正如您在上面的评论中看到的那样,我只想在request.Types
具有值并且服务器具有至少一种匹配类型时才添加“in 子句”。
如何添加in clause
条件?
我可以看到对要求的两种可能的解释:
查询具有与request.Types
相交的Types
的文档
术语查询可以实现这一点。 通常,术语查询以映射为keyword
类型的字段为目标,但Product.Types
被映射为text
类型。 您可能需要考虑将映射更改为 map 作为keyword
,或将 map 更改为text
和多字段keyword
。
将Product.Types
映射为keyword
类型,查询将是
q.Terms(t => t.Field(f => f.Types).Terms(request.Types) );
查询具有request.Types
中指定的所有确切Types
的文档
术语集查询可以实现这一点。 和以前一样, Types
字段应该映射为keyword
,然后查询将是
q.TermsSet(t => t.Field(f => f.Types).Terms(request.Types).MinimumShouldMatchScript(s => s.Source("params.request_types_len").Params(d => d.Add("request_types_len", request.Types.Length) ) ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.