


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.
- Create an index
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".
- Add document data to the 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.
- Update Document
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.
- Delete Document
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.
- Simple Query
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.
- Multi-condition query
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 .
- Paging query
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.
- Sort by score
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.
- Aggregation
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.
- Analyzer
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
