初窺 Elasticsearch-PHP [1.0]

初始化


Elasticsearch-PHP
下載地址:http://www.thinkphp.cn/code/download/id/1290.html
教程以ThinkPHP爲例php

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

Vendor('Elasticsearch.autoload');
    $params['hosts'] = array(
        '127.0.0.1:9200'
    );
    $this->client = new \Elasticsearch\Client($params);

若是不給hosts參數也是能夠的,默認爲localhost:9200thinkphp

索引

建立一個索引api


  • 自增IDelasticsearch

    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);
      }
  • 使用本身的IDide

    只要再添加一個id字段便可。例:ui

    $indexParams['id'] = '123';

刪除索引this


public function delete_index(){
    $deleteParams['index'] = 'my_index';

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

文檔


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來查詢(一條數據,精確查詢)spa

    public function get_document(){
          $getParams = array();
          $getParams['index'] = 'my_index';
          $getParams['type'] = 'my_index';
          $getParams['id'] = 'w1231313';
          
          $retDoc = $this->client->get($getParams);
          dump($retDoc);
      }
  • 模糊查詢code

    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);
      }


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);
    }


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 : 至關於精確查詢

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

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。

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

相關文章
相關標籤/搜索