前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >初窥 Elasticsearch-PHP [1.0]

初窥 Elasticsearch-PHP [1.0]

作者头像
双面人
发布2019-04-10 15:25:01
4200
发布2019-04-10 15:25:01
举报
文章被收录于专栏:热爱IT热爱IT

初始化

Elasticsearch-PHP 教程以ThinkPHP为例

下载Elasticsearch-PHP文件放在ThinkPHP\Library\Vendor\

代码语言:javascript
复制
    Vendor('Elasticsearch.autoload');
    $params['hosts'] = array(
        '127.0.0.1:9200'
    );
    $this->client = new \Elasticsearch\Client($params);

如果不给hosts参数也是可以的,默认为localhost:9200

索引

创建一个索引


  • 自增ID public function create_index(){ $indexParams['index'] = 'my_index'; $indexParams['type'] = 'my_index'; $indexParams['body']['settings']['number_of_shards'] = 2; $indexParams['body']['settings']['number_of_replicas'] = 0; $this->client->create($indexParams); }
  • 使用自己的ID 只要再添加一个id字段即可。例: $indexParams['id'] = '123';

删除索引


代码语言:javascript
复制
public function delete_index(){
    $deleteParams['index'] = 'my_index';

    $ret = $this->client->indices()->delete($deleteParams);
    dump($ret);
}

文档


代码语言:javascript
复制
public function add_document(){
    $params = array();
    $params['body'] = array(
        'testField' => 'dfdsfdsf',
        'ok' => '1  '
    );
    $params['index'] = 'my_index2';
    $params['type'] = 'my_index2';
    $params['id'] = '222';

    $ret = $this->client->index($params);

    dump($ret);
}

  • 根据id、index、type来查询(一条数据,精确查询) public function get_document(){ $getParams = array(); $getParams['index'] = 'my_index'; $getParams['type'] = 'my_index'; $getParams['id'] = 'w1231313'; $retDoc = $this->client->get($getParams); dump($retDoc); }
  • 模糊查询 public function search_(){ $searchParams = array(); $searchParams['body'] = array( 'query' => array( 'match' => array( 'testField' => 'dfdsfdsf' ) ) ); $retDoc = $this->client->search($searchParams); dump($retDoc); }
  • 检查文档是否存在 public function exists(){ $getParams = array( 'index' => 'my_index', 'type' => 'my_index', 'id' => 'w1231313' ); $exists = $this->client->exists($getParams); dump($exists); }

代码语言:javascript
复制
    public function update_document(){
        $updateParams = array();
        $updateParams['index'] = 'my_index';
        $updateParams['type'] = 'my_index';
        $updateParams['id'] = 'w1231313';
        $updateParams['body']['doc']['testField']  = 'xxxx';

        $response = $this->client->update($updateParams);
        dump($response);
    }

代码语言:javascript
复制
    public function delete_document(){
        $deleteParams = array();
        $deleteParams['index'] = 'my_index';
        $deleteParams['type'] = 'my_index';
        $deleteParams['id'] = 'w1231313';

        $retDelete = $this->client->delete($deleteParams);
        dump($retDelete);
    }

搜索

  • 空查询 public function search(){ $retDoc = $this->client->search(); dump($retDoc); } 当然可以加上其他的一些条件,例如: $searchParams['index'] = 'my_index'; $searchParams['type'] = 'my_index';
  • 查询与过滤

term : 相当于模糊查询 match : 相当于精确查询

代码语言:javascript
复制
    public function test111(){
        $params['index'] = 'my_index';
        $params['type']  = 'my_index';

        $filter = array();
        $filter['term']['my_field'] = 'abc';

        $query = array();
        $query['match']['my_other_field'] = 'xyz';

        $params['body']['query']['filtered'] = array(
            "filter" => $filter,
            "query"  => $query
        );

        $results = $this->client->search($params);
        dump($results);
    }
  • 分页

from : 跳过开始的结果数,默认0 size: 结果数,默认为10

代码语言:javascript
复制
    public function search(){
        $searchParams['index'] = 'my_index';
        $searchParams['type'] = 'my_index';
        $searchParams['from'] = 0;
        $searchParams['size'] = 100;
        
        $retDoc = $this->client->search($searchParams);
        dump($retDoc);
    }
  • 布尔查询

must : 多个查询条件的完全匹配,相当于 and。 must_not : 多个查询条件的相反匹配,相当于 not。 should : 至少有一个查询条件匹配, 相当于 or。

代码语言:javascript
复制
    public function boolquery(){
        $params['index'] = 'my_index';
        $params['type']  = 'my_index';
        $params['body']['query']['bool']['must'] = array(
            array('match' => array('testField' => 'abc')),
            array('match' => array('anotherTestField' => 'xyz')),
        );

        $params['body']['query']['bool']['must_not'] = array(
            array('term' => array('testField' => 'abc'))
        );

        $results = $this->client->search($params);
        dump($results);
    }

END

大致的一些使用如上,若有疑问,女生请找我,男生自己看API。 传送门 es-API:http://es.xiaoleilu.com/054_Query_DSL/80_Validating_queries.html es-PHP-API:https://www.elastic.co/guide/en/elasticsearch/client/php-api/1.0/_search_operations.html

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/12/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 初始化
  • 索引
  • 文档
          • 搜索
          • END
          相关产品与服务
          Elasticsearch Service
          腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档