Elasticsearch PHP extensions
- PHP 7.1 +
$/path/to/phpize
$./configure --with-php-config=/path/to/php-config
$make && make install
Description: Creates a ElasticSearch client
$es = new ElasticSearch();
Description: Set ElasticSearch host and port.
$es->setEsConfig("http://172.16.16.221", 9200);
Description: index.
// 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);
Description: bulk.
$data = ... ; // ES JSON FORMAT
$info = $es->bulk("your_index/your_type", $data);
Description: get.
$id = "AVyqzrcLD0y03jdznsAG";
$info = $es->get("your_index/your_type", $id);
Description: mget.
$data = ... ; // ES JSON FORMAT
$info = $es->mget("your_index/your_type", $data);
Description: search.
$data = "{\"query\": {\"match_all\": {}}, \"from\":0, \"size\":1}";
$info = $es->search("your_index/your_type", $data);
Description: update.
$update_data = "{\"doc\" : {\"custom_id\":60}}";
$info = $es->update("your_index/your_type", $id, $update_data);
Description: delete.
$id = "AVyqzrcLD0y03jdznsAG";
$info = $es->delete("your_index/your_type", $id);
Description: count.
$data = ... ; // ES JSON FORMAT
$info = $es->count("your_index/your_type", $data);
Description: setQueryConvertESFormat.
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
)