PHP的ES入门(三)—— 数据查询

本文介绍了如何在PHP中使用ruflin/elastica库进行Elasticsearch的数据查询,包括查询所有数据、范围查询、AND逻辑及复合查询等,通过官方API转化为PHP代码实现。
摘要由CSDN通过智能技术生成

大神请看

本文不一定写的全部都对,如果有哪里写得不好或者不对,可以提但请不要喷,不喜勿看绕道走就是了,虽然有可能在一些小知识点上会误导小白,但是还是可以看看基础的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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值