【问题标题】:Elasticsearch query to get distinct result with most recent date form one monthElasticsearch 查询以一个月的最近日期形式获得不同的结果
【发布时间】:2016-12-09 04:14:55
【问题描述】:

我对 ElasticSearch 很陌生。任何人都可以帮助我找到查询。 我的弹性搜索中有以下记录

Name    Work         Time Stamp
--------------------------------------------
Steve,  eating,     2016-11-12 05:36:40
Steve,  sleeping,   2016-11-12 06:14:50
Steve,  going,      2016-11-12 07:21:22
Steve,  driving,    2016-11-12 08:20:10
Steve,  reading,    2016-11-12 09:24:30
James,  eating,     2016-11-12 11:36:40
James,  sleeping,   2016-11-12 05:14:50
James,  going,      2016-11-12 08:21:22
James,  driving,    2016-11-12 10:20:10
James,  reading,    2016-11-12 09:24:30
Crag,   sleeping,   2016-05-12 09:24:30

我需要以下数据

Name    Work        Time Stamp
-------------------------------------------
Steve,  reading,    2016-11-12 09:24:30
James,  eating,     2016-11-12 11:36:40

【问题讨论】:

    标签: elasticsearch lucene kibana querydsl


    【解决方案1】:

    以正确的方式索引数据非常重要:

    1) 因为要按日期排序,所以要对elastic“说”特定字段是日期字段,所以需要对数据进行映射:

      PUT stack 
    {
      "mappings": {
        "stack": {
          "properties": {
            "time": {
                   "type": "date"
                }
          }
        }
      }
    }
    

    2) 然后插入数据:

        POST /_bulk
    {"index":{"_index":"stack","_type":"stack"}}
    {"name":"Steve","work":"eating","Time":"2016-11-12"}
    {"index":{"_index":"stack","_type":"stack"}}
    {"name":"Steve","work":"sleeping","Time":"2016-11-13"}
    {"index":{"_index":"stack","_type":"stack"}}
    {"name":"James","work":"eating","Time":"2016-11-12"}
    {"index":{"_index":"stack","_type":"stack"}}
    {"name":"James","work":"sleeping","Time":"2016-05-15"}
    

    3)然后搜索:

    GET stack/_search
    {
        "size": 0, 
        "aggs": {
            "top-tags": {
                "terms": {
                    "field": "name",
                    "size": 3
                },
                "aggs": {
                    "top_tag_hits": {
                        "top_hits": {
                            "sort": [
                              { "Time":   { "order": "desc" }}
                            ],
                            "size" : 2
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2013-09-14
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      相关资源
      最近更新 更多