初窥 Elasticsearch-PHP [1.0]

2016-03-15  本文已影响5659人  babybus_hentai

初始化


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

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

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

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

索引

创建一个索引

删除索引

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



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

搜索

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

上一篇 下一篇

猜你喜欢

热点阅读