索引操作

本节通过客户端来介绍一下索引API的各种操作。索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等)。

我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法。REST API的参数是相同的,所以它应该很容易执行你所需要的操作。

创建一个索引

索引操作都包含在一个独特的命名空间中,和根对象上的方法区分开。举个例子,让我们创建一个索引:

  1. $client = new Elasticsearch\Client();
  2. $indexParams['index']  = 'my_index';    //index
  3. $client->indices()->create($indexParams);

当然,你可以指定任何通常会被包含在一个新索引创建API的参数。所有参数通常会在请求体,位于请求体的关联数组:

  1. $client = new Elasticsearch\Client();
  2. $indexParams['index']  = 'my_index';
  3. // Index Settings
  4. $indexParams['body']['settings']['number_of_shards']   = 3;
  5. $indexParams['body']['settings']['number_of_replicas'] = 2;
  6. // Example Index Mapping
  7. $myTypeMapping = array(
  8. '_source' => array(
  9. 'enabled' => true
  10. ),
  11. 'properties' => array(
  12. 'first_name' => array(
  13. 'type' => 'string',
  14. 'analyzer' => 'standard'
  15. ),
  16. 'age' => array(
  17. 'type' => 'integer'
  18. )
  19. )
  20. );
  21. $indexParams['body']['mappings']['my_type'] = $myTypeMapping;
  22. // Create the index
  23. $client->indices()->create($indexParams);

创建一个索引(高级例子)

这是一个更加复杂的创建索引的例子,展示如何定义分析,分词器、过滤器和索引设置。虽然本质上和前面的例子相同,但更复杂的例子可以帮助“现实世界”的客户使用,因为这个特殊的语法很容易陷入困境。

为简便起见,给出的例子是使用PHP 5.4+版本短数组语法([]而不是array())

  1. $params = [
  2. 'index' => 'reuters',
  3. 'body' => [
  4. 'settings' => [ // 顶级设置包含关于索引(分片等)和分析器的配置
  5. 'number_of_shards' => 1,
  6. 'number_of_replicas' => 0,
  7. 'analysis' => [ // 分析嵌套设置,包含分词器、过滤器、字符过滤器和分析器
  8. 'filter' => [
  9. 'shingle' => [
  10. 'type' => 'shingle'
  11. ]
  12. ],
  13. 'char_filter' => [
  14. 'pre_negs' => [
  15. 'type' => 'pattern_replace',
  16. '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',
  17. 'replacement' => '~$1 $2'
  18. ],
  19. 'post_negs' => [
  20. 'type' => 'pattern_replace',
  21. '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+)',
  22. 'replacement' => '$1 ~$2'
  23. ]
  24. ],
  25. 'analyzer' => [
  26. 'reuters' => [
  27. 'type' => 'custom',
  28. 'tokenizer' => 'standard',
  29. 'filter' => ['lowercase', 'stop', 'kstem']
  30. ]
  31. ]
  32. ]
  33. ],
  34. 'mappings' => [ // 映射是另外一个嵌套在body中的顶级元素,包含各种类型的映射
  35. '_default_' => [ // 默认类型是动态模版,应用于任何没有明确配置的字段
  36. 'properties' => [
  37. 'title' => [
  38. 'type' => 'string',
  39. 'analyzer' => 'reuters',
  40. 'term_vector' => 'yes',
  41. 'copy_to' => 'combined'
  42. ],
  43. 'body' => [
  44. 'type' => 'string',
  45. 'analyzer' => 'reuters',
  46. 'term_vector' => 'yes',
  47. 'copy_to' => 'combined'
  48. ],
  49. 'combined' => [
  50. 'type' => 'string',
  51. 'analyzer' => 'reuters',
  52. 'term_vector' => 'yes'
  53. ],
  54. 'topics' => [
  55. 'type' => 'string',
  56. 'index' => 'not_analyzed'
  57. ],
  58. 'places' => [
  59. 'type' => 'string',
  60. 'index' => 'not_analyzed'
  61. ]
  62. ]
  63. ],
  64. 'my_type' => [ // my_type类型是一个用户自定义的类型,包含一个my_field字段
  65. 'properties' => [
  66. 'my_field' => [
  67. 'type' => 'string'
  68. ]
  69. ]
  70. ]
  71. ]
  72. ]
  73. ];
  74. $client->indices()->create($params);

删除一个索引

删除索引是非常简单的:

  1. $deleteParams['index'] = 'my_index';
  2. $client->indices()->delete($deleteParams);

设置API配置

API设置允许你动态地修改任何索引配置:

  1. $params['index'] = 'my_index';
  2. $params['body']['index']['number_of_replicas'] = 0;
  3. $params['body']['index']['refresh_interval'] = -1;
  4. $ret = $client->indices()->putSettings($params);

获取API配置

获取APi配置可以现实当前的一个或多个索引的信息:

  1. // Get settings for one index
  2. $params['index'] = 'my_index';
  3. $ret = $client->indices()->getSettings($params);
  4. // Get settings for several indexes
  5. $params['index'] = array('my_index', 'my_index2');
  6. $ret = $client->indices()->getSettings($params);

设置API映射

设置API映射允许你修改或添加一个以存在的索引映射

  1. // Set the index and type
  2. $params['index'] = 'my_index';
  3. $params['type']  = 'my_type2';
  4. // Adding a new type to an existing index
  5. $myTypeMapping2 = array(
  6. '_source' => array(
  7. 'enabled' => true
  8. ),
  9. 'properties' => array(
  10. 'first_name' => array(
  11. 'type' => 'string',
  12. 'analyzer' => 'standard'
  13. ),
  14. 'age' => array(
  15. 'type' => 'integer'
  16. )
  17. )
  18. );
  19. $params['body']['my_type2'] = $myTypeMapping2;
  20. // Update the index mapping
  21. $client->indices()->putMapping($params);

