随着互联网的快速发展和数据量的增加,如何高效地进行全文搜索已经成为了越来越多开发者面临的问题。elasticsearch是一种流行的全文搜索引擎,它能够快速处理大量的文本数据,并对其进行检索和分析,这使得它成为了很多web应用程序的首选工具。现在,thinkphp6也已经开始支持elasticsearch全文搜索操作,为开发者带来更加高效的搜索方案。
首先,我们需要在ThinkPHP6中安装Elasticsearch支持库,这可以通过在composer.json文件中添加以下代码来完成:
"require": {
1
"elasticsearch/elasticsearch": "^7.0"
}
然后在项目根目录下执行composer update命令,即可完成Elasticsearch支持库的安装。
接下来,我们将创建一个Elasticsearch服务提供者,将Elasticsearch客户端实例绑定到容器中,以便我们随时可以通过依赖注入在应用程序中使用它。在App/Provider目录下,创建ElasticsearchServiceProvider.php文件,代码如下:
namespace appprovider;
use ElasticsearchClientBuilder;
use thinkService;class ElasticsearchServiceProvider extends Service
{1
2
3
4
5
6
7
8
9
10
11
public function register()
{
// 获取config/elasticsearch.php配置
$config = $this->app->config->get(elasticsearch);
// 创建Elasticsearch客户端实例
$client = ClientBuilder::create()->setHosts($config[hosts])->build();
// 将Elasticsearch客户端实例绑定到容器中
$this->app->bind(elasticsearch, $client);
}
}
在本例中,我们首先从config/elasticsearch.php配置文件中获取Elasticsearch的主机地址,然后使用ClientBuilder类创建一个Elasticsearch客户端实例,并将其绑定到应用程序的容器中,这样我们就可以随时使用它来执行全文搜索操作了。
接下来,我们将演示如何在ThinkPHP6应用程序中进行全文搜索操作。在这里,我们创建一个ElasticsearchService服务类,该类包含了几个简单的方法来执行搜索操作。代码如下:
namespace appservice;
use ElasticsearchClient;
use ElasticsearchCommonExceptionsMissing404Exception;
use Throwable;class ElasticsearchService
{1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* 创建索引
*
* @param string $indexName 索引名称
* @return bool
*/
public function createIndex(string $indexName)
{
$params = [
index => $indexName,
body => [
mappings => [
properties => [
title => [
type => text
],
content => [
type => text
]
]
]
]
];
try {
$response = $this->client->indices()->create($params);
return true;
} catch (Throwable $e) {
throw new Exception(创建索引失败: . $e->getMessage());
}
}
/**
* 删除索引
*
* @param string $indexName 索引名称
* @return bool
*/
public function deleteIndex(string $indexName)
{
try {
$response = $this->client->indices()->delete([index => $indexName]);
return true;
} catch (Missing404Exception $e) {
return false;
} catch (Throwable $e) {
throw new Exception(删除索引失败: . $e->getMessage());
}
}
/**
* 添加文档
*
* @param string $indexName 索引名称
* @param string $id 文档ID
* @param array $data 文档数据
* @return bool
*/
public function indexDocument(string $indexName, string $id, array $data)
{
$params = [
index => $indexName,
id => $id,
body => $data
];
try {
$response = $this->client->index($params);
return true;
} catch (Throwable $e) {
throw new Exception(添加文档失败: . $e->getMessage());
}
}
/**
* 搜索文档
*
* @param string $indexName 索引名称
* @param string $query 查询字符串
* @return array
*/
public function searchDocuments(string $indexName, string $query)
{
$params = [
index => $indexName,
body => [
query => [
match => [
_all => $query
]
]
]
];
try {
$response = $this->client->search($params);
return $response[hits][hits];
} catch (Throwable $e) {
throw new Exception(搜索文档失败: . $e->getMessage());
}
}
}
在此服务类中,我们定义了四个方法:createIndex、deleteIndex、indexDocument和searchDocuments。这些方法封装了Elasticsearch API的调用,可以轻松地创建、删除索引,添加和搜索文档。
现在我们将演示如何使用这些方法。在这里,我们将创建一个测试页面,可以创建一个名为“articles”的索引,并添加一些文档,然后使用搜索框搜索文档。在App/controller目录下,创建一个ElasticsearchTestController.php文件,代码如下:
namespace appcontroller;
use appServiceElasticsearchService;
use thinkRequest;class ElasticsearchTestController extends BaseController
{1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
protected $elasticsearchService;
public function __construct(ElasticsearchService $elasticsearchService)
{
$this->elasticsearchService = $elasticsearchService;
}
public function index()
{
$this->elasticsearchService->createIndex(articles);
// 添加测试文档
$this->elasticsearchService->indexDocument(articles, 1, [
title => ThinkPHP,
content => ThinkPHP是一款优秀的PHP开发框架
]);
$this->elasticsearchService->indexDocument(articles, 2, [
title => Laravel,
content => Laravel是一款流行的PHP开发框架
]);
$this->elasticsearchService->indexDocument(articles, 3, [
title => Symfony,
content => Symfony是一款PHP开发框架
]);
// 搜索框
$search = Request::instance()->get(search, );
// 搜索结果
$results = $this->elasticsearchService->searchDocuments(articles, $search);
// 返回搜索结果
return $this->fetch(index, [
results => $results
]);
}
}
在此控制器中,我们注入了ElasticsearchService服务,并在index方法中调用了createIndex、indexDocument和searchDocuments方法来创建索引、添加文档和执行搜索操作。搜索框和搜索结果也被包含在了index方法中。
至此,我们已经完成了在ThinkPHP6应用程序中使用Elasticsearch进行全文搜索操作的演示。值得注意的是,本例仅仅是一个简单的演示用例,实际项目中需要更加细致地进行索引设计和文档管理,以确保搜索效率和搜索结果的准确性。
总的来说,随着Elasticsearch的广泛应用,它已经成为了Web应用程序中一种非常流行和高效的全文搜索引擎。在ThinkPHP6中,通过使用Elasticsearch支持库和Elasticsearch API,我们可以轻松地进行全文搜索操作。
以上就是ThinkPHP6中如何进行Elasticsearch全文搜索操作?的详细内容,更多请关注php中文网其它相关文章!