How to build an efficient filtering function through PHP and Elasticsearch
Introduction:
Elasticsearch is a very powerful solution when building an efficient search engine or a large-scale data filtering function. It is an open source tool based on a distributed search engine, which can quickly process massive data, provides rich query and filtering functions, and can also interact through PHP. This article will introduce how to build efficient filtering functions through PHP and Elasticsearch, including installing and configuring Elasticsearch, using PHP and Elasticsearch for data filtering, etc.
1. Install and configure Elasticsearch
bin/elasticsearch
to start Elasticsearch. Verify whether Elasticsearch starts successfully: Enter http://localhost:9200
in the browser. If information similar to the following is returned, it means that Elasticsearch has started successfully:
{ "name" : "Node-1", "cluster_name" : "elasticsearch", "cluster_uuid" : "42n3GoOpQkm7Bs6NOEXf0A", "version" : { "number" : "7.15.1", "build_flavor" : "default", "build_type" : "zip", "build_hash" : "unknown", "build_date" : "2022-10-26T18:07:47.101138203Z", "build_snapshot" : false, "lucene_version" : "8.10.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
2. Use PHP and Elasticsearch for data filtering
Install the Elasticsearch PHP client: Use Composer to install the PHP Elasticsearch client library. Execute the following command in the terminal:
composer require elasticsearch/elasticsearch
Create a PHP file and import the Elasticsearch PHP client library:
require 'vendor/autoload.php'; use ElasticsearchClientBuilder;
Connect to Elasticsearch:
$client = ClientBuilder::create()->build();
Create an index and mapping:
$params = [ 'index' => 'my_index', 'body' => [ 'mappings' => [ 'properties' => [ 'name' => ['type' => 'text'], 'age' => ['type' => 'integer'] ] ] ] ]; $response = $client->indices()->create($params);
Add some documents:
$params = [ 'index' => 'my_index', 'body' => [ ['index' => ['_index' => 'my_index']], ['name' => 'John Doe', 'age' => 25], ['index' => ['_index' => 'my_index']], ['name' => 'Jane Smith', 'age' => 30] ] ]; $response = $client->bulk($params);
Filter the data :
$params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ ['match' => ['name' => 'John']] ] ] ] ] ]; $response = $client->search($params); print_r($response);
Summary:
Through the above steps, we successfully installed and configured Elasticsearch, and built a simple filtering function using PHP and Elasticsearch. Through Elasticsearch's powerful query and filtering capabilities, we can quickly filter and search large-scale data based on various conditions. I hope this article was helpful and that you can apply this knowledge in future projects.
The above is the detailed content of How to build efficient filtering functionality with PHP and Elasticsearch. For more information, please follow other related articles on the PHP Chinese website!