zoukankan      html  css  js  c++  java
  • ElasticSearch-PHP的API使用(二)

    1:索引内的一个文档的创建(相当表记录的添加)

    比如:要添加一条记录 INSERT INTO blog(title,content,add_time) VALUES('ElasticSearch-PHP之使用二','有关于ElasticSearch在PHP下的扩展使用方法之谈','2016-11-18')

    require_once( __DIR__ . '/../vendor/elasticsearch/autoload.php');
    $hosts = Yii::app()->params['extra']['elasticsearch']['hosts'];  //array('192.168.1.10')     
    $client =  ElasticsearchClientBuilder::create()->setHosts($hosts)->build();
    $params = array(
                'index' => 'website',
                'type' => 'blog',
                'id' => 7,
                'body' => array(
                    'title' => 'ElasticSearch-PHP之使用二',
                    'content' => '有关于ElasticSearch在PHP下的扩展使用方法之谈',
                    'create_time' => '2016-11-18 08:00:00',
                ),
            );
    $resp = $client->index($params);
    echo '<pre>';
    print_r($resp);
    echo '</pre>';
    die('FILE:' . __FILE__ . '; LINE:' . __LINE__);

    2:数据查询一(get)

            $client = $this->getElasticClient();
            $params = array(
                'index' => $this->_index,
                'type' => $this->_type,
                'id' => Yii::app()->request->getParam('id', 1),
            );
            try {
                $resp = $client->get($params);
            } catch (Exception $ex) {
                $resp = $ex->getMessage();
            }
    

    3:数据查询二(search) 

            $client = $this->getElasticClient();
            $params = array(
                'index' => $this->_index,
                'type' => $this->_type,
                'body' => array(
                    'query' => array(
                        'match' => array(
                            'title' => 'elasticsearch php extends'
                        ),
                    ),
                ),
            );
            try {
                $resp = $client->search($params);
            } catch (Exception $ex) {
                $resp = $ex->getMessage();
            }
    
            echo '<pre>';
            print_r($resp);
            echo '</pre>';
            die('FILE:' . __FILE__ . '; LINE:' . __LINE__);

     

  • 相关阅读:
    Python3.7.1学习(六)RabbitMQ在Windows环境下的安装
    使用jmeter进行简单的压测
    python中mock的使用
    python-shutil模块
    python-常用模块之os、sys
    python-冒泡排序
    python-正则基础
    python-二维数组实现90度旋转
    python-使用递归实现二分法
    python-递归的实现
  • 原文地址:https://www.cnblogs.com/amuge/p/6076232.html
Copyright © 2011-2022 走看看