[英]How to apply "in list" operator with Nest (C#) library using the elastic search?
I have an ElasticSearch server that I want to query.我有一个要查询的ElasticSearch服务器。 I want to replicate the behavior of
in
operator in a SQL server using the NEST library.我想使用NEST库在 SQL 服务器中复制
in
运算符的行为。
Here is a short version of the Product
class which is what I am using to search where the Type
field is in request.Types
这是
Product
class 的简短版本,我用它来搜索请求中Type
字段的位置request.Types
public class Product
{
[Text(Name = "types")]
public string[] Types { get; set; }
[Text(Name = "price")]
public double Price { get; set; }
// ...
}
Here is my query request using the Nest library这是我使用 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;
});
As you can see in the comment above, I want to add the "in clause" only when the request.Types
has value and the server has at least one matching type.正如您在上面的评论中看到的那样,我只想在
request.Types
具有值并且服务器具有至少一种匹配类型时才添加“in 子句”。
How can I add in clause
condition?如何添加
in clause
条件?
I can see two possible interpretations of the requirement:我可以看到对要求的两种可能的解释:
query documents that have Types
that intersect with request.Types
查询具有与
request.Types
相交的Types
的文档
A terms query can achieve this. 术语查询可以实现这一点。 Typically, a terms query targets a field mapped as a
keyword
type, but Product.Types
is mapped as a text
type.通常,术语查询以映射为
keyword
类型的字段为目标,但Product.Types
被映射为text
类型。 You may want to consider changing the mapping to map as keyword
, or map it as both text
and keyword
with a multi-field.您可能需要考虑将映射更改为 map 作为
keyword
,或将 map 更改为text
和多字段keyword
。
With Product.Types
mapped as a keyword
type, the query would be将
Product.Types
映射为keyword
类型,查询将是
q.Terms(t => t.Field(f => f.Types).Terms(request.Types) );
query documents that have all the exact Types
specified in request.Types
查询具有
request.Types
中指定的所有确切Types
的文档
A terms set query can achieve this. 术语集查询可以实现这一点。 As before, the
Types
field should be mapped as a keyword
, then the query would be和以前一样,
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.