恕在下才疏学浅,是在不知道怎么组装语句来做前言了,那就粗鄙一点点吧.PHP 牛逼!ElasticSearch 牛逼!PHP + ElasticSearch 牛逼逼!!!

初始化Composer仓库

cd test
mkdir es
composer init
# 遇到选填空的就```键入n```,其他都回车就行
 Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [qvbilam/es]:
Description []:
Author [, n to skip]: n
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:

{
    "name": "qvbilam/es",
    "require": {}
}

Do you confirm generation [yes]?

# 可以看到目录下会成声composer.json文件.说明初始化成功

下载PHP自动加载类

composer dump-autoload

版本要求

Elasticsearch-PHP BranchPHP Version
7.0>= 7.1.0
6.0>= 7.0.0
5.0>= 5.6.6
2.0>= 5.4.0
0.4, 1.0>= 5.3.9

安装ES-PHP

vim composer.json
# 添加
{
    "require":{
        "elasticsearch/elasticsearch":"^7.0"
    }
}
# 下载
composer install  

ID 查询

<?php

#包含自动加载文件
require 'vendor/autoload.php';
#使用elasticsearch-php
use Elasticsearch\ClientBuilder;
# 请求参数
$params = [
    "index" => "small_video",
    "type" => "_doc",
    "id" => 1
];
# 实例化ES
$client = ClientBuilder::create()
    ->setHosts(['127.0.0.1:8101'])
    ->build();
$result = $client->get($params);
print_r($result);

结果

# 执行文件
php index.php
# 显示结果
Array
(
    [_index] => small_video
    [_type] => _doc
    [_id] => 1
    [_version] => 2
    [_seq_no] => 18
    [_primary_term] => 3
    [found] => 1
    [_source] => Array
        (
            [name] => 二滑大魔王
            [content] => 你好啊
        )

)

Query 查询

# 懒癌又发作了 ...
cp index.php query.php
vim query.php

添加

<?php

#包含自动加载文件
require 'vendor/autoload.php';
#使用elasticsearch-php
use Elasticsearch\ClientBuilder;
# 请求参数
$params = [
    "index" => "small_video",
    "type" => "_doc",
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => '二了',
                'fields' => ['name','content']
            ]
        ]
    ]
];
# 实例化ES
$client = ClientBuilder::create()
    ->setHosts(['127.0.0.1:8101'])
    ->build();
# 注意!!!这里用的是 search 而不是 get 了
# $result = $client->get($params);
$result = $client->search($params);
print_r($result);

结果

Array
(
    [took] => 7
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => Array
                (
                    [value] => 4
                    [relation] => eq
                )

            [max_score] => 0.5897495
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => small_video
                            [_type] => _doc
                            [_id] => 5
                            [_score] => 0.5897495
                            [_source] => Array
                                (
                                    [name] => 今天星期二
                                    [content] => hello5
                                )

                        )

                    [1] => Array
                        (
                            [_index] => small_video
                            [_type] => _doc
                            [_id] => 4
                            [_score] => 0.5753642
                            [_source] => Array
                                (
                                    [name] => 123
                                    [content] => 二了吧唧的
                                )

                        )

                    [2] => Array
                        (
                            [_index] => small_video
                            [_type] => _doc
                            [_id] => 2
                            [_score] => 0.2876821
                            [_source] => Array
                                (
                                    [name] => 二滑小天使
                                    [content] => 你好啊
                                )

                        )

                    [3] => Array
                        (
                            [_index] => small_video
                            [_type] => _doc
                            [_id] => 1
                            [_score] => 0.2876821
                            [_source] => Array
                                (
                                    [name] => 二滑大魔王
                                    [content] => 你好啊
                                )

                        )

                )

        )

)

其他操作

  亲亲,这边建议您自己去github看文档,或者看别人的教程哦~

Last modification:February 18th, 2020 at 10:26 pm