![](/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.