Home > Article > Backend Development > PHP development: How to use Elasticsearch to implement full-text search
In modern web applications, the amount of data is getting larger and larger, but so are user expectations and access to data. Therefore, search technology is becoming increasingly important to meet user expectations and provide a better user experience. Full-text search is a powerful technology that can quickly index, search, and sort large amounts of data. In this regard, Elasticsearch is a leading open source search engine that provides many advanced features as well as high availability, easy scalability and other advantages.
In this article, we will introduce how to use Elasticsearch to achieve full-text retrieval through PHP. We will start with the environment setup, including the installation of Elasticsearch and PHP, and then provide an in-depth introduction from the three main aspects of indexing, search and analysis.
1. Environment Settings
First, install Elasticsearch locally or on the server. Elasticsearch can be downloaded and installed from the official website or through the package manager.
Secondly, install the PHP client of Elasticsearch through Composer, which is elasticsearch-php. It provides many convenient methods and classes to call the Elasticsearch API.
composer require elasticsearch/elasticsearch
After the installation is complete, configure the following in the PHP file:
require 'vendor/autoload.php';
$client = ElasticsearchClientBuilder::create()->build();
In this way, a client is created that communicates with the Elasticsearch server.
2. Index
In Elasticsearch, the index is a data collection used to store and quickly find data. We can insert data into the index using elasticsearch-php's API.
First, we need to create a new index. We use the following code to create a type named "my_type" in the index named "my_index".
$params = [
'index' => 'my_index', 'body' => [ 'mappings' => [ 'my_type' => [ 'properties' => [ 'title' => ['type' => 'text'], 'body' => ['type' => 'text'], ] ] ] ]
];
$response = $client->indices()->create($params);
The "title" and "body" fields in the "properties" array are of type "text", which means they will be full-text indexed. In practice, we will set indexes and field types according to specific needs.
In this way, we successfully created an index named "my_index".
Insert the document into the index using the following code:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'title' => 'PHP Elasticsearch 全文检索', 'body' => 'Elasticsearch 是一个领先的全文搜索引擎,其功能包括分布式、高可用、实时搜索和分析能力等。', ]
];
$response = $client->index($params);
Here, we insert a document with a title and body into the index.
If you need to update an existing document in the index, use the following code:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'id' => '1', 'body' => [ 'doc' => [ 'title' => '修改后的标题', 'body' => '修改后的正文内容', ] ]
];
$response = $client->update($params);
It should be noted that the ID of the document must be provided when updating.
If you need to delete an existing document, use the following code:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'id' => '1'
] ;
$response = $client->delete($params);
In this way, we have completed the creation, insertion, update and deletion of indexes and documents.
3. Search
Let’s take a look at how to use the elasticsearch-php API to search.
First, let’s execute a simple query:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'title' => 'PHP' ] ] ]
];
$response = $client->search($params);
In the above code, we execute a match query to query all documents in the index that contain the "PHP" keyword . The search results will be stored in the $response variable.
If you need to query multiple conditions, you can use bool query to combine multiple conditions:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ [ 'match' => [ 'title' => 'PHP' ] ], [ 'match' => [ 'body' => '搜索引擎' ] ] ] ] ] ]
];
$response = $client->search($params);
Here, we specify two query conditions that must be met at the same time through the must parameter .
If the amount of data is large, we can paginate the search results:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'from' => 0, 'size' => 10, 'query' => [ 'match' => [ 'title' => 'PHP' ] ] ]
];
$response = $client->search($params);
Specify the offset and size of the result set through the from and size parameters.
For more accurate search results, Elasticsearch calculates a relevance score for each document. Sorting by rating can be done with the following code:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'title' => 'PHP' ] ], 'sort' => [ '_score' => [ 'order' => 'desc' ] ] ]
];
$response = $client->search($params);
This way the query results will be sorted from high to low by relevance score.
4. Analysis
Elasticsearch supports a variety of powerful analysis and aggregation functions, which we can use to obtain deeper information about the data set.
The following code can obtain the top 10 words with the highest frequency of occurrence in the "title" field:
$params = [
'index' => 'my_index', 'type' => 'my_type', 'size' => 0, 'body' => [ 'aggs' => [ 'top_titles' => [ 'terms' => [ 'field' => 'title.keyword', 'size' => 10 ] ] ] ]
];
$response = $client->search($params);
Specify the size parameter to skip returning documents and only return aggregated results.
Elasticsearch also provides many powerful analyzers to analyze and process text. The following code demonstrates how to use the Chinese parser to process text:
$params = [
'index' => 'my_index', 'body' => [ 'settings' => [ 'analysis' => [ 'analyzer' => [ 'my_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_max_word' ] ] ] ] ]
];
$response = $client->indices()- >putSettings($params);
这里,我们为名为“my_analyzer”的分析器指定了“ik_max_word”分词器。
下面的代码可以使用这个分析器来分析文本:
$params = [
'index' => 'my_index', 'body' => [ 'query' => [ 'query_string' => [ 'query' => '搜索', 'analyzer' => 'my_analyzer', 'default_field' => 'title' ] ] ]
];
$response = $client->search($params);
这样,我们就可以使用中文分析器来分析中文文本了。
总结
在本文中,我向您介绍了如何使用elasticsearch-php的API来创建、添加、更新和删除索引和文档,以及如何使用搜索API来执行简单和复杂的查询。此外,我还介绍了使用聚合和分析器来处理数据的相关技术。
随着数据集规模的增加,Elasticsearch的重要性逐渐增加。只要您熟悉它的API,您就可以通过PHP轻松地利用其强大的搜索和分析能力来优化您的Web应用程序。
The above is the detailed content of PHP development: How to use Elasticsearch to implement full-text search. For more information, please follow other related articles on the PHP Chinese website!