2017-08-08 72 views
1

我使用的是elasticsearchphp,我想用Geo Distance Query進行搜索,但是在閱讀google上的一些文章之後,我必須在當前索引代碼中添加geo location,但不知道如何加。如何在elasticsearch索引中添加地理位置

$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文章,但沒有得到任何想法如何實現和使用的搜索查詢。

有什麼想法?

謝謝。

回答

0

文檔中你需要注入的位置,就像這樣:

$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 
      } 
     } 
    } 
} 
} 
+0

能否請你指導我,我要加' 「屬性」:{ 「位置」:{ 「類型」: 「geo_point」 } }'這個? –

+0

在你的文檔的映射中,你可以通過發送post請求到'_mapping/user'手動完成。如果你看一下映射文檔 –

+0

我還沒有關於映射的想法,但我有這個搜索代碼:'$ query = $ client-> search'[ 'body'=> [ 'sort'=>'date'=> ['order'=>'desc'] ], 'query' => [ '布爾'=> [ '應當'=> [ '匹配'=> [$濾波器=> $ q] ] ] ] ] ]);' –