1、本篇文章目的
- 快速入门 php 开发 ElasticSearch
- 使用中文分词器 ik
- 创建添加文章的索引文档
2、快速入门
2.1、索引的管理
添加
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index'
];
// 创建一个简单的索引
try {
$response = $client->indices()->create($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
查看
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index'
];
// 获取指定索引的索引信息
try {
$response = $client->indices()->get($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
删除
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index'
];
// 删除索引
try {
$response = $client->indices()->delete($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
添加类型/映射,指定属性使用中文分词器
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index',
'type' => 'person',
'body' => [
'person' => [ // 分组名称
'properties' => [ // 指定属性
'user' => [ // user属性
'type' => 'text', // user属性数据类型
'analyzer' => 'ik_max_word', // 文本的分词器 这里是中文分词器
'search_analyzer' => 'ik_max_word', // 搜索词的分词器 这里是中文分词器
],
'title' => [ // title属性
'type' => 'text', // title属性数据类型
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word',
],
'desc' => [ // desc属性
'type' => 'text', // desc属性数据类型
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word',
]
]
]
]
];
try {
$response = $client->indices()->putMapping($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
2.2、文档的管理
添加
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
添加一个文档
$params = [
'index' => 'my_index',
'type' => 'person',
'id' => 1, // 可不指定,自动生成
'body' => [
'user' => '张三',
'title' => '工程师',
'desc' => '数据库管理'
]
];
try {
$response = $client->index($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
更新
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 更新文档
$params = [
'index' => 'my_index',
'type' => 'person',
'id' => 1,
'body' => [
'doc' => [
'desc' => 'DBA'
]
]
];
try {
$response = $client->update($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
查看
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 获取文档
$params = [
'index' => 'my_index',
'type' => 'person',
'id' => 1
];
try {
$response = $client->get($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
搜索
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 搜索文档
$params = [
'index' => 'my_index',
'type' => 'person',
'body' => [
'query' => [
'match' => [
'desc' => '数据库'
]
]
]
];
try {
$response = $client->search($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
删除
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 删除文档
$params = [
'index' => 'my_index',
'type' => 'person',
'id' => 1
];
try {
$response = $client->delete($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
3、添加文章的索引文档
在这里,我们假设一种场景:蜂融网资讯站,该站要提供整站搜索功能,输入某关键词搜索出来,所有相关的文章信息。
根据使用场景,分析如下:我们要使用 ElasticSearch 将所有站内文章添加到索引文档中,并使用中文分词器,对文章内容进行分词索引。
通过下面,我们一步步创建一个文章索引文档库。
创建文章索引及类型
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 创建文章索引及类型
$params = [
'index' => 'article',
'body' => [
'mappings' => [
'news' => [
'properties' => [
'id' => [ // 文章ID
'type' => 'integer'
],
'title' => [ // 文章标题
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word'
],
'description' => [ // 文章简述
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word'
],
'content' => [ // 文章内容
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word'
]
]
]
]
]
];
try {
$client->indices()->create($params);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
添加文章到索引文档
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 从数据库中读出来的文章信息,用于初始化 elasticsearch 文章索引文档
$articles = [
[
'id' => 1,
'title' => '注册与登录分步演示',
'description' => '蜂融网如何注册?蜂融网如何登录?',
'content' => '<span style="font-size:18px;"><strong>如何注册:</strong></span><br /><span> </span><br /><span>1、鼠标点击蜂融网首页右上角的“注册”按钮。</span><br /><p>2、填写用户名、密码、验证码等基本信息后,然后点击“注册”按钮。</p><br /><span>3、填写真实姓名和身份证号,请确保填写正确,否则会影响您的提现。</span><br />'
],
[
'id' => 2,
'title' => '蜂融网是做什么的网站?',
'description' => '蜂融网是一家网金融信息服务平台,提供互联网金融信息服务,于2015年5月20日正式上线,向蜂融网注册会员提供高安全、高收益、有保障、高透明的债权投资项目。蜂融网发布的项目均为经过严格的线下审核,由原债务人(核心企业)提供100%本息保障。蜂融网通过网络,对有融资需求的借款企业与有投资需求的蜂融网会员提供一个投资理财的P2P网络理财平台,为蜂融网会员提供低风险、低门槛、高收益的投资渠道。',
'content' => '蜂融网是一家互联网金融信息服务平台,提供互联网金融信息服务,于2015年5月20日正式上线,向蜂融网注册会员提供高安全、高收益、有保障、高透明的债权投资项目。蜂融网发布的项目均为经过严格的线下审核,由原债务人(核心企业)提供100%本息保障。蜂融网通过网络,对有融资需求的借款企业与有投资需求的蜂融网会员提供一个投资理财的开放平台,为蜂融网会员提供低风险、<span>低门槛、</span>高收益的投资渠道。<br />'
],
[
'id' => 3,
'title' => '平台模式',
'description' => '蜂融网是供应链金融模式的网络理财模式,也就是优选大型集团公司,为其子公司、上下游关联企业提供整合金融服务。蜂融网要求借款企业提供足值资产抵押、并由集团公司(或集团公司所属小贷公司、担保公司)提供担保,以保障投资人的资金安全。',
'content' => '<p><span style="font-size:14px;font-family:\'Microsoft YaHei\';color:#000000;"><strong>供应链金融 集团公司项目</strong></span> </p><span><span style="font-size:14px;font-family:\'Microsoft YaHei\';color:#000000;">有担保有抵押,值得信赖</span></span><br /><br /><span style="font-size:14px;font-family:\'Microsoft YaHei\';color:#000000;">蜂融网平台优选大型集团公司,为其子公司、上下游关联企业提供整合金融服务。蜂融网要求借款企业提供足值资产抵押、并由集团公司(或集团公司所属小贷公司、担保公司)提供担保</span><span style="font-size:14px;font-family:\'Microsoft YaHei\';color:#000000;">,以保障投资人的利益。<span style="font-family:\'Microsoft YaHei\';font-size:14px;line-height:21px;">投资周期一般为1-12个月,</span>可获得8%-16%的预期回报,是中短期投资的明智选择。</span><br />'
]
];
foreach ($articles as $key => $value) {
$params['body'][] = [
'index' => [
'_index' => 'article',
'_type' => 'news',
'_id' => $key + 1,
]
];
$params['body'][] = [
'id' => $value['id'],
'title' => strip_tags($value['title']),
'description' => strip_tags($value['description']),
'content' => strip_tags($value['content'])
];
}
try {
$client->bulk($params);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
至此,我们就索引了3篇文章到 elasticsearch 索引文档中。我们可能通过搜索接口,搜索指定关键词,就 可以得到文章信息了。下面是搜索“集团公司”的一个例子:
<?php
require_once './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'elasticsearch:9200',
];
// 实例化一个客户端
$client = ClientBuilder::create()->setHosts($hosts)->build();
// 搜索文档
$params = [
'index' => 'article',
'type' => 'news',
'body' => [
'query' => [
'match' => [
'content' => '集团公司'
]
]
]
];
try {
$response = $client->search($params);
print_r($response);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage();
}
下面是搜索结果
Array
(
[took] => 33
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => 1
[max_score] => 1.5253408
[hits] => Array
(
[0] => Array
(
[_index] => article
[_type] => news
[_id] => 3
[_score] => 1.5253408
[_source] => Array
(
[id] => 3
[title] => 平台模式
[description] => 蜂融网是供应链金融模式的网络理财模式,也就是优选大型集团公司,为其子公司、上下游关联企业提供整合金融服务。蜂融网要求借款企业提供足值资产抵押、并由集团公司(或集团公司所属小贷公司、担保公司)提供担保,以保障投资人的资金安全。
[content] => 供应链金融 集团公司项目 有担保有抵押,值得信赖蜂融网平台优选大型集团公司,为其子公司、上下游关联企业提供整合金融服务。蜂融网要求借款企业提供足值资产抵押、并由集团公司(或集团公司所属小贷公司、担保公司)提供担保,以保障投资人的利益。投资周期一般为1-12个月,可获得8%-16%的预期回报,是中短期投资的明智选择。
)
)
)
)
)