简体   繁体   English

如何使用弹性搜索将“in list”运算符与 Nest (C#) 库一起应用?

[英]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:我可以看到对要求的两种可能的解释:

  1. 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 beProduct.Types映射为keyword类型,查询将是

    q.Terms(t => t.Field(f => f.Types).Terms(request.Types) );
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM