簡體   English   中英

如何使用彈性搜索將“in list”運算符與 Nest (C#) 庫一起應用?

[英]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條件?

我可以看到對要求的兩種可能的解釋:

  1. 查詢具有與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) );
  2. 查詢具有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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM