当前位置:首页 > PHP

php实现es

2026-02-14 13:22:21PHP

PHP 实现 Elasticsearch 的基本操作

Elasticsearch 是一个分布式搜索和分析引擎,PHP 可以通过官方提供的客户端库与 Elasticsearch 进行交互。以下是 PHP 实现 Elasticsearch 的基本操作步骤。

安装 Elasticsearch PHP 客户端

使用 Composer 安装 Elasticsearch 官方 PHP 客户端:

composer require elasticsearch/elasticsearch

连接 Elasticsearch 服务器

在 PHP 中创建 Elasticsearch 客户端实例:

require 'vendor/autoload.php';

$client = Elasticsearch\ClientBuilder::create()
    ->setHosts(['localhost:9200'])
    ->build();

创建索引

创建一个新的索引并定义映射:

$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ],
        'mappings' => [
            'properties' => [
                'title' => [
                    'type' => 'text'
                ],
                'content' => [
                    'type' => 'text'
                ],
                'timestamp' => [
                    'type' => 'date'
                ]
            ]
        ]
    ]
];

$response = $client->indices()->create($params);

添加文档

向索引中添加文档数据:

$params = [
    'index' => 'my_index',
    'id' => '1',
    'body' => [
        'title' => 'Test Document',
        'content' => 'This is a test document for Elasticsearch.',
        'timestamp' => '2023-01-01'
    ]
];

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

搜索文档

执行搜索查询并获取结果:

$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'content' => 'test'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response['hits']['hits']);

更新文档

更新索引中的文档:

$params = [
    'index' => 'my_index',
    'id' => '1',
    'body' => [
        'doc' => [
            'content' => 'Updated content for the test document.'
        ]
    ]
];

$response = $client->update($params);

删除文档

从索引中删除文档:

$params = [
    'index' => 'my_index',
    'id' => '1'
];

$response = $client->delete($params);

删除索引

删除整个索引:

$params = [
    'index' => 'my_index'
];

$response = $client->indices()->delete($params);

批量操作

执行批量操作以提高效率:

$params = [
    'body' => [
        ['index' => [
            '_index' => 'my_index',
            '_id' => '2'
        ]],
        ['title' => 'Second Document', 'content' => 'Another test document.'],
        ['index' => [
            '_index' => 'my_index',
            '_id' => '3'
        ]],
        ['title' => 'Third Document', 'content' => 'Yet another test document.']
    ]
];

$response = $client->bulk($params);

聚合查询

执行聚合查询以获取数据分析结果:

php实现es

$params = [
    'index' => 'my_index',
    'body' => [
        'aggs' => [
            'content_terms' => [
                'terms' => [
                    'field' => 'content.keyword'
                ]
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response['aggregations']);

以上是 PHP 实现 Elasticsearch 的基本操作,包括索引管理、文档操作、搜索查询和聚合分析等功能。

标签: phpes
分享给朋友:

相关文章

php实现图片上传

php实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现即时通讯

php实现即时通讯

PHP 实现即时通讯的方案 即时通讯(IM)可以通过多种方式实现,PHP 作为服务器端语言,通常需要结合前端技术(如 WebSocket、AJAX)或第三方服务来实现实时通信。以下是几种常见的实现方式…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 实现队列

php 实现队列

PHP 实现队列的方法 PHP 可以通过多种方式实现队列功能,以下是几种常见的实现方法: 使用数组实现队列 PHP 数组可以通过 array_push 和 array_shift 函数模拟队列的先进…

php 实现mvc

php 实现mvc

PHP 实现 MVC 架构的方法 MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、用户界面和用户输入。以下是 PHP 实现 MVC 架构的关键步骤:…