仅导入与Elastic Search中的条件匹配的数组

时间:2019-09-18 01:34:38

标签: elasticsearch-painless

我正在处理ElasticSearch中的嵌套嵌套数据。

我希望它像RDBMS中的SELECT *一样工作。

如果您有以下数据

POST test-stack/test/1234_5678
{
  "Id" : 1234,
  "availables":
  [
    {
      "Id" : 4444,
      "date" : "2019-09-10",
      "time" : [
        {
          "dateTime" : "2019-09-10T09:30:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-10T10:00:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-10T10:30:00+09:00",
          "Count" : 50
        }
      ]
    },
    {
      "Id" : 5555,
      "date" : "2019-09-11",
      "time" : [
        {
          "dateTime" : "2019-09-11T09:30:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-11T10:00:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-11T10:30:00+09:00",
          "Count" : 50
        }
      ]
    },
    {
      "Id" : 6666,
      "date" : "2019-09-12",
      "time" : [
        {
          "dateTime" : "2019-09-12T09:30:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-12T10:00:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-12T10:30:00+09:00",
          "Count" : 50
        }
      ]
    }
  ]
}

如果我这样做,

从测试t中选择*,其中t.availables.date =='2019-09-10';

所以,我想得到这个答案,

      "Id" : 4444,
      "date" : "2019-09-10",
      "time" : [
        {
          "dateTime" : "2019-09-10T09:30:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-10T10:00:00+09:00",
          "Count" : 50
        },
        {
          "dateTime" : "2019-09-10T10:30:00+09:00",
          "Count" : 50
        }
      ]
    }

我是Elastic Search的初学者,我想知道在Elastic Search中是否有可能。

我已经学习了轻松的脚本,但仍然不知道。

1 个答案:

答案 0 :(得分:1)

您需要使用nested queryinner hits

嵌套查询将帮助您过滤嵌套字段,内部匹配将返回匹配的嵌套文档

映射:

PUT testindex11/_mapping
{
  "properties": {
    "Id": {
      "type": "text"
    },
    "availables": {
      "type": "nested",
      "properties": {
        "Id": {
          "type": "text"
        },
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "time":{
          "type": "nested",
           "properties": {
             "dateTime" :{
               "type":"date",
               "format":"yyyy-MM-dd'T'HH:mm:ss"
             },
             "count":{
               "type":"integer"
             }
           }
        }
      }
    }
  }
}

查询:

GET testindex11/_search
{
  "query": {
    "nested": {
      "path": "availables",
      "query": {
        "term": {
          "availables.date": {
            "value": "2019-09-10"
          }
        }
      },
       "inner_hits": {} 
    }
  }
}

结果:

 [
      {
        "_index" : "testindex11",
        "_type" : "_doc",
        "_id" : "PXuHQm0B4boMRQnoJOpR",
        "_score" : 1.0,
        "_source" : {
          "Id" : 1234,
          "availables" : [
            {
              "Id" : 4444,
              "date" : "2019-09-10",
              "time" : [
                {
                  "dateTime" : "2019-09-10T09:30:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-10T10:00:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-10T10:30:00",
                  "Count" : 50
                }
              ]
            },
            {
              "Id" : 5555,
              "date" : "2019-09-11",
              "time" : [
                {
                  "dateTime" : "2019-09-11T09:30:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-11T10:00:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-11T10:30:00",
                  "Count" : 50
                }
              ]
            },
            {
              "Id" : 6666,
              "date" : "2019-09-12",
              "time" : [
                {
                  "dateTime" : "2019-09-12T09:30:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-12T10:00:00",
                  "Count" : 50
                },
                {
                  "dateTime" : "2019-09-12T10:30:00",
                  "Count" : 50
                }
              ]
            }
          ]
        },
        "inner_hits" : {
          "availables" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "testindex11",
                  "_type" : "_doc",
                  "_id" : "PXuHQm0B4boMRQnoJOpR",
                  "_nested" : {
                    "field" : "availables",
                    "offset" : 0
                  },
                  "_score" : 1.0,
                  "_source" : {
                    "Id" : 4444,
                    "date" : "2019-09-10",
                    "time" : [
                      {
                        "dateTime" : "2019-09-10T09:30:00",
                        "Count" : 50
                      },
                      {
                        "dateTime" : "2019-09-10T10:00:00",
                        "Count" : 50
                      },
                      {
                        "dateTime" : "2019-09-10T10:30:00",
                        "Count" : 50
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    ]