版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zch3210/article/details/89497668
protected $es;
public function __construct()
{
$params = array(
'127.0.0.1:9200'
);
$this->es = ClientBuilder::create()->setHosts($params)->build();
}
1.创建索引(分词,拼音,同义词)
public function setMapping()
{
$params = [
'index' => 'news',
'body' => [
'settings' => [
'analysis' => [
'filter' => [
'my_synonym_filter' => [
'type' => 'dynamic_synonym',
// 'synonyms_path' => "analysis/synonym.txt"
'synonyms_path' => "http://127.0.0.1/phpproject/tp5es/synonym.txt",
"interval" => 30
]
],
'analyzer' => [
'my_synonyms' => [
'tokenizer' => 'ik_max_word',
'filter' => [
'lowercase',
'my_synonym_filter'
]
]
]
]
],
'mappings' => [
'_doc' => [
'properties' => [
'name' => [
'type' => 'keyword',
'fields' => [
'pinyin' => [
'type' => 'text',
'analyzer' => 'pinyin',
]
]
],
'age' => [
'type' => 'integer'
],
'content' => [
'type' => 'text',
'analyzer' => 'my_synonyms',
'fields' => [
'pinyin' => [
'type' => 'text',
'analyzer' => 'pinyin'
]
]
]
]
]
]
]
];
$res = $this->es->indices()->create($params);
var_dump($res);
}
2.删除索引
//删除索引
public function deleteindex()
{
$params = [
'index' => 'test2',
];
$response = $this->es->indices()->delete($params);
var_dump($response);
}
3.添加数据(单个添加,批量添加)
//单个添加
public function add()
{
$params =[
'index' => 'news',
'type' => '_doc',
'id' => 'id_1',
'body' => [
'name' => '体育课',
'age' => 31,
'content' => '煤化工哦看'
]
];
$response = $this->es->index($params);
var_dump($response);
}
//批量添加
public function addall()
{
for($i = 2; $i < 10; $i ++) {
$params['body'][] = [
'index' => [
'_index' => 'news',
'_type' => '_doc',
'_id' => 'id_'.$i
]
];
$params['body'][] = [
'name' => '李四'.$i,
'age' => $i,
'content' => '李四也爱吃西红柿'
];
}
$response = $this->es->bulk($params);
var_dump($response);
}
4.更新
public function update()
{
$params = [
'index' => 'news',
'type' => '_doc',
'id' => 'id_2',
'body' => [
'doc' => [
'name' => null
]
]
];
$response = $this->es->update($params);
var_dump($response);
}
5.删除
public function delete()
{
$params = [
'index' => 'news',
'type' => '_doc',
'id' => 'id_3'
];
$response = $this->es->delete($params);
var_dump($response);
}
6.搜索
(1)拼音
public function search()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'match' => [
'name.pinyin' => 'zhang'
]
],
//查询呢个字段
"_source" => [ "name", "age" ],
]
];
$result = $this->es->search($params);
var_dump($result);
}
(2)排序加高亮显示
public function simSearch()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'match' => [
'content' => '中美'
]
],
'sort' => [
'age' => [
'order' => 'desc'
]
],
//高亮
'highlight' => [
//高亮标签(默认<em></em>)
'pre_tags' => ["<em style='color: red'>"],
'post_tags' => ["</em>"],
'fields' => [
'content' => new \stdClass()
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
(3)分页
// from -> size分页
public function page()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'from' => 0,
'size' => 3,
'query' => [
'match' => [
'content' => '番茄'
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
//scroll 分页
public function scroll()
{
//查询5页
$page = 5;
$params = [
'index' => 'news',
'type' => '_doc',
'size' => 3,
'scroll' => '1m',
'body' => [
// 查询条件
]
];
$result = $this->es->search($params);
var_dump($result);
echo "<br><br>";
$scroll_id = $result['_scroll_id'];
for ($i = 2; $i < $page; $i ++) {
$response = $this->es->scroll([
'scroll_id' => $scroll_id,
'scroll' => '1m'
]);
if (count($response['hits']['hits']) > 0) {
$scroll_id = $response['_scroll_id'];
var_dump($response);
echo "<br><br>";
} else {
break;
}
}
}
(4)处理null字符
//过滤非空 is not null
public function filternull()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'constant_score' => [
'filter' => [
//exists过滤为null的字段
'exists' => [
'field' => 'name'
]
]
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
//查找出某字段为空 is null
public function isnull()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must_not' => [
'exists' => [
'field' => 'name'
]
]
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
(5)查询多个字段并设置权重
//多字段查询
public function allSearch()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'multi_match' => [
'query' => '马铃薯',
'fields' => ['name^2.0', 'content^1.0']
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
(6)聚合函数示例
//group分组
public function group()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'aggs' => [
'group_by' => [
'terms' => [
'field' => 'age'
]
]
],
]
];
$result = $this->es->search($params);
var_dump($result);
}
//min
public function min()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'aggs' => [
'group_by' => [
'min' => [
'field' => 'age'
]
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
(7)组合查询
// 组合查询
public function muchSearch()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must' => [
['term' => ['name' => '马铃薯']]
],
'must_not' => [
['match' => ['content' => '中国和美国']]
],
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
// must+should
public function getall()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['age' => 50]],
['bool' => [
'should' => [
['match' => ['content' => '番茄']],
['match' => ['content' => '中国和美国']]
]
]]
]
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}
(8)热搜词
//出现最多的前两个
public function gethot()
{
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'aggs' => [
'top' => [
'terms' => [
'field' => 'name',
'size' => 2, //top2
'order' => ['_count' => 'desc'] //降序排列
]
]
]
]
];
$result = $this->es->search($params);
var_dump($result);
}