zoukankan      html  css  js  c++  java
  • es学习笔记2:php版本es包

    参考:

    https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html(官方php包)

    https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html(选择es-php版本)

    安装php客户端


    //
    1,安装composer包 "elasticsearch/elasticsearch": "~7.0" //2,初始化es客户端 require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build();

     

    索引操作(以7.16版本为例)

    创建索引

    $client = ClientBuilder::create()->build();
    $params = [
        'index' => 'my_index'
    ];
    
    // Create the index
    $response = $client->indices()->create($params);

    删除索引

    $params = ['index' => 'my_index'];
    $response = $client->indices()->delete($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);
    
    //设置
    $params = [
        'index' => 'my_index',
        'body' => [
            'settings' => [
                'number_of_replicas' => 0,
                'refresh_interval' => -1
            ]
        ]
    ];
    
    $response = $client->indices()->putSettings($params);

    索引文档数据结构类型设置

    //查看
    // 全部索引Get mappings for all indices
    $response = $client->indices()->getMapping();
    
    // 单个Get mappings in 'my_index'
    $params = ['index' => 'my_index'];
    $response = $client->indices()->getMapping($params);
    
    // 多个Get mappings for two indices
    $params = [
        'index' => [ 'my_index', 'my_index2' ]
    ];
    $response = $client->indices()->getMapping($params);
    
    //设置
    $params = [
        'index' => 'my_index',
        'body' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'first_name' => [
                    'type' => 'text',
                    'analyzer' => 'standard'
                ],
                'age' => [
                    'type' => 'integer'
                ]
            ]
        ]
    ];
    
    // Update the index mapping
    $client->indices()->putMapping($params);

    文档操作

    添加文档

    $params = [
        'index' => 'my_index',
        'id'    => 'my_id',//如果不设置id,会es自动生成一个id
        'body'  => [ 'testField' => 'abc']
    ];
    
    // Document will be indexed to my_index/_doc/my_id
    $response = $client->index($params);

    获取文档

    $params = [
        'index' => 'my_index',
        'id'    => 'my_id'
    ];
    
    // Get doc at /my_index/_doc/my_id
    $response = $client->get($params);

    删除文档

    $params = [
        'index' => 'my_index',
        'id'    => 'my_id'
    ];
    
    // Delete doc at /my_index/_doc_/my_id
    $response = $client->delete($params);

    修改文档

    //部分修改
    $params = [
        'index' => 'my_index',
        'id'    => 'my_id',
        'body'  => [
            'doc' => [
                'new_field' => 'abc'
            ]
        ]
    ];
    
    // Update doc at /my_index/_doc/my_id
    $response = $client->update($params);
    
    //脚本修改
    $params = [
        'index' => 'my_index',
        'id'    => 'my_id',
        'body'  => [
            'script' => 'ctx._source.counter += count',
            'params' => [
                'count' => 4
            ]
        ]
    ];
    
    $response = $client->update($params);
    
    //修改或新增
    $params = [
        'index' => 'my_index',
        'id'    => 'my_id',
        'body'  => [
            'script' => [
                'source' => 'ctx._source.counter += params.count',
                'params' => [
                    'count' => 4
                ],
            ],
            'upsert' => [
                'counter' => 1
            ],
        ]
    ];
    
    $response = $client->update($params);

    查询文档

    参考:https://www.jianshu.com/p/1e970765198c(bool多条件查询)

    //字段
    $params
    = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $results = $client->search($params); //bool组合 $params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ [ 'match' => [ 'testField' => 'abc' ] ], [ 'match' => [ 'testField2' => 'xyz' ] ], ] ] ] ] ]; $results = $client->search($params);
    //filter
    $params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'filter' => [ 'term' => [ 'my_field' => 'abc' ] ], 'should' => [ 'match' => [ 'my_other_field' => 'xyz' ] ] ] ] ] ]; $results = $client->search($params); $milliseconds = $results['took']; $maxScore = $results['hits']['max_score']; $score = $results['hits']['hits'][0]['_score']; $doc = $results['hits']['hits'][0]['_source'];
  • 相关阅读:
    bzoj 2257 (JSOI 2009) 瓶子与燃料
    bzoj 2257 (JSOI 2009) 瓶子与燃料
    splay 模板 洛谷3369
    费用流 模板 洛谷3381
    bzoj 1024 [SCOI2009]生日快乐——模拟
    bzoj 3231 [Sdoi2008]递归数列——矩阵乘法
    hdu 5823 color II——子集dp(独立集)
    bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑
    洛谷 3959 宝藏——枚举+状压dp
    bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心
  • 原文地址:https://www.cnblogs.com/tkzc2013/p/15534694.html
Copyright © 2011-2022 走看看