Home  >  Article  >  Backend Development  >  Fuzzy search and semantic search implementation based on Elasticsearch in PHP

Fuzzy search and semantic search implementation based on Elasticsearch in PHP

WBOY
WBOYOriginal
2023-10-03 08:37:55608browse

PHP 中基于 Elasticsearch 的模糊搜索与语义搜索实现

The implementation of fuzzy search and semantic search based on Elasticsearch in PHP requires specific code examples

In the modern Internet environment, search functions have become a must for various applications. One of the equipment functions. Traditional fuzzy search often can only perform simple matching based on keywords, but lacks an understanding of user intentions. Semantic search can better capture the user's intent and provide more precise search results. In this article, we will introduce how to use Elasticsearch to implement fuzzy search and semantic search in PHP, and give specific code examples.

Elasticsearch is an open source search engine based on Lucene, which provides rich search functions and efficient distributed search support. In PHP, we can use the official client library provided by Elasticsearch - Elasticsearch-PHP to interact with Elasticsearch.

First, we need to introduce the Elasticsearch-PHP library into the PHP project. It can be installed through Composer. You only need to add the following dependencies to the composer.json file in the project root directory:

{
  "require": {
    "elasticsearch/elasticsearch": "^7.0"
  }
}

and then execute the composer install command to install.

Next, we need to establish a connection with Elasticsearch. In PHP, this can be achieved through the following code:

<?php
require 'vendor/autoload.php';

$client = ElasticsearchClientBuilder::create()
            ->setHosts(['localhost:9200']) // 修改为 Elasticsearch 的地址和端口
            ->build();
?>

localhost:9200 in the above code is the address and port of the Elasticsearch service. By default, the Elasticsearch listening address is localhost and the port is 9200. If Elasticsearch is running on another machine or the port is different, it needs to be modified to the corresponding value.

Now, we can start to implement the fuzzy search function. The following is a simple example:

<?php
require 'vendor/autoload.php';

$client = ElasticsearchClientBuilder::create()
            ->setHosts(['localhost:9200'])
            ->build();

$params = [
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'title' => [
                    'query' => '搜索关键字',
                    'fuzziness' => 'AUTO',
                ],
            ],
        ],
    ],
];

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

foreach ($response['hits']['hits'] as $hit) {
    echo $hit['_source']['title'] . "
";
}
?>

my_index in the above code is an index in Elasticsearch, and title is a field name. We perform fuzzy search through the match query, and use the fuzziness parameter to set the degree of fuzzy matching. AUTO means to let Elasticsearch automatically determine the degree of fuzzy matching.

Of course, if you need more complex search functions, you can also use other query types provided by Elasticsearch, such as multi_match, bool, etc. For specific usage methods, please refer to the official documentation of Elasticsearch.

Next, we will introduce how to implement the semantic search function. In order to implement semantic search, we need to use a plug-in called Elasticsearch-Elasticsearch Elasticsearch-Elasticsearch-DSL. This plug-in provides a DSL (Domain Specific Language) that can build complex queries through PHP.

First, we need to modify the composer.json file and add the following dependencies:

{
  "require": {
    "elasticsearch/elasticsearch": "^7.0",
    "elastic/elasticsearch-dsl": "^8.0"
  }
}

Then execute the composer install command to install.

Next, we can use Elasticsearch-Elasticsearch-DSL to build semantic queries. The following is a simple example:

<?php
require 'vendor/autoload.php';

use ElasticsearchClientBuilder;
use ElasticsearchDSLSearch;

$client = ClientBuilder::create()
            ->setHosts(['localhost:9200'])
            ->build();

$search = new Search();
$search->addQuery(
    (new ElasticsearchElasticsearchDSLQueryMultiMatchQuery('搜索关键词', ['title', 'content']))
        ->setFuzziness('AUTO')
);

$params = [
    'index' => 'my_index',
    'body'  => $search->toArray(),
];

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

foreach ($response['hits']['hits'] as $hit) {
    echo $hit['_source']['title'] . "
";
}
?>

title and content in the above code are field names in Elasticsearch, which we build through MultiMatchQuery Create a multi-field query and set the degree of fuzzy matching.

Through the above code examples, we can use Elasticsearch to implement fuzzy search and semantic search functions in PHP. Of course, in actual business scenarios, the search function can be further optimized and expanded to achieve more accurate and efficient search results. Hope this article helps you!

The above is the detailed content of Fuzzy search and semantic search implementation based on Elasticsearch in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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