获取API映射

获取API映射会返回关于索引和类型的详细信息。根据你希望检索的映射,你可以指定索引和类型的组合数目:

  1. // Get mappings for all indexes and types
  2. $ret = $client->indices()->getMapping();
  3. // Get mappings for all types in 'my_index'
  4. $params['index'] = 'my_index';
  5. $ret = $client->indices()->getMapping($params);
  6. // Get mappings for all types of 'my_type', regardless of index
  7. $params['type'] = 'my_type';
  8. $ret = $client->indices()->getMapping($params);
  9. // Get mapping 'my_type' in 'my_index'
  10. $params['index'] = 'my_index';
  11. $params['type']  = 'my_type'
  12. $ret = $client->indices()->getMapping($params);
  13. // Get mappings for two indexes
  14. $params['index'] = array('my_index', 'my_index2');
  15. $ret = $client->indices()->getMapping($params);

在索引命名空间中的其他API

在索引命名空间中还有其他一定数量的API允许你管理elasticsearch 的索引(添加删除模版、刷新段、关闭索引等)。

如果你使用IDE自动匹配时,你能够轻松的键入命名空间:

  1. $client->indices()->

仔细阅读列表中可用的方法。查看 \Elasticsearch\Namespaces\Indices.php 文件,会显示一个完整的可用的方法列表(以及参数列表和注释)。

Elasticsearch-PHP 索引操作的更多相关文章

  1. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  2. elasticsearch的索引操作和文档操作总结

    参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...

  3. ElasticSearch+Kibana 索引操作( 附源码)

    一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastics ...

  4. elasticsearch java 索引操作

    1.添加maven依赖 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>el ...

  5. elasticsearch的索引操作

    1.创建索引(test_index) curl -XPUT "http://192.168.99.1:9200/test_index" 2.创建索引,指定分片和副本的数量 curl ...

  6. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  7. ElasticSearch 基本概念 and 索引操作 and 文档操作 and 批量操作 and 结构化查询 and 过滤查询

    基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...

  8. Elasticsearch——多索引的使用

    在Elasticsearch中,一般的查询都支持多索引. 只有文档API或者别名等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST local ...

  9. Elasticsearch-PHP 索引操作(转)

    索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...

  10. Elasticsearch多索引

     在Elasticsearch中,一般的查询都支持多索引.只有文档API或者别名API等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST lo ...

随机推荐

  1. HTMLTestRunner显示用例打印内容

    我们知道默认的HTMLTestRunner运行时成功只会显示...,失败也只是显示E suite = unittest.TestLoader().loadTestsFromTestCase(MyTes ...

  2. springMVC+Mybatis的maven-web项目的pom.xml文件内容

    pom.xml文件内容 <!-- 第一行是XML头,指定了该xml文档的版本和编码方式 --> <project xmlns="http://maven.apache.or ...

  3. Python 3 利用 Dlib 实现人脸检测和剪切

    0. 引言 利用 Python 开发,借助 Dlib 库进行人脸检测 / face detection 和剪切:   1. crop_faces_show.py : 将检测到的人脸剪切下来,依次排序平 ...

  4. 微信开发 api 需要 https 服务器

    微信开发 api 需要 https 服务器 先建一个环境,本地的 https 服务器. 以下这篇不错,很完整. https://zhuanlan.zhihu.com/p/23640321

  5. Docker容器编排器概览

    就像Apple推出iPhone让智能手机变成主流,Docker让容器变成了主流.自从项目发布以来,Docker着重于提升开发者的体验.基本理念是可以在整个行业中,在一个标准的框架上,构建.交付并且运行 ...

  6. 使用RawComparator加速Hadoop程序

    使用RawComparator加速Hadoop程序 在前面两篇文章[1][2]中我们介绍了Hadoop序列化的相关知识,包括Writable接口与Writable对象以及如何编写定制的Writable ...

  7. 编译Lichee(FridenlyARM NanoPi-M1)碰到的问题及解决办法

    1. 提示libz.so.1找不到 需要在ubuntu上安装下面两个包: sudo apt-get install lib32ncurses5 ia32-libs 2. 提示xt_hl.o没有make ...

  8. 安装sphinx报错(undefined reference to `libiconv_open' 、undefined reference to `libiconv'、undefined reference to `libiconv_close'、make[1]: *** No rule to make target `all'. Stop. 、make: *** [all-recursive

    (为知笔记copy过来格式有变,希望对遇到此问题的童鞋有帮助) 具体错误: Thank you for choosing Sphinx! [root@vm-vagrant csft-4.1]# mak ...

  9. .NET基础复习一

    . ] ; 等号左边开辟了一个小的栈的空间.等号右边在堆空间开辟了5个空间,会将堆里开辟的第一个空间给地址赋值给栈里的空间 ,]; productList[,] ="1号"; pr ...

  10. 黄聪:如何使用Add-on SDK开发一个自己的火狐扩展

    火狐开放了扩展的开发权限给程序员们,相信很多人都会希望自己做一些扩展来方便一些使用. 我最近做一些项目也需要开发一个火狐扩展,方便收集自己需要的数据,因此研究了几天怎么开发,现在已经差不多完成了,就顺 ...