PHP操作Elasticsearch详解

本文详细介绍了如何使用PHP与Elasticsearch进行交互,包括安装步骤、快速启动示例和一系列基本操作,如创建、删除索引,更新文档以及执行搜索等。适合PHP开发者学习Elasticsearch操作。
摘要由CSDN通过智能技术生成

一、安装

以下es基于6.4

1、在 composer.json 文件中引入 elasticsearch-php:

    "require":{
        "elasticsearch/elasticsearch":"~6.0",
        "monolog/monolog": "~1.0"
    }
}

2、用 composer 安装客户端:

php composer.phar install --no-dev

在这里插入图片描述
在这里插入图片描述

二、快速开始

1、创建一个test.php文件,内容如下

<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;


$hosts = [
    '192.168.16.241:9200',         // IP + Port
    '192.168.16.241',              // Just IP
    'localhost:9200', // Domain + Port
    'localhost',     // Just Domain
    'http://localhost',        // SSL to localhost
    'https://192.168.16.241:9200'  // SSL to IP + Port
];
$client = ClientBuilder::create()->setHosts($hosts)->build();            // Instantiate a new ClientBuilder  // Set the hosts


$params = [
    'index'  => 'test_data',
    'type'   => 'users',
    'id'     => 100027,
    'client' => [ 'ignore' => 404 ]
];
var_dump( $client->get($params));

2、浏览器访问test.php,结果如下(前提是你的es已经有数据)

在这里插入图片描述

三、基本操作

1、创建索引

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

// Create the index
print_r($client->indices()->create($params));

在这里插入图片描述
2、创建索引(指定模板)

$params = [
    'index' => 'test_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 5,
            'number_of_replicas' => 2
        ],
        'mappings' => [
            'test_type' => [
                '_source' => [
                    'enabled' =>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值