PHP操作Elasticsearch「建议收藏」

PHP操作Elasticsearch

大家好,又见面了,我是全栈君。

点击上方“ 码农编程进阶笔记 ”,选择“置顶或者星标

文末有干货,每天定时与您相约!

PHP操作Elasticsearch「建议收藏」

一、安装

以下es基于6.4

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

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

2、用 composer 安装客户端:

curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev

PHP操作Elasticsearch「建议收藏」PHP操作Elasticsearch「建议收藏」

二、快速开始

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已经有数据)

PHP操作Elasticsearch「建议收藏」

三、基本操作

1、创建索引

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

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

PHP操作Elasticsearch「建议收藏」

2、创建索引(指定模板)

$params = [
    'index' => 'test_index',
    'div' => [
        'settings' => [
            'number_of_shards' => 5,
            'number_of_replicas' => 2
        ],
        'mappings' => [
            'test_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'name' => [
                        'type' => 'text',
                        'analyzer' => 'ik_max_word'
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];
// Create the index with mappings and settings now
print_r($client->indices()->create($params));

PHP操作Elasticsearch「建议收藏」

3、删除索引、

$params = ['index' => 'test_index'];
print_r($client->indices()->delete($params));

PHP操作Elasticsearch「建议收藏」

4、更改索引的配置参数:

$params = [
    'index' => 'test_index',
    'div' => [
        'settings' => [
            'number_of_replicas' => 0,
            'refresh_interval' => -1
        ]
    ]
];

print_r($client->indices()->putSettings($params));

PHP操作Elasticsearch「建议收藏」

5、获取一个或多个索引的当前配置参数

$params = [
    'index' => [ 'test_index', 'test_data' ]
];
print_r($client->indices()->getSettings($params));

PHP操作Elasticsearch「建议收藏」

6、更改或增加一个索引的映射

$params = [
    'index' => 'test_index',
    'type' => 'test_type',
    'div' => [
        'test_type' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'name' => [
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ],
                'age' => [
                    'type' => 'integer'
                ],
                'createtime' => [
                    'type' => 'date'  //加了一个时间
                ]

            ]
        ]
    ]
];

// Update the index mapping
print_r($client->indices()->putMapping($params));

PHP操作Elasticsearch「建议收藏」

7、返回索引和类型的映射细节

$response = $client->indices()->getMapping();

// Get mappings for all types in 'my_index'
$params = ['index' => 'my_index'];
$response = $client->indices()->getMapping($params);

// Get mappings for all types of 'my_type', regardless of index
$params = ['type' => 'my_type' ];
$response = $client->indices()->getMapping($params);

// Get mapping 'my_type' in 'my_index'
$params = [
    'index' => 'my_index'
    'type' => 'my_type'
];
$response = $client->indices()->getMapping($params);

// Get mappings for two indexes
$params = [
    'index' => [ 'my_index', 'my_index2' ]
];
$response = $client->indices()->getMapping($params);

8、索引一个文档(提供id,则会更新对应id的记录。若没有提供,则会生成一条文档)

$params = [
    'index' => 'test_data',
    'type' => 'users',
    'id' => '100027',
    'div' => [ 'nickname' => 'update222']
];

// Document will be indexed to my_index/my_type/my_id
print_r($client->index($params));

9、获取文档

$params = [
    'index' => 'test_data',
    'type' => 'users',
    'id' => '100027'
];

// Get doc at /my_index/my_type/my_id
print_r($client->get($params));

PHP操作Elasticsearch「建议收藏」

10、更新文档 (doc指定要更新的字段内容)

$params = [
    'index' => 'test_data',
    'type' => 'users',
    'id' => '100027',
    'div' => [
        'doc' => [
            'nickname' => 'abc',
            'mobile' => '13800138000'
        ]
    ]
];
// Update doc at /my_index/my_type/my_id
print_r($client->update($params));

PHP操作Elasticsearch「建议收藏」

11、执行一个脚本进行更新,对某个字段的数据进行拼接或自增

