多个并使用子请求进行查询。弹性搜索

时间:2019-03-19 16:07:02

标签: elasticsearch

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

{
  "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)

1 个答案:

答案 0 :(得分:1)

首先,您的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
    }
  }
}