Elasticsearch 6.4.2 的安装和使用
一、安装
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://www.jianshu.com/p/add1f1d57241
二、PHP连接
1)客户端版本
composer.json
{
"require": {
"elasticsearch/elasticsearch": "~6.5.0"
}
}
2)官方文档地址
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.5.x/index.html
三、基本概念
index 相当于 数据库
type 相当于 数据表
document 相当于 一行数据
id document的唯一标识
查询关键字:
must 与
must not 排除
should 或
must 查询内容必须出现在匹配的文档中,并将有助于得分;
must_not 查询内容不能出现在匹配的文档中;
filter 查询内容必须出现在匹配的文档中,是对匹配的结果过滤,在过滤器上下文中执行,意味着评分被忽略;
should 查询内容应该出现在匹配的文档中,至少有一个;
四、操作
1)连接
common.php
<?php require 'vendor/autoload.php'; $client = ElasticsearchClientBuilder::create()->setHosts(['192.168.1.3:9200'])->build();
2)基本操作
<?php require 'common.php'; function add(){ //插入数据 $params = array( 'index' => 'website', 'type' => 'blog', 'id' => 7, 'body' => array( 'title' => '111ElasticSearch-PHP之使用二', 'content' => '有关于ElasticSearch在PHP下的扩展使用方法之谈', 'create_time' => '2016-11-18 08:00:00', ), ); global $client; $resp = $client->index($params); $params = array( 'index' => 'website', 'type' => 'blog', 'id' => 6, 'body' => array( 'title' => '222ElasticSearch-PHP之使用二', 'content' => '有关于ElasticSearch在PHP下的扩展使用方法之谈', 'create_time' => '2016-11-18 08:00:00', ), ); $res = $client->index($params); } function query(){ $params = [ 'index' => 'website', 'type' => 'blog', 'id' => 7, ]; global $client; $res = $client->get($params); //获取指定的文档 $res = $client->getSource($params); //获取指定的文档 数据 print_r($res); } function del(){ $params = [ 'index' => 'website', 'type' => 'blog', 'id' => 7, ]; global $client; $res = $client->delete($params); //删除指定的文档 print_r($res); } function update(){ $params = [ 'index' => 'website', 'type' => 'blog', 'id' => 6, 'body' => [ 'doc' => [ 'age' => 150 ] ] ]; global $client; $res = $client->update($params); //更新指定的文档 print_r($res); } function search(){ $params = [ 'index' => 'website', 'type' => 'blog', 'body' => [ 'query' => [ 'match' => [ 'age' => '150' ] ], 'from' => '0', 'size' => '200', 'sort' => [ 'age' => 'desc' //对age字段进行降序排序 ] ] ]; global $client; $res = $client->search($params); //搜索 指定的文档 print_r($res); } function index(){ $params = [ 'index' => 'website', 'client' => [ 'ignore' => 404 ] ]; global $client; //$res = $client->indices()->delete($params); //删除库索引 //$res = $client->indices()->getSettings($params);//获取库索引设置信息 $res = $client->indices()->exists($params); //检测库是否存在 $res = $client->indices()->getMapping($params); //获取mapping信息 print_r($res); } //add(); // query(); //del(); // update(); // search(); index();
3)复杂查询
<?php require 'common.php'; //搜索某一字段 function search1(){ $params = [ 'index' => 'website', 'type' => 'blog', 'body' => [ 'query' => [ 'match' => [ 'content' => '方法' ] ], 'from' => '0', 'size' => '200' ] ]; global $client; $res = $client->search($params); //搜索 指定的文档 print_r($res); } //搜索多个字段 //query bool must 是 and 查询 function search2(){ $params = [ 'index' => 'website', 'type' => 'blog', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ 'match' => [ 'title' => '666' ], 'match' => [ 'content' => '方法之谈' ] ] ] ], 'from' => '0', 'size' => '200' ] ]; global $client; $res = $client->search($params); //搜索 指定的文档 print_r($res); } //search1(); search2();
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10977410.html
转载请著名出处!谢谢~~