PHP で Elasticsearch を使用する方法

不言
リリース: 2023-04-02 19:28:01
オリジナル
8910 人が閲覧しました

この記事では、PHP で Elasticsearch を使用する方法を中心に紹介します。これは、一定の参考価値があります。今回は、それを皆さんに共有します。必要な友人は、それを参照してください。

コースの推奨事項→ : 「Elasticsearch 全文検索の実践」 (実践ビデオ)

コースから 「1000 万レベルのデータ同時実行ソリューション (理論的実践)」

PHP で Elasticsearch を使用すると、適切なバージョンが自動的にロードされます。私のphpは5.6ですが、5.3 elasticsearchバージョンが自動的にロードされます。

composer require elasticsearch/elasticsearch
ログイン後にコピー

簡単な使い方

Using version ^5.3 for elasticsearch/elasticsearch
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing react/promise (v2.7.0): Downloading (100%)         
  - Installing guzzlehttp/streams (3.0.0): Downloading (100%)         
  - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%)         
  - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%)         
Writing lock file
Generating autoload files
ログイン後にコピー
<?php

class MyElasticSearch
{
    private $es;
    // 构造函数
    public function __construct()
    {
        include(&#39;../vendor/autoload.php&#39;);
        $params = array(
            &#39;127.0.0.1:9200&#39;
        );
        $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();
    }

    public function search() {
        $params = [
            &#39;index&#39; => &#39;megacorp&#39;,
            &#39;type&#39; => &#39;employee&#39;,
            &#39;body&#39; => [
                &#39;query&#39; => [
                    &#39;constant_score&#39; => [ //非评分模式执行
                        &#39;filter&#39; => [ //过滤器,不会计算相关度,速度快
                            &#39;term&#39; => [ //精确查找,不支持多个条件
                                &#39;about&#39; => &#39;谭&#39;
                            ]
                        ]

                    ]
                ]
            ]
        ];

        $res = $this->es->search($params);

        print_r($res);
    }
}
ログイン後にコピー

実行結果

<?php
require "./MyElasticSearch.php";

$es = new MyElasticSearch();

$es->search();
ログイン後にコピー

以下は正式な例です:

初期化

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

    [hits] => Array
        (
            [total] => 1
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 3
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => 李
                                    [last_name] => 四
                                    [age] => 24
                                    [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。
                                    [interests] => Array
                                        (
                                            [0] => 英雄联盟
                                        )

                                )

                        )

                )

        )

)
ログイン後にコピー

設定の追加

require &#39;../vendor/autoload.php&#39;;
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
ログイン後にコピー

または

$hosts = [
    &#39;127.0.01:9200&#39;,         // IP + Port
];

$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
->setHosts($hosts)      // Set the hosts
->build();              // Build the client object
ログイン後にコピー

ドキュメントの挿入

$hosts = [
    &#39;127.0.01:9200&#39;,         // IP + Port
];

$clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder
$clientBuilder->setHosts($hosts);           // Set the hosts
$client = $clientBuilder->build();          // Build the client object
ログイン後にコピー

ドキュメントの取得

// Index 一个文档
$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;,
    &#39;body&#39; => [&#39;testField&#39; => &#39;abc&#39;]
];

$response = $client->index($params);
print_r($response);
ログイン後にコピー

ドキュメントのクエリ

$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;
];

$response = $client->get($params);
print_r($response);
ログイン後にコピー

ドキュメントの削除

$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;body&#39; => [
        &#39;query&#39; => [
            &#39;match&#39; => [
                &#39;testField&#39; => &#39;abc&#39;
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);
ログイン後にコピー

結果は次のとおりです

$params = [
    &#39;index&#39; => &#39;my_index&#39;,
    &#39;type&#39; => &#39;my_type&#39;,
    &#39;id&#39; => &#39;my_id&#39;
];

$response = $client->delete($params);
print_r($response);
ログイン後にコピー

インデックスの削除

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 3
    [result] => deleted
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 2
    [_primary_term] => 1
)
ログイン後にコピー

インデックスの作成

$deleteParams = [
    &#39;index&#39; => &#39;my_index&#39;
];
$response = $client->indices()->delete($deleteParams);
print_r($response);
ログイン後にコピー
以上がこの記事の全内容です。その他の関連コンテンツについては、PHP 中国語 Web サイトに注目してください。

関連する推奨事項:

PHP データ構造の基本スタック


Beanstalkd を操作するための PHP メソッドとパラメーターのコメント

以上がPHP で Elasticsearch を使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!