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「建议收藏」

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ManualResetEvent用法「建议收藏」

    ManualResetEvent用法「建议收藏」ManualResetEvent用法

    2022年7月13日
    12
  • Java截取字符串的常见方法「建议收藏」

    Java截取字符串的常见方法「建议收藏」转自:https://blog.csdn.net/zjx2016/article/details/74557301在项目中经常会遇到截取字符串的需求,这里重点介绍两种常见的截取字符串方法。方法一:通过split()将正则传入split()。返回的是一个字符串数组类型。不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时。Stringstr="53285964@qq.co…

    2022年5月24日
    55
  • 女生学Java软件开发好就业吗

    女生学Java软件开发好就业吗  java在IT行业非常火热,近几年不仅引起了很多人的关注,女性同胞也非常关注这一行业,想要学习java技术,但是不知道女生学Java软件开发好就业吗?来看看下面的详细介绍就知道了。  女生学Java软件开发好就业吗?目前大多数想要参加Java培训学习女生的一个重要关注的话题,学习不用多说,只要是自己足够的努力,在选择一个靠谱的Java培训机构,还是比较容易学会的。有的时候我们可以看到同样的老师、同样的课程和同样的学习方式,整个Java培训过程下来女生很多是要比男生学习的更好。  所以,在学习

    2022年7月8日
    17
  • SQL语句嵌套最好用quotedstr函数替换

    SQL语句嵌套最好用quotedstr函数替换
    dmcc.delete_rm_zjfhsj.add(‘select数量fromrm_zjfhsjwhere类型=’CTO”);dmcc.delete_rm_zjfhsj.SQL.add(‘select数量fromrm_zjfhsjwhere类型=’+quotedstr(‘CTO’));凡是内引号对,最好用quotedstr函数替换

    2022年10月17日
    3
  • 5G学习(三)-SSB与初始接入

    5G学习(三)-SSB与初始接入SSB概念SSB是同步信号和PBCH块(SynchronizationSignalandPBCHblock)组合在一起的。它由主同步信号(PrimarySynchronizationSignals,简称PSS)、辅同步信号(SecondarySynchronizationSignals,简称SSS)、PBCH三部分共同组成。SSB频域位置可以从图中看到,SSB时域上共占用4个OFDM符号,频域共占用240个子载波(20个PRB),PSS位于符号0的中间127个子载波。SSS

    2022年6月17日
    483
  • 李宏毅《机器学习 深度学习》简要笔记(一)

    李宏毅《机器学习 深度学习》简要笔记(一)线性回归,梯度下降,优化算法

    2022年8月3日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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