如何将布尔和布尔人或Elasticsearch查询结合使用?

发布于 2025-02-10 17:44:36 字数 1282 浏览 3 评论 0原文

查询:获取ID为“ 200”的员工名称“ Mahesh”,并且加入DateTime在给定的日期范围内,其EPF状态必须为'Nok'或'WRN'。 (epf_status的可能值是{ok,nok,wrn,取消}。

我写了以下查询,将EPF_STATUS匹配也与OK相匹配,但仅在EPF_STATUS是'NOK'或'nok'或'WRN'时才匹配。什么。什么。什么。什么。什么。什么。什么。 需要更改以使其正常工作吗?

GET myindex01/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "empname": { "query": "Mahesh", "operator": "AND" }
          }
        },
        {
          "match": {
            "empid": { "query": "200", "operator": "AND" }
          }
        },
        {
          "range": {
            "joining_datetime": {
              "gte": "2020-01-01T00:00:00",
              "lte": "2022-06-24T23:59:59"
            }
          }
        }
      ],
      "should": [
        { "match": { "epf_status": "NOK" } },
        { "match": { "epf_status": "WRN" } }
      ]
    }
  }
}

否则,

{"Mahesh","200","2022-04-01","OK"}
{"Mahesh","200","2022-04-01","NOK"}
{"Mahesh","200","2022-04-01","WRN"}
{"Mahesh","200","2022-04-01","CANCELLED"}

{"Mahesh","200","2022-04-01","NOK"}
{"Mahesh","200","2022-04-01","WRN"}

Query: Get employee name "Mahesh" whose id is "200" and joining datetime is in a given date range and his epf status must be either 'NOK' or 'WRN'. (Possible values of epf_status are {OK,NOK,WRN,CANCELLED}.

I have written the following query, that matches epf_status also with OK, CANCELLED, but it must only match when epf_status is either 'NOK' or 'WRN'. What else do I need to change to make it work, as required?

GET myindex01/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "empname": { "query": "Mahesh", "operator": "AND" }
          }
        },
        {
          "match": {
            "empid": { "query": "200", "operator": "AND" }
          }
        },
        {
          "range": {
            "joining_datetime": {
              "gte": "2020-01-01T00:00:00",
              "lte": "2022-06-24T23:59:59"
            }
          }
        }
      ],
      "should": [
        { "match": { "epf_status": "NOK" } },
        { "match": { "epf_status": "WRN" } }
      ]
    }
  }
}

SAMPLE DATA:

{"Mahesh","200","2022-04-01","OK"}
{"Mahesh","200","2022-04-01","NOK"}
{"Mahesh","200","2022-04-01","WRN"}
{"Mahesh","200","2022-04-01","CANCELLED"}

REQUIRED OUTPUT:

{"Mahesh","200","2022-04-01","NOK"}
{"Mahesh","200","2022-04-01","WRN"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-02-17 17:44:36

tldr;

您可以使用查询。

返回在提供字段中包含一个或多个确切条款的文档。

解决

GET myindex01/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "empname": { "query": "Mahesh", "operator": "AND" }
          }
        },
        {
          "match": {
            "empid": { "query": "200", "operator": "AND" }
          }
        },
        {
          "range": {
            "joining_datetime": {
              "gte": "2020-01-01T00:00:00",
              "lte": "2022-06-24T23:59:59"
            }
          }
        }
      ],
      "should": [
        { "terms": { "epf_status": ["NOK", "WRN"] } }
      ]
    }
  }
}

Tldr;

You could be using the terms query for that I believe.

Returns documents that contain one or more exact terms in a provided field.

To solve

GET myindex01/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "empname": { "query": "Mahesh", "operator": "AND" }
          }
        },
        {
          "match": {
            "empid": { "query": "200", "operator": "AND" }
          }
        },
        {
          "range": {
            "joining_datetime": {
              "gte": "2020-01-01T00:00:00",
              "lte": "2022-06-24T23:59:59"
            }
          }
        }
      ],
      "should": [
        { "terms": { "epf_status": ["NOK", "WRN"] } }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文