【问题标题】:Using Elastic Search Geo Functionality To Find Most Common Locations Ordered By Time使用弹性搜索地理功能查找按时间排序的最常见位置
【发布时间】:2016-05-23 01:11:47
【问题描述】:

我目前有一个 ES 查询,它使用 geohash_griddate_histogram 为我提供了“地理存储桶”列表:

  "aggregations": {
"zoomedInView": {
  "filter": {
    "geo_bounding_box": {
      "location": {
        "top_left": "-37, 140",
        "bottom_right": "-38, 146"
      }
    }
  },
  "aggregations": {
    "zoom1": {
      "geohash_grid": {
        "field": "location",
        "precision": 6
      },
      "aggs": {
        "ts": {
          "date_histogram": {
            "min_doc_count" : 1,
            "field": "dateTime",
            "interval": "1m",
            "format": "DDD HH:mm"
          }
         },
         "map_zoom": { 
            "geo_bounds": {
                "field": "location"
            }
        }
      }
    }
  }
}

这给我的结果看起来像:

{
              "key": "r1r0fu",
              "map_zoom": {
                 "bounds": {
                    "top_left": {
                       "lat": -38.81073913909495,
                       "lon": 124.96536672115326
                    },
                    "bottom_right": {
                       "lat": -38.81329075805843,
                       "lon": 124.96823584660888
                    }
                 }
              },
              "ts": {
                 "buckets": [
                    {
                       "key_as_string": "136 20:15",
                       "key": 1463354100000,
                    },                       
                    {
                       "key_as_string": "137 04:30",
                       "key": 1463365800000,
                       "doc_count": 1
                    },
....

{
              "key": "r1r0gx",
              "map_zoom": {
                 "bounds": {
                    "top_left": {
                       "lat": -38.798130828887224,
                       "lon": 124.99871227890253
                    },
                    "bottom_right": {
                       "lat": -38.79820383526385,
                       "lon": 124.99872468411922
                    }
                 }
              },
              "ts": {
                 "buckets": [
                    {
                       "key_as_string": "136 23:21",
                       "key": 1463354460000,
                    },
                    {
                       "key_as_string": "137 02:30",
                       "key": 1463365800000,
                    },
                    {
                       "key_as_string": "137 03:31",
                       "key": 1463369460000,
                    }
                 ]
              }
           },

在上面的示例中,结果按地理存储桶r1r0fur1r0gx 排序,存储桶内按事件的排序时间(格式为一年中的日期 HHH:mm)及其计数。

我真正想要的是:

1)按时间排序的结果,可能意味着同一个桶会出现多次。

2) 仅显示每个存储桶内的最短和最长时间(如果可能)

所以上面的结果理想情况下应该是这样的:

                {
              "key": "r1r0fu",
              "map_zoom": {
                 "bounds": {
                    "top_left": {
                       "lat": -38.81073913909495,
                       "lon": 124.96536672115326
                    },
                    "bottom_right": {
                       "lat": -38.81329075805843,
                       "lon": 124.96823584660888
                    }
                 }
              },
              "ts": {
                 "buckets": [
                    {
                       "key_as_string": "136 20:15",
                       "key": 1463354100000,
                    },
                ]
              }
            },
            {
              "key": "r1r0gx",
              "map_zoom": {
                 "bounds": {
                    "top_left": {
                       "lat": -38.798130828887224,
                       "lon": 124.99871227890253
                    },
                    "bottom_right": {
                       "lat": -38.79820383526385,
                       "lon": 124.99872468411922
                    }
                 }
              },
              "ts": {
                 "buckets": [
                    {
                       "key_as_string": "136 23:21",
                       "key": 1463354460000,
                    },                
                    {
                       "key_as_string": "137 03:31",
                       "key": 1463369460000,
                    },  
                }
            },
            {
              "key": "r1r0fu",
              "map_zoom": {
                 "bounds": {
                    "top_left": {
                       "lat": -38.81073913909495,
                       "lon": 124.96536672115326
                    },
                    "bottom_right": {
                       "lat": -38.81329075805843,
                       "lon": 124.96823584660888
                    }
                 }
              },
              "ts": {
                 "buckets": [
                    {
                       "key_as_string": "137 04:30",
                       "key": 1463365800000,
                    }
                ]
              }
            },
            ...

结果按时间排序,因此桶r1r0fu 在这种情况下出现两次。并且事件"key_as_string": "137 02:30", 已被隐藏,因为它不是最小或最大日期。

这可能吗?

非常感谢!

【问题讨论】:

    标签: elasticsearch geolocation


    【解决方案1】:

    如果您想要按时间排序的结果,最好将date_histogram 聚合与geohash_grid 交换,如下所示:

    {
      "aggregations": {
        "zoomedInView": {
          "filter": {
            "geo_bounding_box": {
              "location": {
                "top_left": "-37, 140",
                "bottom_right": "-38, 146"
              }
            }
          },
          "aggregations": {
            "ts": {
              "date_histogram": {
                "min_doc_count": 1,
                "field": "dateTime",
                "interval": "1m",
                "format": "DDD HH:mm"
              },
              "aggs": {
                "zoom1": {
                  "geohash_grid": {
                    "field": "location",
                    "precision": 6
                  }
                },
                "map_zoom": {
                  "geo_bounds": {
                    "field": "location"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    这将解决问题 1)。但是,由于现在每个主存储桶都将是一个时间存储桶,因此您将无法再拥有最小和最大时间。试试看它是否适合您的需求。

    【讨论】:

    • 嗨,瓦尔!感谢您的快速回复。是的,效果很好!现在,相同的地理哈希可能会在最小和最大时间之间的所有时间连续输出几次。如果可以减少返回的数据量并横向影响消费者,我只需要每个地理哈希的最小和最大时间?
    • 您可以按 geohash+date 存储桶(在这种情况下,您不能按日期对 geohash 存储桶进行排序)或按 date+geohash(在这种情况下您无法获得每个 geohash 的最小/最大日期),但是不要混合/匹配两者。您可能需要使用初始解决方案并在客户端进行排序。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2017-09-22
    相关资源
    最近更新 更多