大神请看
本文不一定写的全部都对,如果有哪里写得不好或者不对,可以提但请不要喷,不喜勿看绕道走就是了,虽然有可能在一些小知识点上会误导小白,但是还是可以看看基础的ES操作,对于使用还是一点帮助的
数据的查询
前两回说了Index的创建和数据的操作,那现在既然有了数据,自然要有查询的功能,查询我们PHP开发最熟悉的应该就是Mysql数据库的查询,and,or,in,=,gt,lt,egt,elt,between等等,这里我们讲一下思路,学会怎么根据ES的官方查询api反推 ruflin/elastica 这个包的使用。
相关的文档
https://www.elastic.co/guide/cn/elasticsearch/php/current/_search_operations.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
相关api
GET /index名称/_search
参数为json,raw数据传输
插入数据
为了方便我们做查询,我们先插入一批数据,共计101条
$config = $this->config();
$client = new Client($config);
$indexName = 'test';
$index = $client->getIndex($indexName);
for ($i = 10000; $i <= 10100; $i++) {
$documents[] = new Document($i, [
'id' => $i,
'name' => '白鹭 ' . $i,
'en_name' => 'bai lu ' . $i
]);
}
$index->addDocuments($documents);
查询所有数据
举个例子,查询Index中的所有数据
假如用api去查
GET /test/_search
参数:
{
"query": {
"match_all": {
}
}
}
结果:默认只匹配出来10条,返回条数是可以设置的,但是可以看到全部是101条
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 101,###### 看这里
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "10000",
"_score": 1.0,
"_source": {
"id": 10000,
"name": "白鹭 10000",
"en_name": "bai lu 10000"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "10001",
"_score": 1.0,
"_source": {
"id": 10001,
"name": "白鹭 10001",
"en_name": "bai lu 10001"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "10002",
"_score": 1.0,
"_source"