elasticSearch 查询工具类
<?php
namespace common\tools;
use Yii;
use yii\base\Model;
use Elasticsearch\ClientBuilder;
/**
* @desc elasticSearch 查询工具类
* @date 2019年11月11日14:23:29
* must should 的区别:
* must : 对于给定的搜索字符串,在搜索结果中必须包含改搜索字符串中包含的字符,filter条件无效 如:filter搜素有数据 字符串收缩无数据 最终会显示无数据
* should : 对于给定的搜索字符串,在搜索结果中不必须包含改搜索字符串中包含的字符 与filter有关 如:filter搜素有数据 字符串收缩无数据 最终会显示filter中的数据
*/
class Es
{
const ES_MATCH_OPERATOR_OR = 'or';
const ES_MATCH_OPERATOR_AND = 'and';
//const ES_MATCH_HIGHLIGHT_TAT_PRE = ['('];
const ES_MATCH_HIGHLIGHT_TAT_PRE = '(';
//const ES_MATCH_HIGHLIGHT_TAT_POST = [')'];
const ES_MATCH_HIGHLIGHT_TAT_POST = ')';
const ES_MATCH_SEARCH_MODEL_MATCH = 'match';
const ES_MULTI_MATCH_SEARCH_MODEL_MATCH = 'multi_match';
const ES_MATCH_SEARCH_MODEL_BOOL = 'bool';
private $_hosts = [];
protected $_index = '';
private $_client = null;
private $_params = [];
private $_settings = [];
private $_mappings = [];
private $_numberOfShards = 5;
private $_numberOfReplicas = 0;
private $_indices = null;
private $_response = [];
private $_searchField = null;
private $_searchSort = [];
protected $mappings = [];
//private $_highlightTag = ['pre' => self::ES_MATCH_HIGHLIGHT_TAT_PRE, 'post' => self::ES_MATCH_HIGHLIGHT_TAT_POST];
private $_highlightTag = ['pre' => [self::ES_MATCH_HIGHLIGHT_TAT_PRE], 'post' => [self::ES_MATCH_HIGHLIGHT_TAT_POST]];
private $_scrollExp = 30;
private $_scrollSize = 2;
private $_openScroll = false;
private $_scrollId = '';
private $_operator = self::ES_MATCH_OPERATOR_OR; // 匹配规则 可选 or and
private $_queryKeyword = null;
private $_searchModel = self::ES_MATCH_SEARCH_MODEL_BOOL;
private $_pageSize = 1;
public function __construct(array $config = [])
{
$config = Yii::$app->params;
if (isset($config['host'])) {
$this->_hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
} else {
$this->_hosts = ['127.0.0.1:9200'];
}
if (isset($config['index']) && is_string($config['index']) && !empty($config['index'])) $this->_index = $config['index'];
else {
throw new \Exception("索引名称不能为空", 90003);
}
$this->_params['index'] = $this->_index;
$this->_settings = [//配置
'number_of_shards' => $this->_numberOfShards,//主分片数
'number_of_replicas' => $this->_numberOfReplicas//主分片的副本数
];
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$this->_client = ClientBuilder::create()->setHosts($this->_hosts)->build();
$this->_indices = $this->_client->indices();
}
/**
* @desc 设置映射
*/
public function setMappings(array $mappings)
{
if (!$mappings) {
throw new \Exception("映射不能为空", 90003);
}
$this->_mappings = $mappings;
return $this->_mappings;
}
/**
* @desc 创建索引
*/
public function createIndex($index='')
{
if ($index) $this->_params['index'] = $this->_index = $index;
$this->_response[__FUNCTION__] = $this->_createIndex();
return $this->_response[__FUNCTION__];
}
/**
* @desc 创建索引
*/
private function _createIndex()
{
if (!$this->_mappings || !is_array($this->_mappings)) {
throw new \Exception("未指定mapping参数", 90001);
}
if (!$this->_index) {
throw new \Exception("未指定索引名称", 90001);
}
if (!$this->isIndexExists()) {
$this->_params['body'] = [
'settings' => $this->_settings,
'mappings' => ['properties' => $this->_mappings]
];
$response = $this->_indices->create($this->_params);
$this->_response[__FUNCTION__] = $response;
//print_r($respone);
if (!$response['acknowledged']) {
return false;
}
} else {
//throw new \Exception("索引名称已存在", 900011);
$this->deleteIndex($this->_index);//如果索引存在删除索引
$this->_params['body'] = [
'settings' => $this->_settings,
'mappings' => ['properties' => $this->_mappings]
];
$response = $this->_indices->create($this->_params);
$this->_response[__FUNCTION__] = $response;
//print_r($respone);
if (!$response['acknowledged']) {
return false;
}
//return false;
}
return true;
}
/**
* @desc 检查索引是否存在
*/
public function isIndexExists($index='')
{
if ($index) $this->_params['index'] = $this->_index = $index;
return $this->_indices->exists(['index' => $this->_params['index']]);
}
/**
* @desc 获取参数
*/
public function getParams()
{
return $this->_params;
}
/**
* @desc 根据索引获取所有入库数据
*/
public function getSearch($index='')
{
if ($index) $this->_params['index'] = $this->_index = $index;
try {
return $this->_client->search($this->_params);
}catch (\Exception $e) {
return false;
}
}
/**
* @各种查询类型
*/
public function Search($index='',$data = [])
{
if ($index) $this-