elasticsearch-php8*使用

PHP实战   2025-06-24 21:35   120   0  

## 连接


```php

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;
    }
}


上一篇
没有了
博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。