How to implement multilingual search with PHP and Elasticsearch

王林
Release: 2023-07-17 08:14:02
Original
759 people have browsed it

How to implement multilingual search through PHP and Elasticsearch

In today's Internet environment, with the development of globalization, multilingual search has become one of the necessary functions for many websites. As a powerful open source search engine, Elasticsearch has efficient and flexible search capabilities. This article will introduce how to implement multi-language search through PHP and Elasticsearch, with code examples.

1. Preparation
Before starting, you need to ensure that the following environment has been installed:

  1. PHP environment: Make sure that PHP has been installed and can run normally.
  2. Elasticsearch: Make sure Elasticsearch has been installed and can start normally.

2. Establish index
Before performing multi-language search, you first need to create a suitable index and store the corresponding data in Elasticsearch. The following is a simple example:

<?php

require 'vendor/autoload.php';

use ElasticsearchClientBuilder;

$hosts = [
    'http://localhost:9200'
];

$client = ClientBuilder::create()
                ->setHosts($hosts)
                ->build();

$params = [
    'index' => 'my_index',
    'body'  => [
        'mappings' => [
            'properties' => [
                'title' => [
                    'type' => 'text',
                    'fields' => [
                        'zh' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word',
                            'search_analyzer' => 'ik_smart'
                        ],
                        'en' => [
                            'type' => 'text',
                            'analyzer' => 'english',
                            'search_analyzer' => 'english'
                        ]
                    ]
                ],
                'content' => [
                    'type' => 'text',
                    'fields' => [
                        'zh' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word',
                            'search_analyzer' => 'ik_smart'
                        ],
                        'en' => [
                            'type' => 'text',
                            'analyzer' => 'english',
                            'search_analyzer' => 'english'
                        ]
                    ]
                ]
            ]
        ]
    ]
];

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

In the above code, we use the PHP client of Elasticsearch to create an index named my_index, which contains a title and a content field, corresponding to the title of the article. and content. Two subfields 'zh' and 'en' are defined, which are used to store Chinese and English versions of data respectively, and the corresponding analyzers are specified.

3. Insert data
Next, we need to insert some data into the index for subsequent searches. The following is a simple example:

<?php

$doc = [
    'title' => [
        'zh' => '中文标题',
        'en' => 'English title'
    ],
    'content' => [
        'zh' => '中文内容',
        'en' => 'English content'
    ]
];

$params = [
    'index' => 'my_index',
    'id'    => '1',
    'body'  => $doc
];

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

In the above code, we create a document containing Chinese and English titles and content, and insert it into the index named my_index.

4. Search
After we have completed the establishment of the index and the insertion of data, we can start multi-language search. The following is a simple example:

<?php

$params = [
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'multi_match' => [
                'query' => '关键词',
                'fields' => ['title.zh', 'content.zh', 'title.en', 'content.en']
            ]
        ]
    ]
];

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

var_dump($response['hits']['hits']);
Copy after login

In the above code, we use multi-language multi-field search to search by specifying the fields and keywords to be searched. Search results are sorted by relevance and matching documents are returned.

5. Summary
It is not complicated to implement multi-language search through PHP and Elasticsearch. By building appropriate indexes and data insertion, combined with multi-language, multi-field searches, we can implement multi-language search capabilities quickly and accurately. Hope this article is helpful to you.

The above is the detailed content of How to implement multilingual search with 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!