索引操作

索引在客户端非常容易。因为关联数组很容易转换为JSON文档,索引文档只是提供正确和结构性的关联数组和调用方法。

单文档索引

当你索引你个文档时,可以自己提供一个ID,也可以让elasticsearch 为你生成一个ID。

提供一个ID值

  1. $params = array();
  2. $params['body']  = array('testField' => 'abc');
  3. $params['index'] = 'my_index';
  4. $params['type']  = 'my_type';
  5. $params['id']    = 'my_id';
  6. // Document will be indexed to my_index/my_type/my_id
  7. $ret = $client->index($params);

缺省ID值

  1. $params = array();
  2. $params['body']  = array('testField' => 'abc');
  3. $params['index'] = 'my_index';
  4. $params['type']  = 'my_type';
  5. // Document will be indexed to my_index/my_type/<autogenerated_id>
  6. $ret = $client->index($params);

像大多数其他API一样,还有一些其他参数可以指定。它们在参数数组中指定的就像是索引或类型。例如,让我们设置这个新文档的路由和时间戳。

附加参数

  1. $params = array();
  2. $params['body']  = array('testField' => 'xyz');
  3. $params['index']     = 'my_index';
  4. $params['type']      = 'my_type';
  5. $params['routing']   = 'company_xyz';
  6. $params['timestamp'] = strtotime("-1d");
  7. $ret = $client->index($params);

批量索引

Elasticsearch还支持批量索引文档。客户端也提供一个批量索引的接口,但是并不是很友好的。在未来我们会添加“帮助”方法去简化这个流程。

批量的API方法期待一个批量的body和友好的elasticsearch所期待的是一样的:JSON的 动作/元数据对被新行分割。一个常见的批量操作如下:

使用PHP数组批量索引

  1. for($i = 0; $i < 100; $i++) {
  2. $params['body'][] = array(
  3. 'index' => array(
  4. '_id' => $i
  5. )
  6. );
  7. $params['body'][] = array(
  8. 'my_field' => 'my_value',
  9. 'second_field' => 'some more values'
  10. );
  11. }
  12. $responses = $client->bulk($params);

你当然可以使用任何一个批量方法,这里有一个使用upserts的例子:

使用PHP数组进行批量upserting操纵

  1. for($i = 0; $i < 100; $i++) {
  2. $params['body'][] = array(
  3. 'update' => array(
  4. '_id' => $i
  5. )
  6. );
  7. $params['body'][] = array(
  8. 'doc_as_upsert' => 'true',
  9. 'doc' => array(
  10. 'my_field' => 'my_value',
  11. 'second_field' => 'some more values'
  12. )
  13. );
  14. }
  15. $responses = $client->bulk($params);

批量更新与Nowdocs

如果你是手工的指定块或者是从现有的JSON文件中提取它们,Nowdocs 可能是最好的方法。否则,当你通过算法去构造它们,小心确保使用 ‘\n"换行符分割每一行,包括最后一行。
批量索引

  1. $params = array();
  2. $params['body']  = <<<'EOT'
  3. { "index" : { "_index" : "my_index", "_type" : "my_type", "_id" : "1" } }
  4. { "field1" : "value1" }
  5. EOT;
  6. $ret = $client->bulk($params);

像批量API一样,如果你在参数中制定索引/类型,你可以从批量请求本身省略掉它(这往往可以省略大量的空间和冗余的数据传输)
批量索引 w/ 明确的索引/类型

    1. $params = array();
    2. $params['body']  = <<<'EOT'
    3. { "index" : { "_id" : "1" } }
    4. { "field1" : "value1" }
    5. EOT;
    6. $params['index'] = 'my_index';
    7. $params['type']  = 'my_type';
    8. $ret = $client->bulk($params);

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

  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. hadoop入门手册1:hadoop【2.7.1】【多节点】集群配置【必知配置知识1】

    问题导读 1.说说你对集群配置的认识?2.集群配置的配置项你了解多少?3.下面内容让你对集群的配置有了什么新的认识? 目的 目的1:这个文档描述了如何安装配置hadoop集群,从几个节点到上千节点.为 ...

  2. 《DSP using MATLAB》示例Example 8.21

    %% ------------------------------------------------------------------------ %% Output Info about thi ...

  3. LeetCode Best Time to Buy and Sell Stock with Transaction Fee

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/descripti ...

  4. nginx ngscript 简单使用

    备注: 默认没有集成到nginx包里,需要单独安装(推荐使用动态模块的方式进行安装) 1. 安装 wget https://nginx.org/download/nginx-1.13.11.tar.g ...

  5. jsp转向,exception

    jsp要像servlet那样转向时可以得 <jsp:forward page="Counter.jsp"> <jsp:param name="parma ...

  6. Zookeeper--安装及命令

    Zookeeper--单机模式安装 及命令 下载地址: http://zookeeper.apache.org/releases.html tar -zxvf zookeeper-3.4.10.tar ...

  7. Spring Cloud 服务网关Zuul

    Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...

  8. python 网络编程--socket模块/struct模块

    socket模块: 客户端:CS架构,  client -> server 浏览器:BS架构,  browser -> server 网络通信本质:传输字节 doc命令查看ip地址:ipc ...

  9. python--logging库学习_第一波

    简单使用 #!/usr/local/bin/python # -*- coding:utf-8 -*- import logging logging.debug('debug message') lo ...

  10. JAVA代码实现从文件写入东西后有读出来=========FileInputStream

    总结: 这个原理是,先把for循环里的数从程序读到文件里,然后把文件把内容读到程序里 package com.a.b; import java.io.FileInputStream; import j ...