elasticsearch 如何从Elasticsearch索引中的两个字段派生字段?,elasticsearch,curl,vega,elasticsearch,Curl,Vega" /> elasticsearch 如何从Elasticsearch索引中的两个字段派生字段?,elasticsearch,curl,vega,elasticsearch,Curl,Vega" />

elasticsearch 如何从Elasticsearch索引中的两个字段派生字段?

elasticsearch 如何从Elasticsearch索引中的两个字段派生字段?,elasticsearch,curl,vega,elasticsearch,Curl,Vega,我有一个包含字段的索引: 房间名称 开始时间\使用开始时间间隔 结束\使用日期结束时间间隔 我正在创建一个curl命令,在该命令中我可以获得房间使用的时间 可能吗 下面是当前的curl命令: curl -XGET "https://localhost:9200/testindex/_search?pretty" -H 'Content-Type: application/json' -d' { "aggs": { "room_bucket":{ "

我有一个包含字段的索引:

房间名称 开始时间\使用开始时间间隔 结束\使用日期结束时间间隔 我正在创建一个curl命令,在该命令中我可以获得房间使用的时间

可能吗

下面是当前的curl命令:

curl -XGET "https://localhost:9200/testindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "aggs": {
        "room_bucket":{
            "terms": {
                "field": "room_name.keyword",
            },
            "aggs":{
                "hour_bucket": {
                    "terms": {
                        "script": {
                            "inline": "def l = doc[\"start_date \"].value;\nif ( l <= 20 && l >= 9 ) {\n  return l;\n}",
                            "lang": "painless"
                        },
                        "order": {
                            "_key": "asc"
                     },
                     "value_type": "long"
                    }
                }
            }
        }
    }
}'
但我在聚合中的预期结果如下:

"aggregations" : {
    "room_bucket" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "room_V",
          "doc_count" : 1,
          "hour_bucket" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 11,
                "doc_count" : 1
              },
              {
                "key" : 12,
                "doc_count" : 1
              },
              {
                "key" : 13,
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "room_Y",
          "doc_count" : 1,
          "hour_bucket" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 10,
                "doc_count" : 2
              },
              {
                "key" : 11,
                "doc_count" : 2
              },
              {
                "key" : 12,
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
在当前结果中,它只读取开始日期


但是,在预期的输出中,Room_V的key=11、key=12、key=13每个key的doc_count应该是1,因为基于开始日期和结束日期,Room是从11到13使用的。

您可以通过利用LongStream并创建一个包含间隔内所有小时的数组来实现所需的功能,如下所示:

curl -XGET "https://localhost:9200/testindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "room_bucket": {
      "terms": {
        "field": "room_name.keyword"
      },
      "aggs": {
        "hour_bucket": {
          "terms": {
            "script": {
              "inline": """
              return LongStream.rangeClosed(doc.start_date.value, doc.end_date.value).toArray();

""",
              "lang": "painless"
            },
            "order": {
              "_key": "asc"
            },
            "value_type": "long"
          }
        }
      }
    }
  }
}'

目前还不清楚实际结果和预期结果之间存在什么问题。请多解释details@Val我在预期的结果之后添加了一个解释。哦,我明白了,你希望间隔时间内的每一个小时都是一个小时。有可能吗?是的,有可能,请看下面我的答案。酷,很高兴它有帮助!我还有最后一个问题。如果我只想要一个小时桶的特定范围,怎么样?bucket[]中的示例键值应仅为10-15。如果开始日期=9,结束日期=11,hour\u bucket.bucket[]将有键:10和键:11。您可以对这些值添加范围查询,对吗?可能吗?我认为会有冲突,因为返回的是数组。我认为这应该是另一个问题,因为这是一个不同的问题。请随意参考这篇文章的上下文。
curl -XGET "https://localhost:9200/testindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "room_bucket": {
      "terms": {
        "field": "room_name.keyword"
      },
      "aggs": {
        "hour_bucket": {
          "terms": {
            "script": {
              "inline": """
              return LongStream.rangeClosed(doc.start_date.value, doc.end_date.value).toArray();

""",
              "lang": "painless"
            },
            "order": {
              "_key": "asc"
            },
            "value_type": "long"
          }
        }
      }
    }
  }
}'