php elasticsearch-php8*使用

连接

function client(){
    try {
        $client = ClientBuilder::create()
            ->setHosts(['https://127.30.0.1:9200'])
            ->setSSLVerification(false) // 禁用 SSL 证书验证
            ->setBasicAuthentication('账号', '密码')
            ->build();
// 检查连接是否成功
        $response = $client->ping();
        if ($response) {
            return $client;
        } else {
            echo "连接失败";exit();
        }
    }catch (\Exception){
        return false;
    }
}

索引

文本类型
text:用于全文搜索,会被分析器分词。
keyword:用于精确值,不会被分析器分词,适用于过滤、排序、聚合。
数值类型
long:64位整数。
integer:32位整数。
short:16位整数。
byte:8位整数。
double:64位双精度浮点数。
float:32位单精度浮点数。
half_float:16位半精度浮点数。
scaled_float:缩放的浮点数,用于精确的小数但不需要完整的double精度。
日期和时间类型
date:包含日期的字段,可以解析多种日期格式。
布尔类型
boolean:布尔值(true或false)。
二进制类型
binary:接受一个base64编码的字符串,Elasticsearch不会对其进行索引。
范围类型
integer_range、float_range、long_range、double_range、date_range:用于存储一个范围的值,如开始日期和结束日期。
具体索引建立

//建立索引的字段根据自己的实际情况建立
$error_collection_indices = [
    'unique_id'         => ['type' => 'keyword'],//错误id
    'state'             => ['type' => 'integer'],//状态
    'error_type'        => ['type' => 'keyword'],
    'operation_records' => ['type' => 'text'],操作记录
    'create_time'       => ['type' => 'integer'],//创建时间
    'update_time'       => ['type' => 'integer'],///更新
];
//判断索引存不存在 不存在则去创建
if (!indices_exists_index($client,$indexName)){
            $params = [
                'index' => $indexName,
                'body' => [
                    'mappings' => [
                        'properties' => $data
                    ]
                ]
            ];

            $res = $client->indices()->create($params);
            if ($res->getStatusCode() =='200'){
                return true;
            }else{
                return false;
            }
 }

操作

插入

$data 插入数据  
$indexName  索引(我的理解类似于表名,但是这在官方解释是索引)
function install($client,$data,$indexName){
    try {
        $params = [
            'index' => $indexName,
            'body' => $data
        ];
        $response = $client->index($params);
        if ($response->getStatusCode() =='201'){
          return $response->asArray();
        }
        return  false;
    }catch (\Exception){
        return false;
    }
}

删除

索引删除
function indices_delete_index($client,$indexName){
    try {
        $res = $client->indices()->delete(['index' => $indexName]);
        if ($res->getStatusCode() == '200') {
            return true;
        } else {
            return false;
        }
    }catch (\Exception){
        return false;
    }
}

查询

/**
 * 查询
 * @param $client
 * @param $query
 * @param $indexName
 * @return false|mixed
 */
function query($client,$query,$indexName){
    try {
        $params = [
            'index' => $indexName,
            'body' => $query,
        ];
        $response = $client->search($params);
        if ($response->getStatusCode() =='200'){
            return $response->asArray();
        }
        return false;
    }catch (\Exception){
        return false;
    }
}
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值