php 使用ES

Download Elasticsearch | Elastic

<?php

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

# 索引一个文档
# Version 7.11
$params = [
    'index' => 'my_index',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

# 获取一个文档
# Version 7.11
$params = [
    'index' => 'my_index',
    'id' => 'my_id'
];

$response = $client->get($params);
print_r($response);

# 搜索一个文档
# Version 7.11
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);

注意:

testindex 为 ElasticSearch 的索引,文档 ID 为 5,在插入文档时,报错:Rejecting mapping update to [testindex] as the final mapping would have more than 1 type: [_doc, 5]。测试环境使用的是 7.1.1 版本的 ElasticSearch,经查阅,原因是 Elasticsearch 在 7.x 版本中已经去除 type 的概念。

  type 的版本迭代:

5.x 及以前版本,一个 index 有一个或者多个 type
6.x 版本,一个 index 只有一个 type
7.x 版本移除了 type,type 相关的所有内容全部变成 Deprecated,为了兼容升级和过渡,所有的 7.x 版本 es 数据写入后 type 字段都默认被置为 “_doc”
8.x 版本完全废弃 type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值