如何在 elasticsearch 中使用 AND 加入查询?

How to join queries with AND in elasticsearch?

我有这个映射的索引:

{
  "SampleIndex" : {
    "mappings" : {
      "properties" : {
        "$type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "data" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        
        "id" : {
          "type" : "keyword"
        },
        "isActive" : {
          "type" : "boolean"
        },
        "isDeleted" : {
          "type" : "boolean"
        },
        "ownerSSN" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "ownerId" : {
          "type" : "keyword"
        },
        "type" : {
          "type" : "integer"
        }
      }
    }
  }

现在我想创建这个查询:

select * From SampleIndex where ownerSSN = "someSNN" and ownerId = "someOwnerId" and type = 1

我尝试使用 NEST 通过此代码生成我的查询:

var result = await ElasticClient.SearchAsync<BankAccountReadModel>(
                    s => s
                    .Index(IndexName)
                    .Query(q =>
                                q.ByOwnerId(personageId) &&
                                q.ByOwnerSSN(nationalCode) &&
                                q.ByType(type)
                   ));

            return result.Hits.Select(x => x.Source).ToList(); 

下面列出了我的扩展方法:

 internal static class BankAccountFilterExtention {
        public static QueryContainer ByOwnerId(
            this QueryContainerDescriptor<BankAccountReadModel> search
            , Guid? id) {
            if (id.HasValue)
                return search.Term(x =>
                x.PersonageId, id.Value.ToString());
            return null; 
        }

        public static QueryContainer ByOwnerSSN(
            this QueryContainerDescriptor<BankAccountReadModel> search
            , string nationalId)
        {
            if (!string.IsNullOrEmpty(nationalId))
                return search.Term(x =>
                    x.OwnerNationalCode,nationalId
                );
            return null;
        }

        public static QueryContainer ByType(
            this QueryContainerDescriptor<BankAccountReadModel> search,
            BankAccountType? type)
        {
            if (type.HasValue)
            {
                search.Term(b=> b.Type,type.Value);
            }
            return null;
        }
    }

我希望当我 运行 通过“SomeSSN”和 type = 1 进行查询时,它只会检索与这两个值都匹配的记录。但我得到的每条记录都有“someSSN”或类型 =1.

如何更正我的查询?

ByType 中,您没有返回术语查询

    public static QueryContainer ByType(
        this QueryContainerDescriptor<BankAccountReadModel> search,
        BankAccountType? type)
    {
        if (type.HasValue)
        {
            return search.Term(b=> b.Type,type.Value);    <--- change this
        }
        return null;
    }