$root_dir = $this->_app->config('ROOT_DIR');
require_once $root_dir . "/vendor/autoload.php";
$client = Elasticsearch\ClientBuilder::create()->build();
//创建索引
$params = [
'index' => '**************',
'body' => [
'mappings' => [
'shop' => [
'_source' => [
'enabled' => true
],
'properties' => [
'sid' => [
'type' => 'string'
],
'name' => [
'type' => 'string'
],
'address' => [
'type' => 'string'
],
'location' => [
'type' => 'geo_point'
]
]
]
]
]
];
$response = $client->indices()->create($params);
//创建文档
$shops = Shop::find('lat !=0 AND is_deleted=0')->asArray()->getAll();
foreach($shops as $s)
{
$data = array();
$data['sid'] = $s['sid'];
$data['name'] = $s['name'];
$data['address'] = $s['address'];
$data['location'] = $s['lat'].",".$s['lng'];
$params = array(
'index' => '***************',
'type' => 'shop',
'id' => $s['sid'],
'body' => $data
);
//dump($params);exit;
// Document will be indexed to my_index/my_type/my_id
$response = $client->index($params);
//dump($response);exit;
}
echo "ok";
exit;
//搜索
$params = [
'index' => '*************',
'type' => 'shop',
'body' => [
'query' => [
'filtered' =>[
'filter' => [
'geo_distance' => [
'distance' => '1km',
'distance_type' => 'plane',
'location' => [
'lat' => 31.219,
'lon' => 121.474
]
]
]
]
],
'sort' => [
'_geo_distance' => [
'location' => [
'lat' => 31.219,
'lon' => 121.474
],
'order' => 'asc',
'unit' => 'km',
'distance_type' => 'plane'
]
]
]];
$results = $client->search($params);
dump($results);
exit;
$params = [
'index' => '**************',
'type' => 'shop',
'q' => 'name:Paris',
'body' => '{"size":20,"query":{"filtered":{"filter":{"geo_distance":{"distance":"1km","distance_type":"plane","location":{"lat":31.23958,"lon":121.497816}}}}},"sort":{"_geo_distance":{"location":{"lat":31.23958,"lon":121.497816},"order":"asc","unit":"km","distance_type":"plane"}}}'
];
$results = $client->search($params);
dump($results);
exit;
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => '****************',
'type' => 'shop',
'body' => [
'query' => [
'filtered' =>[
'query' => [
//'match_all' => new stdClass(),
'match' => [
'name' => 'Bar'
]
],
'filter' => [
'geo_distance' => [
'distance' => '1km',
'distance_type' => 'plane',
'location' => [
'lat' => 31.219,
'lon' => 121.474
]
]
]
]
],
'sort' => [
'_geo_distance' => [
'location' => [
'lat' => 31.219,
'lon' => 121.474
],
'order' => 'asc',
'unit' => 'km',
'distance_type' => 'plane'
]
]
]];
// dump(json_encode($params['body']));
//exit;
$results = $client->search($params);
dump($results);
exit;
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => '***************',
'type' => 'shop',
'body' => [
'query' => [
'filtered' =>[
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'name' => 'Paris' ] ],
// [ 'match' => [ 'address' => '兴业路' ] ],
]
]
],
'filter' => [
'geo_distance' => [
'distance' => '1km',
'distance_type' => 'plane',
'location' => [
'lat' => 31.219,
'lon' => 121.474
]
]
]
]
],
'sort' => [
'_geo_distance' => [
'location' => [
'lat' => 31.219,
'lon' => 121.474
],
'order' => 'asc',
'unit' => 'km',
'distance_type' => 'plane'
]
]
]];
// dump(json_encode($params['body']));
//exit;
$results = $client->search($params);
dump($results);
exit;