Home > Backend Development > PHP Tutorial > How to implement multi-condition search using PHP and Elasticsearch

How to implement multi-condition search using PHP and Elasticsearch

WBOY
Release: 2023-07-18 21:14:01
Original
1207 people have browsed it

How to use PHP and Elasticsearch to implement multi-condition search

Introduction:
In Web development, the search function is an indispensable part. In order to provide more accurate and flexible search results, we usually need to implement multi-condition search. This article will introduce how to use PHP and Elasticsearch to implement multi-condition search. We will introduce the basic concepts of Elasticsearch and give code examples to help you understand and practice this process.

Part One: Overview of Elasticsearch
Elasticsearch is an open source distributed search and analysis engine that can quickly store, search and analyze large amounts of data. By using Elasticsearch, we can easily implement powerful search capabilities and provide high-performance search results.

Part 2: Indexing
Before searching, we need to index the data first. An index is a collection of documents, each consisting of a set of fields. In this example, we will use an index called "products" to store product information.

The PHP code to create the index is as follows:

require 'vendor/autoload.php';

$client = ElasticsearchClientBuilder::create()->build();

$params = [
    'index' => 'products',
    'body' => [
        'settings' => [
            'number_of_shards' => 1,
            'number_of_replicas' => 0
        ],
        'mappings' => [
            'properties' => [
                'name' => [
                    'type' => 'text',
                    'analyzer' => 'ik_max_word',
                ],
                'price' => [
                    'type' => 'integer'
                ],
                'category' => [
                    'type' => 'keyword'
                ]
            ]
        ]
    ]
];

$response = $client->indices()->create($params);
Copy after login

In the above code, we use the PHP client library of Elasticsearch. First, we set up a new client instance. Then, we define the settings and mapping information for the index, and create the index by calling the indices()->create() method.

Part 3: Adding Documents
After indexing, we need to add the actual data to Elasticsearch. Here is a sample code to add a product document:

$params = [
    'index' => 'products',
    'body' => [
        'name' => 'iPhone 12',
        'price' => 9999,
        'category' => '手机'
    ]
];

$response = $client->index($params);
Copy after login

In this code, we specify the index name and the document to be added. We just need to pass the document's fields and corresponding values ​​to the index() method.

Part 4: Perform a multi-criteria search
Once the data is successfully added to Elasticsearch, we can start performing a multi-criteria search. The following is a sample code for multi-condition search using PHP and Elasticsearch:

$params = [
    'index' => 'products',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    ['match' => ['name' => 'iPhone']],
                    ['range' => ['price' => ['gte' => 5000]]],
                    ['term' => ['category' => '手机']]
                ]
            ]
        ]
    ]
];

$response = $client->search($params);

$results = $response['hits']['hits'];
Copy after login

In this sample code, we first create a query structure, through match, range and term define multiple query conditions. Then, we pass the query parameters to Elasticsearch through the search() method and obtain the search results.

Finally, we store the search results in the $results array, which can be further processed and displayed as needed.

Conclusion:
By using PHP and Elasticsearch, we can implement multi-condition search functions and provide users with more accurate and flexible search results. In this article, we introduced the basic concepts of Elasticsearch and showed through code examples how to index, add documents, and perform multi-criteria searches. I hope this article can provide you with some useful information and guidance, allowing you to better understand and apply multi-criteria search technology.

The above is the detailed content of How to implement multi-condition search using PHP and Elasticsearch. For more information, please follow other related articles on the PHP Chinese website!

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