官方client php api接口日记
2019独角兽企业重金招聘Python工程师标准>>>
安装:
composer.json
{ "require": { "elasticsearch/elasticsearch": "~2.0@beta" }}
curl -s http://getcomposer.org/installer | phpphp composer.phar install --no-dev
require 'vendor/autoload.php';$client = Elasticsearch\ClientBuilder::create()->build();
主机配置:
$hosts = [ '192.168.1.1:9200', // IP + Port '192.168.1.2', // Just IP 'mydomain.server.com:9201', // Domain + Port 'mydomain2.server.com', // Just Domain 'https://localhost', // SSL to localhost 'https://192.168.1.3:9200' // SSL to IP + Port];$client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts ->build(); // Build the client object
创建一个索引
$client = ClientBuilder::create()->build();
$params = [ 'index' => 'my_index'];// Create the index
$response = $client->indices()->create($params);
//创建带有字段映射的索引
$client = ClientBuilder::create()->build();$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2 ], 'mappings' => [ 'my_type' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'first_name' => [ 'type' => 'string', 'analyzer' => 'standard' ], 'age' => [ 'type' => 'integer' ] ] ] ] ]];// Create the index with mappings and settings now$response = $client->indices()->create($params);
//高级创建索引
$params = [ 'index' => 'reuters', 'body' => [ 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 0, 'analysis' => [
'filter' => [ 'shingle' => [ 'type' => 'shingle' ] ], 'char_filter' => [ 'pre_negs' => [ 'type' => 'pattern_replace', 'pattern' => '(\\w+)\\s+((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\b', 'replacement' => '~$1 $2' ], 'post_negs' => [ 'type' => 'pattern_replace', 'pattern' => '\\b((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\s+(\\w+)', 'replacement' => '$1 ~$2' ] ], 'analyzer' => [ 'reuters' => [ 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'stop', 'kstem'] ] ] ] ], 'mappings' => [
'_default_' => [
'properties' => [ 'title' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes', 'copy_to' => 'combined' ], 'body' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes', 'copy_to' => 'combined' ], 'combined' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes' ], 'topics' => [ 'type' => 'string', 'index' => 'not_analyzed' ], 'places' => [ 'type' => 'string', 'index' => 'not_analyzed' ] ] ], 'my_type' => [
'properties' => [ 'my_field' => [ 'type' => 'string' ] ] ] ] ]];$client->indices()->create($params);
删除一个索引
$params = ['index' => 'my_index'];$response = $client->indices()->delete($params);
动态修改索引设置
$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_replicas' => 0, 'refresh_interval' => -1 ] ]];$response = $client->indices()->putSettings($params);
//获取索引设置
// Get settings for one index$params = ['index' => 'my_index'];$response = $client->indices()->getSettings($params);// Get settings for several indices$params = [ 'index' => [ 'my_index', 'my_index2' ]];$response = $client->indices()->getSettings($params);
//修改或增加已经存在的索引字段映射modify or add to an existing index’s mapping
// Set the index and type$params = [ 'index' => 'my_index', 'type' => 'my_type2', 'body' => [ 'my_type2' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'first_name' => [ 'type' => 'string', 'analyzer' => 'standard' ], 'age' => [ 'type' => 'integer' ] ] ] ]];// Update the index mapping$client->indices()->putMapping($params);
获取索引字段映射
// Get mappings for all indexes and types$response = $client->indices()->getMapping();// Get mappings for all types in 'my_index'$params = ['index' => 'my_index'];$response = $client->indices()->getMapping($params);// Get mappings for all types of 'my_type', regardless of index$params = ['type' => 'my_type' ];$response = $client->indices()->getMapping($params);// Get mapping 'my_type' in 'my_index'$params = [ 'index' => 'my_index' 'type' => 'my_type'];$response = $client->indices()->getMapping($params);// Get mappings for two indexes$params = [ 'index' => [ 'my_index', 'my_index2' ]];$response = $client->indices()->getMapping($params);
索引文档
单个文档索引,指定ID
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'testField' => 'abc']];// Document will be indexed to my_index/my_type/my_id$response = $client->index($params);
单个文档索引,不指定ID,自动生成
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'testField' => 'abc']];// Document will be indexed to my_index/my_type/<autogenerated ID>$response = $client->index($params);
附加参数
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'routing' => 'company_xyz', 'timestamp' => strtotime("-1d"), 'body' => [ 'testField' => 'abc']];$response = $client->index($params);
分块建文档
for($i = 0; $i < 100; $i++) { $params['body'][] = [ 'index' => [ '_index' => 'my_index', '_type' => 'my_type', ] ]; $params['body'][] = [ 'my_field' => 'my_value', 'second_field' => 'some more values' ];}$responses = $client->bulk($params);
分块分批建文档
$params = ['body' => []];for ($i = 1; $i <= 1234567; $i++) { $params['body'][] = [ 'index' => [ '_index' => 'my_index', '_type' => 'my_type', '_id' => $i ] ]; $params['body'][] = [ 'my_field' => 'my_value', 'second_field' => 'some more values' ]; // Every 1000 documents stop and send the bulk request if ($i % 1000 == 0) { $responses = $client->bulk($params); // erase the old bulk request $params = ['body' => []]; // unset the bulk response when you are done to save memory unset($responses); }}// Send the last batch if it existsif (!empty($params['body'])) { $responses = $client->bulk($params);}
获得文档
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id'];// Get doc at /my_index/my_type/my_id$response = $client->get($params);
更新文档
部分文档更新
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'doc' => [ 'new_field' => 'abc' ] ]];// Update doc at /my_index/my_type/my_id$response = $client->update($params);
脚本更新
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'script' => 'ctx._source.counter += count', 'params' => [ 'count' => 4 ] ]];$response = $client->update($params);
更新或插入操作
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'script' => 'ctx._source.counter += count', 'params' => [ 'count' => 4 ], 'upsert' => [ 'counter' => 1 ] ]];$response = $client->update($params);
删除文档
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; // Delete doc at /my_index/my_type/my_id $response = $client->delete($params);
搜索操作
匹配查询
curl -XGET 'localhost:9200/my_index/my_type/_search' -d '{ "query" : { "match" : { "testField" : "abc" } } }'
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ]];$results = $client->search($params);
print_r(json_encode($params['body']));{"query":{"match":{"testField":"abc"}}}
原生JSON查询
$json = '{ "query" : { "match" : { "testField" : "abc" } } }';$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => $json];$results = $client->search($params);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ]];$results = $client->search($params);$milliseconds = $results['took'];$maxScore = $results['hits']['max_score'];$score = $results['hits']['hits'][0]['_score'];$doc = $results['hits']['hits'][0]['_source'];
布尔查询
curl -XGET 'localhost:9200/my_index/my_type/_search' -d '{ "query" : { "bool" : { "must": [ { "match" : { "testField" : "abc" } }, { "match" : { "testField2" : "xyz" } } ] } } }'
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ [ 'match' => [ 'testField' => 'abc' ] ], [ 'match' => [ 'testField2' => 'xyz' ] ], ] ] ] ]];$results = $client->search($params);
复杂的查询
curl -XGET 'localhost:9200/my_index/my_type/_search' -d '{ "query" : { "filtered" : { "filter" : { "term" : { "my_field" : "abc" } }, "query" : { "match" : { "my_other_field" : "xyz" } } } } }'
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'filtered' => [ 'filter' => [ 'term' => [ 'my_field' => 'abc' ] ], 'query' => [ 'match' => [ 'my_other_field' => 'xyz' ] ] ] ] ]];$results = $client->search($params);
扫描/滚动查询
$client = ClientBuilder::create()->build();$params = [ "search_type" => "scan", // use search_type=scan "scroll" => "30s", // how long between scroll requests. should be small! "size" => 50, // how many results *per shard* you want back "index" => "my_index", "body" => [ "query" => [ "match_all" => [] ] ]];$docs = $client->search($params); // Execute the search$scroll_id = $docs['_scroll_id']; // The response will contain no results, just a _scroll_id// Now we loop until the scroll "cursors" are exhaustedwhile (\true) { // Execute a Scroll request $response = $client->scroll([ "scroll_id" => $scroll_id, //...using our previously obtained _scroll_id "scroll" => "30s" // and the same timeout window ] ); // Check to see if we got any search hits from the scroll if (count($response['hits']['hits']) > 0) { // If yes, Do Work Here // Get new scroll_id // Must always refresh your _scroll_id! It can change sometimes $scroll_id = $response['_scroll_id']; } else { // No results, scroll cursor is empty. You've exported all the data break; }}
Namespace | Functionality |
---|---|
|
Index-centric stats and info |
|
Node-centric stats and info |
|
Cluster-centric stats and info |
|
Methods to snapshot/restore your cluster and indices |
|
Access to the Cat API (which is generally used standalone from the command line |
$client = ClientBuilder::create()->build();// Index Stats// Corresponds to curl -XGET localhost:9200/_stats$response = $client->indices()->stats();// Node Stats// Corresponds to curl -XGET localhost:9200/_nodes/stats$response = $client->nodes()->stats();// Cluster Stats// Corresponds to curl -XGET localhost:9200/_cluster/stats$response = $client->cluster()->stats();
$client = ClientBuilder::create()->build();// Corresponds to curl -XGET localhost:9200/my_index/_stats$params['index'] = 'my_index';$response = $client->indices()->stats($params);// Corresponds to curl -XGET localhost:9200/my_index1,my_index2/_stats$params['index'] = array('my_index1', 'my_index2');$response = $client->indices()->stats($params);
$params['body'] = array( 'actions' => array( array( 'add' => array( 'index' => 'myindex', 'alias' => 'myalias' ) ) ));$client->indices()->updateAliases($params);
HTTP认证
$hosts = [ 'http://user:pass@localhost:9200', // HTTP Basic Authentication 'http://user2:pass2@other-host.com:9200' // Different credentials on different host];$client = ClientBuilder::create() ->setHosts($hosts) ->build();
转载于:https://my.oschina.net/sharesuiyue/blog/744007
官方client php api接口日记相关推荐
- 淘宝网及新浪网等几大官方IP查询API接口地址库的调用及使用方法教程
淘宝对外提供共享的一个IP地址库API,更新也比较及时,非常适合我们日常的使用. 以下讨论的是和淘宝网 IP查询 地址库 调用 API 相关的淘宝网及新浪网等几大官方IP查询API接口地址库的调用及使 ...
- IT隐匿者官方解析系统API接口文档
IT隐匿者官方解析系统API接口文档 简要描述: 需要到平台注册自己账号,然后开通会员权限,就有自己的api接口 平台地址:IT隐匿者解析系统 接口只能用于自己的产品,禁止打包外售或与其他人共用,发现 ...
- 12306官方火车票各种Api接口
2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...
- 丁香医生vue开发项目,接口用的主要是官方开放的Api接口,无后端
文章目录 1.项目介绍 2.部署须知 3.项目部分截图 1.项目介绍 项目源码:丁香医生源码 可以实时地显示每日的疫情状况,每日更新,有地图显示,可以看到各个区域的疫情状况. 2.部署须知 vue方式 ...
- python文本自动伪原创_Python调用有道智云文本翻译API接口实现“智能”伪原创
随着人工智能的火热,互联网热门韭菜行业SEO也貌似进入了AI的快车道,尤其以智能伪原创超级热门,你会发现几乎是个seo工具网站都挂着智能伪原创的"狗头"招牌,抱着人无我有,人有我优 ...
- python翻译成中文_Python调用有道智云文本翻译API接口实现“智能”伪原创
>> 开始伪原创中..\")"],[20,"\n","24:\"OL7j\"|36:131"],[20,&q ...
- 程序员不得不知道的 API 接口常识
说实话,我非常希望两年前刚准备找实习的自己能看到本篇文章,那个时候懵懵懂懂,跟着网上的免费教程做了一个购物商城就屁颠屁颠往简历上写. 至今我仍清晰地记得,那个电商教程是怎么定义接口的: 管它是增加.修 ...
- 分享一些前端开发者需要知道的 API 接口常识
作者: 胡涂阿菌 链接:https://www.cnblogs.com/tanshaoshenghao/p/16215751.html 说实话,我非常希望自己能早点看到本篇文章,大学那个时候懵懵懂懂, ...
- c++调用python接口_Python调用有道智云文本翻译API接口实现“智能”伪原创
>> 开始伪原创中..\")"],[20,"\n","24:\"OL7j\"|36:131"],[20,&q ...
- java 新浪短链接_新浪短链接/腾讯短链接的API接口分享(含调用代码)
最新好多朋友需要使用腾讯/新浪的官方短连接api接口,但是自己无法获取,或者说现有的API接口不支持现在的高频率调用,今天我就分享几个新浪/腾讯短域名的API接口给大家使用! 短链接他的目的就是将冗长 ...
最新文章
- Android 防止快速点击
- MySQL ADDTIME(t,n)	时间 t 加上 n 秒的时间
- HP-UX的终端TERM要设置成什么,才能输入中文呢?
- 第43课 最大公约数 《小学生C++趣味编程》
- expect java ssh_使用expect实现自动化ssh以及执行命令
- Leetcode 448. Find All Numbers Disappeared in an Array
- opencv 骨架提取_抗爆墙方盛提取车间抗爆墙记录@温州贴吧
- px和毫米的换算_关于PX像素、PT点数、CM厘米、MM毫米之间的换算
- 1041: 数列求和1
- arcengine Icommond 自定义工具
- CAD打断曲线(com接口c#语言)
- Python:混合动力汽车能量管理_动态规划简版(2/2)
- linux rm 文件找回_linux rm让你在删除的文件有地方找回 | DevOps
- linux内核编译详解
- k30最小宽度380不管用了_各场所疏散楼梯净宽度知识点归纳
- WordPress BuddyPress 越权RCE漏洞复现(CVE-2021-21389)
- allegro放置器件无法放_Allegro怎么放置后台元器件?
- CSS 中的 initial、inherit、unset、revert、all
- workbench前处理分块画分网格
- UI设计入门知识大全!零基础小白必看