Dieser Artikel stellt hauptsächlich die Methode zur Verwendung von Elasticsearch in PHP vor. Jetzt kann ich ihn mit Ihnen teilen
Kursempfehlungen→ : "Elasticsearch Full Text Search Practical Combat" (Praktisches Video)
Aus dem Kurs "Ten Million Level Data Concurrency Solution (Theorie + praktischer Kampf)"
Mit Elasticsearch in PHP
composer require elasticsearch/elasticsearch
lädt automatisch die entsprechende Version! Mein PHP ist 5.6, es lädt automatisch die Elasticsearch-Version 5.3!
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
Einfache Verwendung
<?php class MyElasticSearch { private $es; // 构造函数 public function __construct() { include('../vendor/autoload.php'); $params = array( '127.0.0.1:9200' ); $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build(); } public function search() { $params = [ 'index' => 'megacorp', 'type' => 'employee', 'body' => [ 'query' => [ 'constant_score' => [ //非评分模式执行 'filter' => [ //过滤器,不会计算相关度,速度快 'term' => [ //精确查找,不支持多个条件 'about' => '谭' ] ] ] ] ] ]; $res = $this->es->search($params); print_r($res); } }
<?php require "./MyElasticSearch.php"; $es = new MyElasticSearch(); $es->search();
Ausführungsergebnisse
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] => 英雄联盟 ) ) ) ) ) )
Hier sind einige offizielle Beispiele:
require '../vendor/autoload.php'; use Elasticsearch\ClientBuilder; $client = ClientBuilder::create()->build();
Konfiguration hinzufügen
$hosts = [ '127.0.01:9200', // IP + Port ]; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts ->build(); // Build the client object
oder
$hosts = [ '127.0.01:9200', // IP + Port ]; $clientBuilder = ClientBuilder::create(); // Instantiate a new ClientBuilder $clientBuilder->setHosts($hosts); // Set the hosts $client = $clientBuilder->build(); // Build the client object
// Index 一个文档 $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'] ]; $response = $client->index($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->get($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $response = $client->search($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); print_r($response);
Die Ergebnisse sind wie folgt
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 = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response);
$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); print_r($response);
Das Obige ist der gesamte Inhalt dieses Artikels Für jedermanns Studie. Weitere verwandte Inhalte finden Sie auf der chinesischen PHP-Website!
Verwandte Empfehlungen:
Stapel der PHP-Datenstrukturgrundlage
PHP-Methoden und Parameterkommentare für den Betrieb von Beanstalkd
Das obige ist der detaillierte Inhalt vonSo verwenden Sie Elasticsearch in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!