弹性serach根据位置查找文档

时间:2016-08-01 07:18:59

标签: php elasticsearch couchbase

我正在使用带有couchbase 3.2的弹性搜索1.4。我必须根据位置找到所有文件。例如,我必须找到用户的纬度和经度,并显示他感兴趣的匹配附近的所有事件。我将事件数据存储在couchbase上。

{
  "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
            }
        }
    }
}
}

我的弹性搜索集群中有五个索引名为“事件,组,聊天消息,赞助商和用户数据”。从他们我想要基于位置的映射只为事件和组索引。我需要改变什么..?

1 个答案:

答案 0 :(得分:1)

注意:此答案特定于ES 1.x,语法在2.x

中有所不同

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

  1. 以一种ES能够映射到geo_point的格式存储lat / lon,这意味着作为对象的子属性{ 'location': {'lat': 123, 'lon': 123}},或者作为数组[<lon>, <lat>](注意在这里订购)。
  2. 手动更新ES映射,将您的位置字段视为geo_point类型。看一下这里的例子:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-geo-point-type.html
  3. 使用其中一个地理过滤器按距离/范围/边界形状查询,例如geo_distance过滤器:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-geo-distance-filter.html
  4. 示例:

    文档映射片段以将位置索引为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
                    }
                }
            }
        }
    }