编程学习网 > PHP技术 > php高级 > PHP操作Elasticsearch
2020
03-24

PHP操作Elasticsearch

attachments-2020-03-3r28NMwL5e796bf57a0c0.jpg

一、安装

以下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

v2-11bebee1bbd03d340f8594aa1a8734eb_720w.pngv2-3caaa355e9b8048037d8a99a356d095c_720w.jpg

二、快速开始

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

v2-f6752c7a8eadf211bf4622fb0dcccb2f_720w.jpg

三、基本操作

1、创建索引

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

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

v2-7ecbb0c566e20f21f201a27ebd33270d_720w.jpg

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

$params = [
    'index' => 'test_index',
    'body' => [
        '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));

v2-e592439480f0ab99fbc8afb161d74fae_720w.jpg

3、删除索引、

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

v2-e5961f0d4ccf81215e95304527c692e3_720w.jpg

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

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

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

v2-78cb704c5e1c7aacef385d3df205bc29_720w.jpg

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

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

v2-488428a1817c74096e1241cb043b3699_720w.jpg

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

$params = [
    'index' => 'test_index',
    'type' => 'test_type',
    'body' => [
        '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));

v2-1ba86d189788c06fa6729c1a5cb89405_720w.jpg

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',
    'body' => [ '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));

v2-46b9cb83d28eb3dc3d5092159e44275f_720w.jpg

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

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

v2-10a1238ec05a913a568f7cffaa6531c1_720w.jpg

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

$params = [
    "index" => "test_data",
    "type" => "users",
    "id" => "100027",
    "body" => [
        "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',
    'body' => $json
];
print_r($client->search($params));


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

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


扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取