【问题标题】:How to add geo location in elasticsearch index如何在弹性搜索索引中添加地理位置
【发布时间】:2020-10-06 16:46:16
【问题描述】:

我正在使用elasticsearch 和php,并且我想使用地理距离查询进行搜索,但是在阅读了谷歌上的一些文章后,我必须在我当前的索引代码中添加geo location,但不知道如何添加。

$indexed = $client->index([
    'index' => 'users',
    'type' => 'user',
    'body' => [
        'name' => $name,
        'email' => $email,
        'company' => $company,
        'phone' => $phone,
        'streetAddress' => $streetAddress,
        'route' => $route,
        'city' => $locality,
        'state' => $state,
        'postalCode' => $postalCode,
        'country' => $country,
        'latitude' => $latitude,
        'longitude' => $longitude,
        'content' => $content,
        'date' => strtotime(date('Y-m-d H:i:s'))
    ]
]);

我指的是这篇Geo Location and Search 文章,但不知道如何实现和使用搜索查询。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    在您的文档中,您需要注入位置,如下所示:

    $indexed = $client->index([
    'index' => 'users',
    'type' => 'user',
    'body' => [
        'name' => $name,
        'email' => $email,
        'company' => $company,
        'phone' => $phone,
        'streetAddress' => $streetAddress,
        'route' => $route,
        'city' => $locality,
        'state' => $state,
        'postalCode' => $postalCode,
        'country' => $country,
        'content' => $content,
        'date' => strtotime(date('Y-m-d H:i:s')),
        'location' => [
            'lat' => $latitude,
            'lon' => $longitude
        ]
    ]
    ]);
    

    您还需要在文档中添加属性 (mapping):

    PUT 请求到您的弹性搜索,例如:localhost:9200/users/_mapping/user

      "properties" : {
            "location" : {
                "type" : "geo_point"
            }
      }
    

    并且在您发送的查询中是这样的:

    {
    "filtered" : {
        "query" : {
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km",
                "user.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
    }
    

    【讨论】:

    • 你能指导我在哪里添加"properties" : { "location" : { "type" : "geo_point" } }这个吗?
    • 在您的文档映射中,您可以通过向_mapping/user发送post请求来手动完成。如果您查看映射文档会更好
    • 我不知道映射,但我有这个搜索代码:$query = $client->search([ 'body' => [ 'sort' => [ 'date' => ['order' => 'desc'] ], 'query' => [ 'bool' => [ 'should' => [ 'match' => [$filter => $q] ] ] ] ] ]);
    • 我是否需要在上面的注释代码中添加`"properties" : { "location" : { "type" : "geo_point" } }` 代码?
    • No.. 有一个叫做mapping的东西,每个文档都有默认映射,你需要使用邮递员发送一个PUT请求来更新用户文档的mapping
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2020-03-06
    • 2012-09-07
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多