2016-08-01 25 views
1

我正在使用彈性搜索1.4與couchbase 3.2。我必須根據位置找到所有文件。例如,我必須找到用戶的經度和緯度,並顯示他的興趣所匹配的所有附近的事件。我將事件數據存儲在couchbase中。彈性serach根據位置查找文檔

{ 
    "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": "[email protected]", 
     "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 
      } 
     } 
    } 
} 
} 

我有五個指標以我命名 「活動,組,chatmessages,贊助商和用戶數據」 elasticsearch集羣。從他們我想要基於位置的映射只有事件和組索引。我必須改變..?

+0

嘿@val,任何建議從你身邊 –

+0

你的ES映射如何?在我的映射 – Val

+0

它被顯示爲 「映射」:{ 「couchbaseDocument」:{ 「DOC」:{ 「LAN」:{ 「類型」: 「串」 }, 「LAT」:{ 「type」:「string」 } } } } –

回答

1

注:這個答案是具體到ES 1.x中,語法將在2.X

有所不同。如果你想存儲的座標的方式,將在Elasticsearch可查詢的,你有執行以下操作:

  1. 商店緯度/經度在於ES將能夠映射到geo_point,這意味着一個格式或是作爲對象{ '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

實施例:

文獻映射片段來索引位置作爲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 
       } 
      } 
     } 
    } 
} 
+0

的映射結構樣本,我可以手動更新ES映射。這是一個現有的項目,我不知道如何創建映射以及如何更新它。你能向我解釋這個過程嗎? –

+0

嗨@David我使用** elasticsearch-couchbase-transport-2.1.1 **將Elasticbase服務器與Elasticsearch一起使用 –

+0

您可能想看看這個博客,它解釋瞭如何更新現有索引的映射:https: //www.elastic.co/blog/changing-mapping-with-zero-downtime –