【问题标题】:How to join queries with AND in elasticsearch?如何在弹性搜索中使用 AND 连接查询?
【发布时间】:2021-06-24 06:31:29
【问题描述】:

我有这个映射的索引:

{
  "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”和类型 = 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 中,您没有返回术语查询。
  • 这太愚蠢了……谢谢

标签: c# elasticsearch nest


【解决方案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;
    }

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多