Elasticsearch-PHP 索引操作
索引操作
本节通过客户端来介绍一下索引API的各种操作。索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等)。
我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法。REST API的参数是相同的,所以它应该很容易执行你所需要的操作。
创建一个索引
索引操作都包含在一个独特的命名空间中,和根对象上的方法区分开。举个例子,让我们创建一个索引:
- $client = new Elasticsearch\Client();
- $indexParams['index'] = 'my_index'; //index
- $client->indices()->create($indexParams);
当然,你可以指定任何通常会被包含在一个新索引创建API的参数。所有参数通常会在请求体,位于请求体的关联数组:
- $client = new Elasticsearch\Client();
- $indexParams['index'] = 'my_index';
- // Index Settings
- $indexParams['body']['settings']['number_of_shards'] = 3;
- $indexParams['body']['settings']['number_of_replicas'] = 2;
- // Example Index Mapping
- $myTypeMapping = array(
- '_source' => array(
- 'enabled' => true
- ),
- 'properties' => array(
- 'first_name' => array(
- 'type' => 'string',
- 'analyzer' => 'standard'
- ),
- 'age' => array(
- 'type' => 'integer'
- )
- )
- );
- $indexParams['body']['mappings']['my_type'] = $myTypeMapping;
- // Create the index
- $client->indices()->create($indexParams);
创建一个索引(高级例子)
这是一个更加复杂的创建索引的例子,展示如何定义分析,分词器、过滤器和索引设置。虽然本质上和前面的例子相同,但更复杂的例子可以帮助“现实世界”的客户使用,因为这个特殊的语法很容易陷入困境。
为简便起见,给出的例子是使用PHP 5.4+版本短数组语法([]而不是array())
- $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' => [ // 映射是另外一个嵌套在body中的顶级元素,包含各种类型的映射
- '_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' => [ // my_type类型是一个用户自定义的类型,包含一个my_field字段
- 'properties' => [
- 'my_field' => [
- 'type' => 'string'
- ]
- ]
- ]
- ]
- ]
- ];
- $client->indices()->create($params);
删除一个索引
删除索引是非常简单的:
- $deleteParams['index'] = 'my_index';
- $client->indices()->delete($deleteParams);
设置API配置
API设置允许你动态地修改任何索引配置:
- $params['index'] = 'my_index';
- $params['body']['index']['number_of_replicas'] = 0;
- $params['body']['index']['refresh_interval'] = -1;
- $ret = $client->indices()->putSettings($params);
获取API配置
获取APi配置可以现实当前的一个或多个索引的信息:
- // Get settings for one index
- $params['index'] = 'my_index';
- $ret = $client->indices()->getSettings($params);
- // Get settings for several indexes
- $params['index'] = array('my_index', 'my_index2');
- $ret = $client->indices()->getSettings($params);
设置API映射
设置API映射允许你修改或添加一个以存在的索引映射
- // Set the index and type
- $params['index'] = 'my_index';
- $params['type'] = 'my_type2';
- // Adding a new type to an existing index
- $myTypeMapping2 = array(
- '_source' => array(
- 'enabled' => true
- ),
- 'properties' => array(
- 'first_name' => array(
- 'type' => 'string',
- 'analyzer' => 'standard'
- ),
- 'age' => array(
- 'type' => 'integer'
- )
- )
- );
- $params['body']['my_type2'] = $myTypeMapping2;
- // Update the index mapping
- $client->indices()->putMapping($params);
获取API映射
获取API映射会返回关于索引和类型的详细信息。根据你希望检索的映射,你可以指定索引和类型的组合数目:
- // Get mappings for all indexes and types
- $ret = $client->indices()->getMapping();
- // Get mappings for all types in 'my_index'
- $params['index'] = 'my_index';
- $ret = $client->indices()->getMapping($params);
- // Get mappings for all types of 'my_type', regardless of index
- $params['type'] = 'my_type';
- $ret = $client->indices()->getMapping($params);
- // Get mapping 'my_type' in 'my_index'
- $params['index'] = 'my_index';
- $params['type'] = 'my_type'
- $ret = $client->indices()->getMapping($params);
- // Get mappings for two indexes
- $params['index'] = array('my_index', 'my_index2');
- $ret = $client->indices()->getMapping($params);
在索引命名空间中的其他API
在索引命名空间中还有其他一定数量的API允许你管理elasticsearch 的索引(添加删除模版、刷新段、关闭索引等)。
如果你使用IDE自动匹配时,你能够轻松的键入命名空间:
- $client->indices()->
仔细阅读列表中可用的方法。查看 \Elasticsearch\Namespaces\Indices.php 文件,会显示一个完整的可用的方法列表(以及参数列表和注释)。
Elasticsearch-PHP 索引操作的更多相关文章
- ElasticSearch+Kibana 索引操作
ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...
- elasticsearch的索引操作和文档操作总结
参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...
- ElasticSearch+Kibana 索引操作( 附源码)
一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastics ...
- elasticsearch java 索引操作
1.添加maven依赖 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>el ...
- elasticsearch的索引操作
1.创建索引(test_index) curl -XPUT "http://192.168.99.1:9200/test_index" 2.创建索引,指定分片和副本的数量 curl ...
- Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查
今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...
- ElasticSearch 基本概念 and 索引操作 and 文档操作 and 批量操作 and 结构化查询 and 过滤查询
基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...
- Elasticsearch——多索引的使用
在Elasticsearch中,一般的查询都支持多索引. 只有文档API或者别名等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST local ...
- Elasticsearch-PHP 索引操作(转)
索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...
- Elasticsearch多索引
在Elasticsearch中,一般的查询都支持多索引.只有文档API或者别名API等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST lo ...
随机推荐
- JSON 总结
<!--Json格式配置映射 直接能访问JSON文本数据--> <system.webServer> <staticContent> <mimeM ...
- 为什么 UEFI 方式启动的 U 盘必须使用 FAT32 文件系统?
如果你希望更刺激地安装 Windows,那么你需要了解很多 Windows 系统相关的问题. 为什么 UEFI 方式启动的 U 盘必须使用 FAT32 文件系统? 因为 NTFS 是 Windows ...
- 《DSP using MATLAB》示例Example 8.10
这个例子的代码我不会写,只是放一张书的截图图片在这里.以后再改进吧.
- 阿里云视频点播 php开发
先购买开通阿里云的<视频点播>服务,视频点播 可以购买套餐 ,我在项目中使用的是299套餐 开发前在<用户信息管理>生成Access Key Secret,开发密钥使用 阿里云 ...
- Android中处理崩溃异常 (转)
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- java内存占用问题(一)
Nocturne 2012-12-24 java数组内存占用问题. 30 Contact[] ca = new Contact[10]; while(x<10){ ca[x]=new ...
- CentOS 7 安装 Nodejs npm 及版本冲突解决
JC&BC 笔记: 可能没安装过 npm 的人会有点疑惑,安装 npm 跟安装 nodejs 有什么关系? 安装 npm 其实就是安装 nodejs 的过程.这一点官方说的很明白,npm 依赖 ...
- centos开机提示CPU无法识别的问题
centos版本和对应cpu兼容性,官方网址: https://access.redhat.com/support/policy/intel Red Hat Enterprise Linux Vers ...
- 十七.jQuery源码解析之入口方法Sizzle(1)
函数Sizzle(selector,context,results,seed)用于查找与选择器表达式selector匹配的元素集合.该函数是选择器引擎的入口. 函数Sizzle执行的6个关键步骤如下: ...
- Oracle 10g RAC TAF
Oracle RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会影 ...