gpt4 book ai didi

php - elasticsearch根据位置查找文档

转载 作者:行者123 更新时间:2023-12-02 23:29:04 25 4
gpt4 key购买 nike

我正在使用带有沙发床3.2的 flex 搜索1.4。我必须根据位置查找所有文档。例如,我必须找到用户的纬度和经度,并显示他附近兴趣匹配的所有事件。我将事件数据存储为如下。

{
"createdUnder": "11",
"createdby": "4",
"name": "Vcunnect April Event Testing",
"location": "Madurai, Tamil Nadu, India",
"address": {
"venuename": "MKU University",
"address1": "MKU University",
"address2": "MKU University",
"city": "Madurai",
"state": "Tamil Nadu",
"country": "India",
"zipcode": "875485"
},
"description": "Vcunnect testing Event\r\n",
"lat": "9.9252007",
"lng": "78.11977539999998",
"startdate": "08/10/2016",
"enddate": "08/10/2016",
"starttime": "5:20pm",
"endtime": "7:00pm",
"organisers": [
{
"organisername": "Raushan Kumar",
"organiseremail": "raushankumar@gmail.in",
"organiserdescription": "Event Organizer",
"status": "active"
}
],
"starttimestamp": 1470867600,
"endtimestamp": 1470873600
}

我的文档中有两个字段,分别是“lat”和“lng”。使用这个和用户位置的纬度和经度,我想找到用户附近的事件。我正在使用此查询来获取文档。
    $searchParams['index'] = 'events';
$searchParams['type'] = 'couchbaseDocument';
$searchParams['from'] = 0;
$searchParams['size'] = 1000;
$searchParams['body']['query']['filtered']['filter']['bool']['must'][]['term']['displaystatus'] = 0;
$searchParams['body']['query']['filtered']['filter']['bool']['must'][]['term']['eventstatus'] = "active";
$searchParams['body']['query']['filtered']['filter']['bool']['must'][]['range']['starttimestamp']['from'] = $today;
$searchParams['body']['query']['filtered']['filter']['bool']['must'][]['term']['displayoutside'] = 1;
$searchParams['body']['query']['filtered']['filter']['bool']['must'][]['terms']['cat_id'] = $interestIdArray;
$result = $client->search($searchParams);

我在json文件中的映射为
{
"template" : "*",
"order" : 10,
"mappings" : {
"couchbaseCheckpoint" : {
"_source" : {
"includes" : ["doc.*"]
},
"dynamic_templates": [
{
"store_no_index": {
"match": "*",
"mapping": {
"store" : "no",
"index" : "no",
"include_in_all" : false
}
}
}
]
},
"_default_" : {
"_source" : {
"includes" : ["meta.*"]
},
"properties" : {
"meta" : {
"type" : "object",
"include_in_all" : false
}
}
}
}
}

我在Elasticsearch集群上有五个索引,分别为“事件,组,聊天消息,赞助者和用户数据”。从他们我想到仅事件和组索引的基于位置的映射。我必须改变什么..?

最佳答案

注意:此答案特定于ES 1.x,其语法在2.x中会有所不同

如果要以在Elasticsearch中可查询的方式存储坐标,则必须执行以下操作:

  • 以ES能够映射到geo_point的格式存储纬度/经度,这意味着可以作为对象{ 'location': {'lat': 123, 'lon': 123}}的子属性,也可以作为数组[<lon>, <lat>](在此注意顺序)。
  • 手动更新ES映射,以将您的位置字段视为geo_point类型。以这里为例:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-geo-point-type.html
  • 使用地理位置过滤器之一按距离/范围/边界形状进行查询,例如geo_distance过滤器:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-geo-distance-filter.html

  • 例:

    文档映射片段,将位置索引为geo_point类型:
    {
    "data" : {
    "properties" : {
    "location" : {
    "type" : "geo_point"
    }
    }
    }
    }

    带有位置的文档:
    {
    "data" : {
    "location" : {
    "lat" : 9.9252007,
    "lon" : 78.11977539999998
    }
    }
    }

    查询以找到某点半径10公里以内的文档(至少应返回前面的示例文档):
    {
    "filtered" : {
    "query" : {
    "match_all" : {}
    },
    "filter" : {
    "geo_distance" : {
    "distance" : "10km",
    "data.location" : {
    "lat" : 9.9,
    "lon" : 78.12
    }
    }
    }
    }
    }

    关于php - elasticsearch根据位置查找文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693016/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com