下面文章中的方法和其它未说明的内容请前往此文档进行查看:Elasticsearch在PHP中的使用
在PHP中使用ELasticsearch查询时,怎样对内容进行检索,怎样将不同的条件像if一样进行and或or的组合查询,这篇文章就来说说这些事。
先来看几个查询搜索的例子:
单词或多词查询,多个词之间用空格间隔,默认or组合检索,即只要有一个符合就会被检索出来。
//单(多)词查询 $search_where = [ 'match' => [ 'title' => [ 'query' => '删库 别烦我',//检索内容,多个词使用空格隔开 ] ] ]; //或者:精简写法(当无其他参数时,推荐词用法) $search_where = [ 'match' => [ 'title' =>'删库 别烦我',//检索内容,多个词使用空格隔开 ] ]; $data = $ES->search_doc($search_where, [], 0, 10);
多词查询,为了提高查询精度想要多个条件同时满足,需要用and进行组合检索,因为使用operator操作符,所以不能在使用精简写法。
//单(多)词查询,提高精度 $search_where = [ 'match' => [ 'title' => [ 'query' => '删库 别烦我',//检索内容,多个词使用空格隔开 "operator": "and" ] ] ]; $data = $ES->search_doc($search_where, [], 0, 10);
多词查询,为了控制精度,需要引入minimum_should_match操作符。minimum_should_match 操作符指的是最小匹配参数,这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数,因为我们无法控制用户搜索时输入的单词数量。
$search_where = [ 'match' => [ 'title' => [ 'query' => '区别 推荐 排序', "minimum_should_match"=>"70%" //在此三个词的示例中, 75% 会自动被截断成 66.6% ,即至少匹配三个里面的两个词。 ] ] ]; $data = $ES->search_doc($search_where, [], 0, 10);
然后来一个复杂的组合查询:bool过滤查询。
此例子查询返回title字段中带有php词项,但不带有Elasticsearch词项的结果,且结果如果还满足mysql词项或者tp词项,那么_score的值将会更高。
bool 查询会为每个文档计算相关度评分 _score ,再将所有匹配的 must 和 should 语句的分数 _score 求和,最后除以 must 和 should 语句的总数。
must_not 语句不会影响评分;它的作用只是将不相关的文档排除。
$search_where = [ 'bool' => [ 'must' => [ 'match' => [ 'title' => [ 'query' => 'php', ] ] ], 'must_not' => [ 'match' => [ 'title' => [ 'query' => 'Elasticsearch', ] ] ], 'should' => [ 'match' => [ 'title' => [ 'query' => 'mysql tp', ] ] ] ] ]; $data = $ES->search_doc($search_where, [], 0, 10);
然后先总结一下bool过滤查询的几种类型:
Bool查询现在包括四种子句:must、filter、should、must_not
①must:
返回的文档必须满足must子句的条件,并且参与计算分值。
②filter:
返回的文档必须满足filter子句的条件,但不会像must一样,参与计算分值。
③should:
返回的文档可能满足should子句的条件。在一个bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。minimum_should_match参数定义了至少满足几个子句。
④must_not:
返回的文档必须不满足must_not的定义的条件。
boost操作符的使用
$search_where = [ 'bool' => [ 'should' => [ 'match' => [ 'title' => [ 'query' => 'php', ] ], 'match' => [ 'description' => [ 'query' => 'php', 'boost'=>9//权重更大 ] ] ] ] ]; $data = $ES->search_doc($search_where, [], 0, 10);
有关boost使用的补充:
当未定义boost时默认为1,。
boost参数是用来提升一个语句的相对权重,当大于1时是提高权重,当处于0-1时是讲定权重,但这种提升或者降低并不是线性的,换句话说,如果一个boost的值为2,并不能获得两倍的_score。
简单来说,更高的boost值为我们带来更高的_score。
下面文章中的方法和其它未说明的内容请前往此文档进行查看:Elasticsearch在PHP中的使用
在PHP中使用ELasticsearch查询时,怎样对查询结果进行排序,我分为了三种情况:
情况一:
当查询中sort条件为空时,Elasticsearch会自动根据检索条件匹配相似度,生成一个相似度的数值_score,而查询结果会按这个_score进行倒序排列。
$data = $ES->search_doc($search_where, '', 0, 10);
情况二:
当只传字段名时,Elasticsearch会按检索结果中此字段的值进行正序排列。
$data = $ES->search_doc($search_where, 'age', 0, 10);
情况三:
组合排序,一个或多个字段进行指定排序时,类似于数据库中order的作用。
用法(排序规则,desc倒序排列,asc正序排列):
['字段名'=>['order'=>'排序规则'],'字段名2'=>['order'=>'排序规则']]
注意:当未指定_score字段时,Elasticsearch不会计算检索条件的匹配相似程度,查询结果中的_score的值为null。
$data = $ES->search_doc($search_where, ['age'=>['order'=>'desc'],'_score'=>['order'=>'desc'],], 0, 10);
Elasticsearch是一个基于java语言开发的分布式多用户的全文搜索引擎,在云计算中,能够达到实时搜索,稳定、可靠、快速,安装方便。
官方客户端在java、PHP、Python、.NET等其他语言都是可用的。
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。
首先安装java环境,因为Elasticsearch是基于java运行的。参考:java的安装。
下载Elasticsearch,建议最新版,其他版本我下载后出现问题,解决非常麻烦。
GitHub下载地址:https://github.com/elastic/elasticsearch
下载成功后打开根目录样式如下:
命令行启动Elasticsearch服务:
elasticsearch.bat
浏览器访问以下地址,如果如图显示则安装成功!
http://localhost:9200/
end
此工具是为了对Elasticsearch中的数据进行可视化管理,类似于Navicat。
安装使用参考查看:Elasticsearch-Head的安装。
Elasticsearch-analysis-ik中文分词器是为了将中文搜索条件进行分词搜索使用的。
安装使用参考查看:Elasticsearch-analysis-ik的安装。
1、首先下载composer
2、cmd进入项目根目录下
3、使用composer命令进行安装:
composer require elasticsearch/elasticsearch
4、end
use Elasticsearch\ClientBuilder; class Es extends BaseController { protected $client; public $index_name;//索引值,类似于数据库名 /** * Es constructor. * @param string $index_name 索引名称 */ public function __construct($index_name = "shx") { parent::__construct(); //给定默认值 $this->index_name = $index_name; //自动连接Elasticsearch服务器 $this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); } /** * ELasticsearch初次使用 */ public function index() { //删除原索引 //$this->delete_index(); //创建新索引 //$this->create_index(); //自定义内容,也可换成数据库内容 $docs = []; $docs[] = ['id' => 1, 'name' => '叶凡', 'content' => '我做的ui界面强无敌。', 'age' => 23]; $docs[] = ['id' => 2, 'name' => '秦昊', 'content' => '我的php代码无懈可击。', 'age' => 24]; $docs[] = ['id' => 3, 'name' => '萧炎', 'content' => 'C的生活,快乐每一天。', 'age' => 29]; $docs[] = ['id' => 4, 'name' => '林动', 'content' => '就没有我做不出的前端页面。', 'age' => 26]; $docs[] = ['id' => 5, 'name' => '唐三', 'content' => 'php是最好的语言。', 'age' => 21]; $docs[] = ['id' => 6, 'name' => '秦宇', 'content' => '别烦我,我正在敲bug呢!', 'age' => 25]; $docs[] = ['id' => 7, 'name' => '宋书航', 'content' => '为所欲为,不行就删库跑路', 'age' => 27]; foreach ($docs as $k => $v) { //添加文档 $this->add_doc($v['id'], $v); } //查看映射(整个index索引的相关信息,不包括数据) //$res = $this->get_mapping(); //搜索结果 $search_where = [ 'bool' => [ 'should' => [ [ 'match' => [ 'content' => [ 'query' => '删库 别烦我',//查询内容 'boost' => 3, // 权重大 ] ] ], [ 'match' => [ 'name' => [ 'query' => '删库 别烦我', 'boost' => 2, ] ] ], ], ], ]; //按照搜索条件,age倒序排列,每页10条数据,查看第一页的数据 //$data = $this->search_doc($search_where, ['age' => ['order' => 'desc']], 0, 10); //按照搜索条件,按照匹配度由高到低排序,每页2条数据,查看第一页的数据 $data = $this->search_doc($search_where, [], 0, 2); dump($data); } /** * 创建索引 * @return array|mixed */ public function create_index() { //给定索引名 $index_name = $this->index_name; // 只能创建一次 $params = [ 'index' => $index_name, 'body' => [ //定义映射中的一些设置 'settings' => [ 'number_of_shards' => 5,//数据分片数,默认为5,有的时候也会用3 'number_of_replicas' => 0//数据备份数,如果只有一台机器,则设置为0 ] ] ]; try { //完成创建索引的映射 return $this->client->indices()->create($params); } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg, true); return $msg; } } /** * 删除索引 * @return array */ public function delete_index() { //给定索引名 $index_name = $this->index_name; $params = ['index' => $index_name]; //删除指定索引,包括其中的数据映射等 $response = $this->client->indices()->delete($params); return $response; } /** * 查看映射 * @return array */ public function get_mapping() { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, ]; //查看指定索引下指定 $response = $this->client->indices()->getMapping($params); return $response; } /** * 添加文档,一次仅插入一条 * @param $id 此条数据索引id * @param $doc 此条数据具体内容,例:['name'=>'shx'] * @return array|callable */ public function add_doc($id, $doc) { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, 'id' => $id, 'body' => $doc ]; //添加一条文档 $response = $this->client->index($params); return $response; } /** * 判断文档存在 * @param $id 索引id * @return bool */ public function exists_doc($id) { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, 'id' => $id ]; //判断文档是否存在 $response = $this->client->exists($params); return $response; } /** * 获取文档 * @param $id 索引id * @return array|callable */ public function get_doc($id) { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, 'id' => $id ]; //获取指定id的文档内容 $response = $this->client->get($params); return $response; } /** * 更新文档,一次仅更新一次 * @param $id 需要更新的索引id * @param $update_data 更新内容,例:['name'=>'shxtest'] * @return array|callable */ public function update_doc($id, $update_data) { //给定索引名 $index_name = $this->index_name; // 可以灵活添加新字段,最好不要乱添加 $params = [ 'index' => $index_name, 'id' => $id, 'body' => [ 'doc' => $update_data ] ]; $response = $this->client->update($params); return $response; } /** * 删除文档 * @param $id 索引id * @return array|callable */ public function delete_doc($id) { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, 'id' => $id ]; $response = $this->client->delete($params); return $response; } /** * 查询文档 * @param $search_where 检索条件 * @param array $order 排序,此处如果传空数组,则会按相似度由高到低进行排序。例如:['age' => ['order' => 'desc']] * @param int $from 分页的页数,从0开始为第一页 * @param int $size 分页条数(每页显示多少条) * @return array|callable */ public function search_doc($search_where, $order, $from = 0, $size = 2) { //给定索引名 $index_name = $this->index_name; $params = [ 'index' => $index_name, 'body' => [ //检索条件 'query' => $search_where, //排序内容 'sort' => $order, //页数 'from' => $from, //每页条数 'size' => $size ] ]; $results = $this->client->search($params); return $results; } }
MySQL中的数据库就相当于Elasticsearch的index索引。
MySQL中的数据表就相当于Elasticsearch的type。
MySQL中的每行数据就相当于Elasticsearch的doument。
MySQL中的表字段就相当于Elasticsearch的Field。
MySQL中的视图就相当于Elasticsearch的Mapping。
注意:MySQL和Elasticsearch无任何直接关系,只是在此举例说明。
elasticsearch运行是基于java的,所以如果想要使用elasticsearch首先需要安装java环境。
如果安装最新版ES插件,请检查PHP版本,例如当前安装elasticsearch7.11,则PHP版本必须大于等于7.1。
首先说明下我安装时的环境是windows10。
安装node.js,下载与自己系统符合的版本进行安装:
安装完成后可在命令行中输入:node -v查看版本
下载Elasticsearch-Head
下载解压后的目录:
cmd命令行进入此目录,执行npm install,成功后执行npm run start:
服务开启成功后,在浏览器访问localhost:9100,一开始集群健康值可能会显示:未连接
如果显示未连接,则找到Elasticsearch安装目录(注意,不是Elasticsearch-head安装目录),打开config/elasticsearch.yml,在最下方增加以下代码:
http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"
保存后重启Elasticsearch和Elasticsearch-head。
end
首先说明下我安装时的环境是windows10。
下载Elasticsearch-analysis-ik前提:
重要提醒:Elasticsearch-analysis-ik的版本必须与Elasticsearch版本一致!!!
官方下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases/
下载完成后在Elasticsearch安装目录的plugins文件夹下新建ik文件夹,并将下载的文件解压到此文件夹:
然后重启Elasticsearch,可以使用Elasticsearch-Head中的复合查询测试下效果。
end