SQL Elastic Search with Nest Client 中等效的 IN 运算符

SQL IN Operator equivalent in Elastic Search with Nest Client

我在过滤试图匹配值列表的搜索时遇到问题。

这是我的代码

List<string> DepartmentIds = new List<string>(){"d1","d2","d3","d4"};

term = "*" + "akshay" + "*"

var searchResults = client.Search<Employee>(c => c
                          .Index(index)
                          .Type(type)
                          .Query(+q.Term(f => f.DeptId.ToLower(), DepartmentIds))   // Facing Issue in Matching list of Department IDs
                            && ((q.Wildcard(wc => wc.Field(f => f.FirstName).Value(term)))                        
                                 || (q.Wildcard(wc => wc.Field(f => f.LastName).Value(term)))                       
                                 ));

假设有 10 个部门,我想从部门 D1、D2、D3 和 D4 获取结果。我基本上应该看看索引文档中的部门 ID 是否与 4 个部门中的任何一个相匹配。

由于部门 ID 的数量及其值是动态的。我想基本上在 NEST 客户端的运算符中找到 sql 的等价物,以便我可以适当地过滤我的搜索。

您可以为此使用 Terms query

List<string> departmentIds = new List<string>() { "d1", "d2", "d3", "d4" };

var term = "*" + "akshay" + "*";
var index = "index";
var type = "type";

var searchResults = client.Search<Employee>(c => c
    .Index(index)
    .Type(type)
    .Query(q => 
        +q.Terms(t => t
            .Field(f => f.DeptId)
            .Terms<string>(departmentIds)
        ) && (
            q.Wildcard(wc => wc.Field(f => f.FirstName).Value(term)) || 
            q.Wildcard(wc => wc.Field(f => f.LastName).Value(term))
        )
    )
);

将产生以下查询

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "deptId": [
                    "d1",
                    "d2",
                    "d3",
                    "d4"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "wildcard": {
                  "firstName": {
                    "value": "*akshay*"
                  }
                }
              },
              {
                "wildcard": {
                  "lastName": {
                    "value": "*akshay*"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

请记住wildcard queries can be slow and it is recommended not to start them with the * wilcard

您可能想查看 completion suggesters 了解您正在做的事情