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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HttpClient 4 和 HttpClient 3 设置超时

    HttpClient 4 和 HttpClient 3 设置超时HttpClient4:连接超时:httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);//或者HttpConnectionParams.setConnectionTimeout(params,6000);

    2022年7月22日
    12
  • 倒立摆:Simulink建模[通俗易懂]

    倒立摆:Simulink建模[通俗易懂]倒立摆:Simulink建模内容在此页面中,我们概述了如何建立倒立摆系统的模型,刹车使用Simulink及其附件进行仿真。然后可以使用非线性仿真来测试模型的线性化版本的有效性。仿真模型还可以用于评估基于线性化模型设计的控制方案的性能。物理设置和系统方程式在此示例中,我们将考虑带有手推车的倒立摆系统的二维版本,其中放置被约束为在下图所示的垂直平面中移动。对于该系统,控制输入是使推车水平移动的力,输出是摆的角位置和推车的水平位置。对于此示例…

    2022年8月18日
    22
  • VMware 15 Pro 激活码 2021[在线序列号]

    VMware 15 Pro 激活码 2021[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    95
  • linux安装tomcat7.0

    linux安装tomcat7.0本文章将详细讲解如何在linux系统中安装tomcat一、登录tomcat官网,下载tomcat压缩包。地址:http://tomcat.apache.org/  ,找到对应下载版本。这里以tomcat7.0为例。将压缩包下载完成后,利用sftp工具将包上传至linux系统,我的路径为/usr/local/java二、利用tar命令解压tomcat:tar-zxvf…

    2022年6月2日
    35
  • 想入行3D游戏建模,看我这个你还敢想吗?

    想入行3D游戏建模,看我这个你还敢想吗?所有行业都是一样的,没有什么容易的,只不过这一行是偏向于技术的,一个有好的建模师月薪10k+是很常见的,这个需要有自己刻苦学习的成果。游戏建模前景在游戏模型行业,你基本不用担心找不到工作,因为游戏模型师人才缺口非常大。举个例子:游戏制作公司的人员配比大多数是这样的:比如100人的三维制作组,可能有60人在做模型贴图,10个人在K动画。只要你保证技能在手,一定是抢手的人才。在几年前游戏建模这个行业不仅仅缺人才,甚至连新手都非常稀缺,那个时候公司愿意招聘实习生,培养他们然后给公司干活,但是工资一定不会给开的很

    2022年5月12日
    44
  • hive基础总结(面试常用)

    hive基础总结(面试常用)

    2021年11月27日
    37

发表回复

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

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