$params = [
    "index" => "test_data",
    "type" => "users",
    "id" => "100027",
    "div" => [
        "script" => "ctx._source.nickname += 'hahh'"
    ]
];

print_r($client->update($params));

12、删除文档

$params = [
    'index' => 'test_data',
    'type' => 'users',
    'id' => '100027'
];

// Delete doc at /my_index/my_type/my_id
print_r($client->delete($params));

13、搜索内容

$json = '{
    "query" : {
        "match" : {
            "id" : "100073"
        }
    }
}';

$params = [
    'index' => 'test_data',
    'type' => 'users',
    'div' => $json
];
print_r($client->search($params));


$params = [
    'index' => 'test_data',
    'type' => 'users',
    'div' => [
        'query' => [
            'bool' => [
                'should' => [
                    [ 'match' => [ 'nickname' => [
                        'query' => 'user440032',
                        'boost' => 3, // 权重大
                    ]]]
                ],
            ],
        ],
        'sort' => ['id'=>['order'=>'desc']]     //排序   分页
        , 'from' => 0, 'size' => 10
    ]
];

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

在公众号后台回复”进群”关键字,即可免费加入高大上的互联网后端技术交流微信群。(公众号主免进!) 

往日精选文章

PHP 面试踩过的坑

PHP 面试踩过的坑(二)

PHP 面试踩过的坑(三)

5G时代必备技能 音视频WebRTC实时互动直播技术入门与实战

PHP操作Elasticsearch「建议收藏」

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/111845.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 第六章 SDRAM控制器的设计

    第六章 SDRAM控制器的设计介绍的重点:·动态随机存储介绍·介绍SDARM的工作原理与Verilog的实现方法·基本实验:利用基本实例来解释SDRAM控制器顶层模块的设计·高级实验:利用高级实例来完整的描述SDRAM控制器顶层模块的修改技巧与注意事项…

  • Redis:Jedis连接池JedisPool[通俗易懂]

    Redis:Jedis连接池JedisPool[通俗易懂]目录1、JedisPool的应用1.1基本应用1.2封装应用1.3增加超时重试2、JedisPool配置2.1工厂配置2.2资源池配置Jedis提供了连接池JedisPool。由于Jedis对象不是线程安全的,所以一般会从连接池中取出一个Jedis对象独占,使用完毕后再归还给连接池。maven依赖:<!–https://mv…

  • Escape/Unescape,HTML实体编码,敲击码(Tap code),摩尔斯电码(Morse Code)

    1.Escape/Unescape加密解码/编码解码,又叫%u编码,采用UTF-16BE模式,Escape编码/加密,就是字符对应UTF-1616进制表示方式前面加%u。Unescape解码/解密,就是去掉”%u”后,将16进制字符还原后,由utf-16转码到自己目标字符。如:字符“中”,UTF-16BE是:“6d93”,因此Escape是“%u6d93”。2.敲击码(Tapcode)敲击码(Tapcode)是一种以非常简单的方式对文本信息进行编码的方法。因该编码对信息通过使用一系列的点击声音

  • 人力外包 vs 软件外包

    人力外包 vs 软件外包A公司:大外包公司,人力外包,号称有7000多员工,外派某国内银行IT部门工作,开工资10KB公司:小外包公司(外企性质,需要英语能力),软件外包(类似竞标,把项目拿到本公司来做),200员工,发包方是国外大银行,开工资12K。PS:6年JAVA经验这个该选那个好?从职业生涯、稳定性等方面考虑!…

  • oracle数据库的随堂笔记(三)-过程、函数、触发器

    oracle数据库的随堂笔记(三)-过程、函数、触发器

  • PyCharm撤销快捷键以及注释快捷键

    PyCharm撤销快捷键以及注释快捷键返回快捷键:当写程序时,不小心删掉了某一行程序,Ctrl+Z或者Ctrl+Shift+Z快捷键即可返回上一步注释快捷键:选中要注释的内容然后Ctrl+/

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号