首页 > 解决方案 > 多个和查询与子请求。弹性搜索

问题描述

当我有以下查询时,出现错误

{
  "query": {
    "bool": {
      "must": [
        { "match": { "context.branch": "17270" }},
        { "match": { "context.version": "12.0.R2" }},
        { "match": { "run_uid": "AAA / SSS / AA-ContextMenu / 3507031CR3.exe"}},
        {
          "range": {
            "epoch": {
              "lte": 1510340778.78
            }
          }
        }
      ]
    }
  }
}

映射:

{
                "mappings": {
                    "_doc": {
                        "properties": {
                            "partition_id": {"type": "integer"},
                            "report_id": {"type": "integer"},
                            "title": {"type": "text"},
                            "epoch": {"type": "date",
                                      "format": "epoch_millis"},
                            "context": {
                                "dynamic": True,
                                "properties": {}
                            },
                            "run_uid": {"type": "text"},
                            "run_id": {"type": "integer"},
                            "build_name": {"type": "text"},
                            "status_id": {"type": "integer"},
                            "rep_custom_id": {"type": "text"},
                            "created": {
                                "type": "date",
                                "format": "strict_date_optional_time||epoch_millis"
                            }
                        }
                    }
                }
            }

你能告诉我为什么我仍然看到不同的run_uid吗?以下选项有效:

    { "match": { "context.branch": "17270" }},
    { "match": { "context.version": "12.0.R2" }}

但是 { "match": { "run_uid": "AAA / SSS / AA-ContextMenu / 3507031CR3.exe"} 不起作用。它返回我随机 run_uids 但正确的“context.branch”和“context.version”

例如,我有以下数据:

run_id context      created

4      blabala132   2019-03-20 08:00:00.0000+0300
5      blabala132   2019-03-20 07:00:00.0000+0300
6      blabala132   2019-03-20 06:00:00.0000+0300
7      blabala132   2019-03-20 05:00:00.0000+0300
8      blabala132   2019-03-20 09:00:00.0000+0300
9      blabala133   2019-03-20 07:00:00.0000+0300

输入数据:run_id 4

结果如下:

5      blabala132   2019-03-20 07:00:00.0000+0300
6      blabala132   2019-03-20 06:00:00.0000+0300
7      blabala132   2019-03-20 05:00:00.0000+0300

每个数据小于2019-03-20 08:00:00.0000+0300(属于run_id 4),上下文相同。

8      blabala132   2019-03-20 09:00:00.0000+0300

上述字符串不在结果中,因为数据为 gt 2019-03-20 08:00:00.0000+0300(属于 run_id 4)

9      blabala133   2019-03-20 07:00:00.0000+0300

上述字符串不在结果中,因为上下文与 blabala132 不同(属于 run_id 4)

标签: elasticsearch

解决方案


首先,您的 JSON 结构不正确。之后,您的查询语法也不正确。它应该是这样的:

 {
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "run_id": 5000
          }
        },
        {
          "term": {
            "context": "{\"branch,keyword\": \"TRT-26878\",\"version\": \"12.0.R2\"}"
          }
        },
        {
          "range": {
            "created.keyword": {
              "lte": 1510047258.826,
              "boost": 2
            }
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1
    }
  }
}

推荐阅读