How to build efficient filtering functionality with PHP and Elasticsearch

王林
Release: 2023-07-17 12:52:01
Original
1328 people have browsed it

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

  1. Install Java: Elasticsearch is written based on Java, so you need to install Java first. You can download the Java installation package from the official Java website, and then follow the installation wizard to install it.
  2. Download and unzip Elasticsearch: Download the Elasticsearch compressed package suitable for your operating system on the Elasticsearch official website, and then unzip it to the location you wish to install.
  3. Start Elasticsearch: Enter the decompression directory of Elasticsearch and execute the command bin/elasticsearch to start Elasticsearch.
  4. 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"
    }
    Copy after login

2. Use PHP and Elasticsearch for data filtering

  1. 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
    Copy after login
  2. Create a PHP file and import the Elasticsearch PHP client library:

    require 'vendor/autoload.php';
    use ElasticsearchClientBuilder;
    Copy after login
  3. Connect to Elasticsearch:

    $client = ClientBuilder::create()->build();
    Copy after login
  4. Create an index and mapping:

    $params = [
     'index' => 'my_index',
     'body' => [
         'mappings' => [
             'properties' => [
                 'name' => ['type' => 'text'],
                 'age' => ['type' => 'integer']
             ]
         ]
     ]
    ];
    $response = $client->indices()->create($params);
    Copy after login
  5. 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);
    Copy after login
  6. Filter the data :

    $params = [
     'index' => 'my_index',
     'body' => [
         'query' => [
             'bool' => [
                 'must' => [
                     ['match' => ['name' => 'John']]
                 ]
             ]
         ]
     ]
    ];
    $response = $client->search($params);
    print_r($response);
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!