博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现geo相关
阅读量:6155 次
发布时间:2019-06-21

本文共 3275 字,大约阅读时间需要 10 分钟。

hot3.png

​$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;

转载于:https://my.oschina.net/sharesuiyue/blog/744006

你可能感兴趣的文章
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
centos 下安装g++
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
jquery用法大全
查看>>
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>
自己制作交叉编译工具链
查看>>
Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
查看>>
[物理学与PDEs]第3章习题1 只有一个非零分量的磁场
查看>>
深入浅出NodeJS——数据通信,NET模块运行机制
查看>>