Skip to content

cikewang/elasticsearch

Repository files navigation

elasticsearch

Elasticsearch PHP extensions

Requirement

  • PHP 7.1 +

Install

Compile elasticsearch in Linux

$/path/to/phpize
$./configure --with-php-config=/path/to/php-config
$make && make install

Class and methods

Class Elasticsearch


Description: Creates a ElasticSearch client

Example
$es = new ElasticSearch();

setEsConfig method


Description: Set ElasticSearch host and port.

Example
$es->setEsConfig("http://172.16.16.221", 9200);

index method


Description: index.

Example
// Automatic generation _id
$data = "{\"user_id\": 1,\"username\": "LiBo"}";
$info = $es->index("your_index/your_type", $data);


// Specify the _id
$id = "AVyqzrcLD0y03jdznsAG";
$data = "{\"user_id\": 1,\"username\": "LiBo"}";
$info = $es->index("your_index/your_type", $data, $id);

bulk method


Description: bulk.

Example
$data = ... ;  // ES JSON FORMAT
$info = $es->bulk("your_index/your_type", $data);

get method


Description: get.

Example
$id = "AVyqzrcLD0y03jdznsAG";
$info = $es->get("your_index/your_type", $id);

mget method


Description: mget.

Example
$data = ... ;  // ES JSON FORMAT
$info = $es->mget("your_index/your_type", $data);

search method


Description: search.

Example
$data = "{\"query\": {\"match_all\": {}}, \"from\":0, \"size\":1}";
$info = $es->search("your_index/your_type", $data);

update method


Description: update.

Example
$update_data = "{\"doc\" : {\"custom_id\":60}}";
$info = $es->update("your_index/your_type", $id, $update_data);

delete method


Description: delete.

Example
$id = "AVyqzrcLD0y03jdznsAG";
$info = $es->delete("your_index/your_type", $id);

count method


Description: count.

Example
$data = ... ;  // ES JSON FORMAT
$info = $es->count("your_index/your_type", $data);

setQueryConvertESFormat method


Description: setQueryConvertESFormat.

Example
where = "user_id in (164, 800010264)";  # support : =、 !=、 >、 >=、 <、 <=、 in; 
$order_by = "user_id DESC"; OR $order_by = "user_id DESC, user_name ASC";
$group_by = "";  						#Temporary dissupport
$limit = "0, 10"; 
$es_format_arr = $es->setQueryConvertESFormat($where, $order_by, $group_by, $limit);

print_r($es_format_arr);
Array
(
    [query] => Array
        (
            [bool] => Array
                (
                    [must] => Array
                        (
                            [0] => Array
                                (
                                    [bool] => Array
                                        (
                                            [should] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [match] => Array
                                                                (
                                                                    [user_id] => 164
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [match] => Array
                                                                (
                                                                    [user_id] => 800010264
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [sort] => Array
        (
            [0] => Array
                (
                    [user_id] => Array
                        (
                            [order] => DESC
                        )

                )

        )

    [from] => 0
    [size] => 10
)



About

Elasticsearch PHP extensions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published