How to build a multi-criteria search function using Elasticsearch and PHP

WBOY
Release: 2023-07-08 09:20:01
Original
738 people have browsed it

How to use Elasticsearch and PHP to build multi-condition search functions

Introduction:
Elasticsearch is an open source distributed search and analysis engine that is often used to build powerful search functions. It enables fast and accurate searches and supports various types of queries. This article will introduce how to use Elasticsearch and PHP to build multi-condition search functions, and provide corresponding code examples.

  1. Install and configure Elasticsearch:
    First, we need to install and configure Elasticsearch. You can download Elasticsearch from the official website and follow the instructions to install it. After the installation is complete, start Elasticsearch using the command line interface.
  2. Install and configure the Elasticsearch PHP extension:
    Next, we need to install and configure the Elasticsearch PHP extension. Installing the Elasticsearch PHP extension is easy using Composer. Create a composer.json file in the root directory of the project and add the following content:
{
    "require": {
        "elasticsearch/elasticsearch": "^7.0"
    }
}
Copy after login

Then run the composer install command to install the Elasticsearch PHP extension.

  1. Connect to Elasticsearch:
    In the PHP code, we need to establish a connection with Elasticsearch. First, introduce the namespace of the Elasticsearch PHP extension and create an Elasticsearch client object:
<?php
require 'vendor/autoload.php';

use ElasticsearchClientBuilder;

$hosts = [
    'http://localhost:9200' // Elasticsearch的访问地址
];

$client = ClientBuilder::create()->setHosts($hosts)->build();
?>
Copy after login
  1. Create an index:
    Before performing a multi-condition search, we need to create an index, and add the data to the index. If you already have an existing index, you can skip this step.

First, define an index configuration and mapping. For example, we create an index called "articles" and define the "article" type and specify the types of the "title" and "content" fields:

<?php
$params = [
    'index' => 'articles',
    'body' => [
        'mappings' => [
            'properties' => [
                'title' => ['type' => 'text'],
                'content' => ['type' => 'text']
            ]
        ]
    ]
];

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

Then, add documents to the index. For example, we add a document titled "News":

<?php
$params = [
    'index' => 'articles',
    'body' => [
        'title' => '新闻',
        'content' => '这是一条新闻'
    ]
];

$response = $client->index($params);
?>
Copy after login
  1. Perform a multi-criteria search:
    Now, we can perform a multi-criteria search. First, define a query that includes all search criteria. For example, we want to search for documents whose title contains "News" and whose content contains "International":
<?php
$params = [
    'index' => 'articles',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    ['match' => ['title' => '新闻']],
                    ['match' => ['content' => '国际']]
                ]
            ]
        ]
    ]
];

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

Finally, iterate through the search results and print out the title and content of each document:

<?php
foreach ($response['hits']['hits'] as $hit) {
    echo '标题:' . $hit['_source']['title'] . '<br>';
    echo '内容:' . $hit['_source']['content'] . '<br>';
    echo '----------------<br>';
}
?>
Copy after login

Conclusion:
Through the above steps, we can use Elasticsearch and PHP to build a multi-condition search function. First, install and configure Elasticsearch and the corresponding PHP extension; then, connect to Elasticsearch and create an index; finally, perform multi-condition search and process the search results.

Note: In order to ensure the accuracy and performance of the search, it is recommended to optimize the index before using multi-condition search, such as setting up a suitable word segmenter, building an index, etc.

Reference materials:

  • Elasticsearch official documentation: https://www.elastic.co/guide/index.html
  • Elasticsearch PHP official documentation: https: //www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html

The above is the detailed content of How to build a multi-criteria search function using Elasticsearch and PHP. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!