<?php
namespace app\controller;
use app\BaseController;
use app\common\Elasticsearch;
class Esearch extends BaseController
{
public $elasticsearch;
/**
* 构造方法
*/
public function __construct()
{
$this->elasticsearch = new Elasticsearch();
}
public function index()
{
$index = 'person';
$type = 'person_';
//设置索引
$this->elasticsearch->setIndex($index,$type);
//判断索引是否存在,不存在则创建
if($this->elasticsearch->existsIndex())
{
$this->elasticsearch->createIndex();
}
//获取映射 如果为空则创建
$mapping = $this->elasticsearch->getMapping();
if(empty($mapping[$index]['mappings'][$type])){
$params = [
'properties' => [
'id' => [
'type' => 'long', // 整型
],
'name' => [
//5.x以上已经没有string类型。如果需要分词的话使用text,不需要分词使用keyword。
'type' => 'text', // 字符串型
],
'age' => [
'type' => 'long',
],
'hobby' => [
'type' => 'text',
],
'introduction' => [
'type' => 'text',
],
]
];
$this->elasticsearch->createMappings($params);
}
//添加文档
$data = [
'id' => 5,
'name' => '张三',
'introduction' => '我是张三',
'age' => 30,
'hobby' => '程序员',
];
// $rs = $this->elasticsearch->addDoc(4,$data);
// var_dump($rs);
$arr=[];
for ($i=6;$i< 20;$i++){
$arr[]=[
'id' => $i,
'name' => '张三_'. $i,
'introduction' => '我是张三_'. $i,
'age' => 30 + $i,
'hobby' => '程序员_'. $i,
];
}
$body=[];
foreach ($arr as $k=>$v){
$body[($k*2)] = [
'index' => [
'_id' => $v['id'].'_'.$v['age'],
// '_type'=> $type,
'_index'=> $index
]
];
$body[(($k*2)+1)] = $v;
}
// $this->elasticsearch->addBulkDoc($body);
//获取文档
$data = $this->elasticsearch->searchDoc('张',0,3);
echo json_encode($data['hits']);die();
var_dump($data['hits']['hits']);
//var_dump($data['aggregations']['min']);
}
}<?php
namespace app\common;
use Elastic\Elasticsearch\ClientBuilder;
use think\facade\Db;
class Elasticsearch
{
private $client;
private $indexName;
private $type;
/**
* 构造函数
*/
public function __construct()
{
$params = array('127.0.0.1:9200');
$this->client = ClientBuilder::create()->setHosts($params)->build();
}
/**
* 设置索引与类型
* @param string $indexName
* @param string $type
*/
public function setIndex($indexName = 'index', $type = 'type')
{
$this->indexName = $indexName;
$this->type = $type;
}
/**
* mysql涉及到搜索的数据插入文档
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function sync()
{
$list = Db::name("test")->select();
foreach ($list as $k => $v) {
$r = $this->addDoc($v['id'], $v);
}
}
/**
* 查看索引是否存在
*/
public function existsIndex(){
$params = ['index' => $this->indexName];
return $this->client->indices()->exists($params);
}
/**
* 删除索引
*
* @return void
*/
public function deleteIndex()
{
$params = ['index' => $this->indexName];
return $this->client->indices()->delete($params);
}
/**
* 创建索引
*/
public function createIndex()
{
// 只能创建一次
$params = [
'index' => $this->indexName,
'type' => $this->type,
'body' => [
"settings" => [
"number_of_shards" => 1,
"number_of_replicas" => 1
]
]
];
return $this->client->index($params);
}
/**
* 获取文档
*
* @param [type] $id
* @return void
*/
public function getDoc($id)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'id' => $id
];
return $this->client->get($params);
}
/**
* 创建文档结构
*
* @param [type] $id
* @param [type] $data
* @return void
*/
public function createMappings($body)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'include_type_name' => true, //7.0以上版本必须有
'body' => $body
];
return $this->client->indices()->putMapping($params);
}
/**
* 查看映射
*
* @return void
*/
public function getMapping()
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'include_type_name' => true, //7.0以上版本必须有
];
return $this->client->indices()->getMapping($params);
}
/**
* 添加文档
* @param string $id
* @param array $doc 跟创建文档结构时properties的字段一致
* @return array|callable
*/
public function addDoc(string $id, array $doc)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'id' => $id,
'body' => $doc
];
return $this->client->index($params);
}
/**
* 批量添加文档
* @param $body
* @return array
*/
public function addBulkDoc(array $body)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'body' => $body
];
return $this->client->bulk($params);
}
/**
* 判断文档存在
*
* @param integer $id
* @return void
*/
public function existsDoc($id = 1)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'id' => $id
];
return $this->client->exists($params);
}
/**
* 更新文档
* @param $id
* @param $key
* @param $value
* @return array|callable
*/
public function updateDoc($id, $key, $value)
{
// 可以灵活添加新字段,最好不要乱添加
$params = [
'index' => $this->indexName,
'type' => $this->type,
'id' => $id,
'body' => [
'doc' => [
$key => $value
]
]
];
return $this->client->update($params);
}
/**
* 删除文档
* @param int $id
* @return array|callable
*/
public function deleteDoc($id = 1)
{
$params = [
'index' => $this->indexName,
'type' => $this->type,
'id' => $id
];
return $this->client->delete($params);
}
/**
* 查询表达式搜索
* @param $keywords
* @param $from
* @param $size
* @return array
*/
public function searchDoc1($keywords, $from, $size)
{
return [
'query' => [
"match" => [
//"name"=>$keywords, 或者
"name" => [
'query' => $keywords,
'boost' => 3, // 权重
],
],
],
'from' => $from,
'size' => $size
];
}
/**
* 短语搜索
* @param $keywords
* @param $from
* @param $size
* @return array
*/
public function searchDoc3($keywords, $from, $size)
{
return [
'query' => [
"match_phrase" => [
//"name"=>$keywords, 或者
"name" => [
'query' => $keywords,
'boost' => 3, // 权重
],
],
],
'from' => $from,
'size' => $size
];
}
/**
* 高亮搜索
* @param string $keywords
* @param $from
* @param $size
* @return array
*/
public function searchDoc4(string $keywords, $from, $size): array
{
return [
'query' => [
"match_phrase" => [
//"name"=>$keywords, 或者
"name" => [
'query' => $keywords,
'boost' => 3, // 权重
],
],
],
'highlight' => [
"fields" => [
//必须加object,要不然转json时,这里依然是数组,而不是对象
"name" => (object)[]
]
],
'from' => $from,
'size' => $size
];
}
/**
* 搜索结果增加分析
* @param string $keywords
* @param int $from
* @param int $size
* @return array
*/
public function searchDoc5(string $keywords = '', int $from = 0, int $size = 12)
{
return [
'query' => [
'bool' => [
//必须匹配
"must" => [
"match" => [
"profile" => $keywords,
]
],
//应该匹配
'should' => [
['match' => [
'profile' => [
'query' => $keywords,
'boost' => 3, // 权重
]
]],
['match' => ['name' => [
'query' => $keywords,
'boost' => 2,
]]],
],
//复杂的搜索 限制年龄大于25岁
'filter' => [
"range" => [
"age" => ["gt" => 25]
]
]
],
],
'highlight' => [
"fields" => [
//必须加object,要不然转json时,这里依然是数组,而不是对象
"name" => (object)[]
]
],
'aggs' => [
"result" => [
//terms 桶 统计文档数量
"terms" => [
"field" => "age"
]
],
"avg" => [
//avg 平均值
"avg" => [
"field" => "age"
]
],
"max" => [
//max 最大值
"max" => [
"field" => "age"
]
],
"min" => [
//avg 最小值
"min" => [
"field" => "age"
]
],
],
'from' => $from,
'size' => $size,
];
}
/**
* 使用过滤器 filter
* @param $keywords
* @param $from
* @param $size
* @return array
*/
public function searchDoc2($keywords, $from, $size)
{
return [
'query' => [
'bool' => [
//必须匹配
"must" => [
"match" => [
"name" => $keywords,
]
],
//应该匹配
'should' => [
['match' => [
'profile' => [
'query' => $keywords,
'boost' => 3, // 权重
]
]],
['match' => ['name' => [
'query' => $keywords,
'boost' => 2,
]]],
],
//复杂的搜索 限制年龄大于25岁
'filter' => [
"range" => [
"age" => ["gt" => 40]
]
]
],
],
// 'sort' => ['age'=>['order'=>'desc']],
'from' => $from,
'size' => $size
];
}
public function searchDocs($keywords, $from, $size)
{
//匹配所有查询
// return [
// 'query' => [
// 'match_all' => (object)[],
// ]
// ];
//匹配查询
return [
'query' => [
'match' => [
"name" => '张三'
],
]
];
}
/**
* 查询文档 (分页,排序,权重,过滤)
* @param $keywords
* @param int $from
* @param int $size
* @return array|callable
*/
public function searchDoc($keywords, $from = 0, $size = 12)
{
$query = $this->searchDoc2($keywords, $from, $size);
$params = [
'index' => $this->indexName,
'type' => $this->type,
'body' => $query
];
return $this->client->search($params);
}
}在Ubuntu系统中配置静态IP地址,你可以编辑/etc/netplan目录下的YAML配置文件。以下是一个配置静态IP的例子:找到Netplan配置文件,通常命名为01-netcfg.yaml,00-installer-config.yaml或类似。ls /etc/netplan编辑该配置文件。使用文本编辑器,例如nano或vim或vi:sudo vi /etc/netplan/00-installer-config.yaml修改配置文件以设置静态IP。以下是一个配置示例:确保将enp160替
计算坐标是否在某区域内
获取范围内的经纬坐标点
/**
* 根据起点坐标和终点坐标测距离
* @param [array] $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
vm centos 共享宿主机文件夹 在 VMware 或 VirtualBox 中共享宿主机文件夹到 CentOS 虚拟机中是一个常见的需求,特别是在进行开发或测试时。这里我将分别介绍在 VMware 和 VirtualBox 中如何实现这一功能。
PHP中用Trait封装单例模式的实现
单例模式的定义
确保某一个类只有一个实例,不能重复实例,只能它自己实例化,而且向整个系统提供这个实例。
报错信息:error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/51/5107333e08a87b836d48ff7528b1e84b9c86781cc9f1748bbc1b8c42a870d933/data?verify=1725414357-z%2BnaVrrs0%2Fi0D3ixKDH8JHhJgUQ%3D: dial tcp 88.191.249.182:443: i/o timeout
Powered By Z-BlogPHP 1.7.3