程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用elasticsearch地理功能查找最常见的位置?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用elasticsearch地理功能查找最常见的位置??

开发过程中遇到使用elasticsearch地理功能查找最常见的位置?的问题如何解决?下面主要结合日常开发的经验,给出你关于使用elasticsearch地理功能查找最常见的位置?的解决方法建议,希望对你解决使用elasticsearch地理功能查找最常见的位置?有所启发或帮助;

为了使其工作,您需要像这样修改映射:

@H_262_5@PUT /LOCATIOns
{
  "mapPings": {
    "LOCATIOn": {
      "propertIEs": {
        "LOCATIOn": {
          "type": "geo_point"
        },
        "timestampMs": {
          "type": "long"
        },
        "accuracy": {
          "type": "long"
        }
      }
    }
  }
}

然后,当您为文档建立索引时,需要将纬度和经度除以10000000,然后像这样进行索引:

@H_262_5@PUT /LOCATIOns/LOCATIOn/1
{
  "timestampMs": "1461820561530",
  "LOCATIOn": {
    "lat": -37.8103308,
    "lon": 14.4967407
  },
  "accuracy": 35
}

最后,您的搜索查询如下…

@H_262_5@POST /LOCATIOns/LOCATIOn/_search
{
  "aggregations": {
    "zoomedInVIEw": {
      "filter": {
        "geo_bounding_Box": {
          "LOCATIOn": {
            "top_left": "-37, 14",
            "bottom_right": "-38, 15"
          }
        }
      },
      "aggregations": {
        "zoom1": {
          "geohash_grID": {
            "fIEld": "LOCATIOn",
            "precision": 6
          },
          "aggs": {
            "ts": {
              "date_histogram": {
                "fIEld": "timestampMs",
                "interval": "15m",
                "format": "DDD yyyy-MM-dd HH:mm"
              }
            }
          }
        }
      }
    }
  }
}

…将产生以下结果:

@H_262_5@{
  "aggregations": {
    "zoomedInVIEw": {
      "doc_count": 1,
      "zoom1": {
        "buckets": [
          {
            "key": "k362cu",
            "doc_count": 1,
            "ts": {
              "buckets": [
                {
                  "key_as_String": "Thu 2016-04-28 05:15",
                  "key": 1461820500000,
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }
  }
}

根据我们的讨论,这是一个可以为您服务的解决方案。使用Logstash,您可以调用您的API并检索大的JsON文档(使用@H_262_5@http_pollerinput),提取/转换所有位置并将其轻松沉入Elasticsearch(带有@H_262_5@elasticsearch输出)。

这是如何格式化我最初回答中描述的每个事件的方式。

  1. 使用,@H_262_5@http_poller您可以检索JsOn位置(请注意,我已将轮询间隔设置为1天,但是您可以将其更改为其他值,或者每次想要检索位置时都可以手动运行Logstash)
  2. 然后,我们@H_262_5@split将位置数组分解为单个事件
  3. 然后,我们将纬度/经度字段除以10,000,000,以获得适当的坐标
  4. 我们还需要通过移动和删除一些字段来对其进行清理
  5. 最后,我们只是将每个事件发送到Elasticsearch

Logstash配置@H_262_5@LOCATIOns.conf:

@H_262_5@input {
  http_poller {
    urls => {
      get_LOCATIOns => {
        method => get
        url => "http://your_API.com/LOCATIOns.Json"
        headers => {
          Accept => "application/Json"
        }
      }
    }
    request_timeout => 60
    interval => 86400000
    codec => "Json"
  }
}
filter {
  split {
    fIEld => "LOCATIOns" 
  }
  ruby {
    code => "
      event['LOCATIOn'] = {
        'lat' => event['LOCATIOns']['latitudeE7'] / 10000000.0,
        'lon' => event['LOCATIOns']['longitudeE7'] / 10000000.0
      }
    "
  }
  mutate {
    add_fIEld => {
      "timestampMs" => "%{[LOCATIOns][timestampMs]}"
      "accuracy" => "%{[LOCATIOns][accuracy]}"
      "junk_i_want_to_save_but_ignore" => "%{[LOCATIOns][junk_i_want_to_save_but_ignore]}"
    }
    remove_fIEld => [
      "LOCATIOns", "@timestamp", "@version" 
    ]
  }
}
output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "LOCATIOns"
    document_type => "LOCATIOn"
  }
}

然后可以使用以下命令运行:

@H_262_5@bin/logstash -f LOCATIOns.conf

运行该命令后,您可以启动搜索查询,并且应该得到期望的结果。

解决方法

我有一个geojson文件,其中包含一个位置列表,每个位置都有一个经度,纬度和时间戳。请注意,经度和纬度乘以10000000。

@H_262_5@{
  "LOCATIOns" : [ {
    "timestampMs" : "1461820561530","latitudeE7" : -378107308,"longitudeE7" : 1449654070,"accuracy" : 35,"junk_i_want_to_save_but_ignore" : [ { .. } ]
  },{
    "timestampMs" : "1461820455813","latitudeE7" : -378107279,"longitudeE7" : 1449673809,"accuracy" : 33
  },{
    "timestampMs" : "1461820281089","latitudeE7" : -378105184,"longitudeE7" : 1449254023,"accuracy" : 35
  },{
    "timestampMs" : "1461820155814","latitudeE7" : -378177434,"longitudeE7" : 1429653949,"accuracy" : 34
  }
  ..

这些位置中的许多位置将是相同的物理位置(例如,用户的家),但显然经度和纬度可能并不完全相同。

我想使用Elastic Search及其Geo功能来生成最常见位置的排名列表,如果这些位置位于彼此之间(例如100m之内),则认为这些位置相同?

对于每个公共位置,如果可能的话,我还希望它们在该位置的所有时间戳列表!

非常感谢您通过示例查询开始学习

提前谢谢了。

大佬总结

以上是大佬教程为你收集整理的使用elasticsearch地理功能查找最常见的位置?全部内容,希望文章能够帮你解决使用elasticsearch地理功能查找最常见的位置?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。