zoukankan      html  css  js  c++  java
  • php-elasticsearch bulk批量插入数据

    1.单条插入 

    <?php
    include '../vendor/Elasticsearch/autoload.php';
    $a['hosts'] = array(
    #需要用户名时 http://user:password@URL:por 其他时候直接写ip:port
    'ip:9200',
    );
    $client = new ElasticsearchClient($a);
    
    #单条插入
    $params = array();
    $params['body'] = array(
    'xzdfaf' => 'xfsa'
    );
    $params['index'] = 'paopao';
    $params['type'] = 'test';
    // $params['id'] = 'w1231313';
    $ret = $client->index($params);
    View Code

    2.批量插入

    <?php
    include '../vendor/Elasticsearch/autoload.php';
    $a['hosts'] = array(
    #需要用户名时 http://user:password@URL:por 其他时候直接写ip:port
    'ip:9200',
    );
    $client = new ElasticsearchClient($a);
    
    #bulk批量生成
    $params['index'] = 'paopao';
    $params['type'] = 'test';
    for($i = 21; $i <= 30; $i ++) {
    $params['body'][]=array(
    'create' => array( #注意create也可换成index
    '_id'=>$i
    ),
    );
    
    $params['body'][]=array(
    'aa'=>$i
    );
    }
    $res = $client->bulk($params);
    View Code

    3.以上必须指定id,但是我想用默认的id怎么办

    <?php
    include '../vendor/Elasticsearch/autoload.php';
    $a['hosts'] = array(
    #需要用户名时 http://user:password@URL:por 其他时候直接写ip:port
    'ip:9200',
    );
    $client = new ElasticsearchClient($a);
    #bulk批量生成
    for($i = 41; $i <= 50; $i ++) {
    $params['body'][]=array(
    'index' => array(
    '_index'=> 'paopao',
    '_type'=> 'test'
    ),
    );
    
    $params['body'][]=array(
    'aa'=>$i
    );
    }
    $res = $client->bulk($params);
    View Code

    4.其他拓展
    行为 解释

    create
    当文档不存在时创建
    index 
    创建新文档或替换已有文档。
    update
    局部更新文档。
    delete
     删除一个文档。
    POST /_bulk
    { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
    { "create": { "_index": "website", "_type": "blog", "_id": "123" }}
    { "title": "My first blog post" }
    { "index": { "_index": "website", "_type": "blog" }}
    { "title": "My second blog post" }
    { "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
    { "doc" : {"title" : "My updated blog post"} } 


    请注意 delete 动作不能有请求体,它后面跟着的是另外一个操作。

    谨记最后一个换行符不要落下。

  • 相关阅读:
    Django 初试水(一)
    自己动手系列----使用数组实现一个简单的Set
    自己动手系列----使用数组实现一个简单的Map
    DB2中的MQT优化机制详解和实践
    Airy Memory 内存清理 + 注册码
    eclipse 工具翻译插件安装
    用sysdba登录oracle 11g数据库时遇到已连接到空闲例程 ora-013113
    Firewall 防火墙规则
    未找到段的定义
    ORACLE 锁表的解决方法 ORA-00054
  • 原文地址:https://www.cnblogs.com/paopao123/p/10749266.html
Copyright © 2011-2022 走看看