代码之家  ›  专栏  ›  技术社区  ›  nad

如何在时间戳中获取Elasticsearch查询结果时间

  •  0
  • nad  · 技术社区  · 3 年前

    当我对Elasticsearch(通过Kibana开发工具)进行如下查询时

    GET _search
    {
      "query": {
        "match": {
          "node_id": "Abc"
        }
      }
    }
    

    我得到了下面的结果样本。结果是 ts 钥匙在里面 date 格式,而不是时间戳格式。如何编写查询,以便 ts 字段是否在时间戳中?有没有办法指定格式?

    "_source" : {
              "organization_eid" : "Ga2",
              "node_id" : "Abc",
              "ts" : "2021-02-27T00:18:39.75226593Z
    }
    

    更新 :

    我补充道 docvalue_fields 在上面 _source 在查询中。我的整个问题如下:

    GET _search
    {
      "version": true,
      "size": 500,
      "sort": [
        {
          "ts": {
            "order": "desc",
            "unmapped_type": "boolean"
          }
        }
      ],
      "docvalue_fields": [
        {
          "field": "ts",
          "format": "epoch_millis"
        }
      ],
      "_source": {
        "excludes": []
      },
      "aggs": {
        "2": {
          "date_histogram": {
            "field": "ts",
            "fixed_interval": "10m",
            "time_zone": "America/Los_Angeles",
            "min_doc_count": 1
          }
        }
      },
      "stored_fields": [
        "*"
      ],
      "script_fields": {},
      "query": {
        "bool": {
          "must": [],
          "filter": [
            {
              "match_all": {}
            },
            {
              "range": {
                "ts": {
                  "format": "strict_date_optional_time",
                  "gte": "2021-03-03T00:30:00.000Z",
                  "lte": "2021-03-03T12:00:00.000Z"
                }
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      },
      "highlight": {
        "pre_tags": [
          "@kibana-highlighted-field@"
        ],
        "post_tags": [
          "@/kibana-highlighted-field@"
        ],
        "fields": {
          "*": {}
        },
        "fragment_size": 2147483647
      }
    }
    

    现在在输出中,我看到了下面的内容(仅显示 hits 部分)

    "hits" : [
          {
            "_index" : "url.0.2_6_18689",
            "_type" : "_doc",
            "_id" : "...",
            "_version" : 1,
            "_score" : null,
            "_source" : {
              "organization_eid" : "Ga2",
              "node_id" : "Abc",
              "ts" : "2021-03-03T11:59:30.705142021Z",
              "destination" : "....",
            "fields" : {
              "ts" : [
                "1614772770705"
              ]
    

    如上所述,内部 fields 这个 ts 已转换为 timestamp 但在里面 _来源 这个 ts 还在 datetime .

    我错过了什么?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Val    3 年前

    你能做的就是利用 doc values fields 为了检索原始时间戳值:

    GET _search
    {
      "docvalue_fields": [ "ts" ],                    <---- add this
      "_source": ["organization_eid", "node_id"],
      "query": {
        "match": {
          "node_id": "Abc"
        }
      }
    }