Using Elasticsearch in PHP to implement distributed search engine
Introduction:
Distributed search engine is a very important part of modern Internet applications. It can achieve Fast full-text retrieval, efficient data search and sorting. Elasticsearch is an open source distributed search engine based on Lucene, providing powerful search and analysis capabilities. This article will introduce how to use Elasticsearch in PHP to implement a simple distributed search engine and provide specific code examples.
Prerequisites:
Before you begin, make sure you have installed Elasticsearch and PHP environments and successfully connected to Elasticsearch. You can download and install Elasticsearch through the official website of Elasticsearch (https://www.elastic.co/downloads/elasticsearch), and install the PHP client of Elasticsearch through Composer (https://www.elastic.co/guide/en /elasticsearch/client/php-api/current/introduction.html#_installation_2).
Example background:
Suppose we have a blog system with multiple blog posts and tags in the system. We want to be able to search blog posts by keywords and sort them according to relevance. We use Elasticsearch to build a simple distributed search engine to implement full-text retrieval and sorting of blog articles.
Sample code:
The following is a simple sample code that shows how to use Elasticsearch's PHP client to implement a distributed search engine:
<?php require 'vendor/autoload.php'; // 设置Elasticsearch的连接信息 $hosts = [ [ 'host' => 'localhost', 'port' => 9200, 'scheme' => 'http', 'user' => 'username', // 可选的用户名 'pass' => 'password' // 可选的密码 ] ]; // 创建Elasticsearch客户端 $client = ElasticsearchClientBuilder::create() ->setHosts($hosts) ->build(); // 创建索引(博客文章) $params = [ 'index' => 'blog', 'body' => [ 'settings' => [ 'number_of_shards' => 2, // 分片数 'number_of_replicas' => 1 // 副本数 ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'standard' ], 'content' => [ 'type' => 'text', 'analyzer' => 'standard' ], 'tags' => [ 'type' => 'keyword' ] ] ] ] ]; $response = $client->indices()->create($params); // 添加文档(博客文章) $params = [ 'index' => 'blog', 'id' => 1, 'body' => [ 'title' => 'PHP入门教程', 'content' => '这是一篇PHP入门教程的示例文章。', 'tags' => ['PHP', '教程'] ] ]; $response = $client->index($params); // 搜索文档(博客文章) $params = [ 'index' => 'blog', 'body' => [ 'query' => [ 'match' => [ 'content' => 'PHP' ] ], 'sort' => [ '_score' => 'desc' ] ] ]; $response = $client->search($params); // 解析搜索结果 foreach ($response['hits']['hits'] as $hit) { $source = $hit['_source']; echo '文章标题:' . $source['title'] . '<br>'; echo '文章内容:' . $source['content'] . '<br>'; echo '相关标签:' . implode(', ', $source['tags']) . '<br>'; echo '<hr>'; } ?>
Summary:
Introduction to this article Learn how to implement a simple distributed search engine using Elasticsearch in PHP. Through this example, we learned the basic operations of Elasticsearch, including creating indexes, adding documents, searching documents, and parsing search results. In actual applications, you can perform more complex operations as needed, such as custom analyzers, filters, and aggregations. I hope this article is helpful to you and inspires you to learn and apply Elasticsearch more deeply.
The above is the detailed content of Using Elasticsearch in PHP to implement a distributed search engine. For more information, please follow other related articles on the PHP Chinese website!