安装插件
composer elasticsearch/elasticsearch
引用
use Elasticsearch\ClientBuilder;
public function esinit()
{
//建立es连接
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$params = [
'index' => 'fang', #index的名字不能是大写和下划线开头
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
],
/*'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'fang_name' => [
'type' => 'text',
//'analyzer' => 'ik_max_word'
],
'fang_xiaoqu' => [
'type' => 'text',
//'analyzer' => 'ik_max_word'
]
]
]*/
]
];
$res = $client->indices()->create($params);
dd($res);
}
public function esindex()
{
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$params = [
'index' => 'fang',
'type' => '_doc',
'include_type_name' => true,
'body' => [
'_doc' => [
'_source' => [
'enabled' => true
],
'properties' => [
'fang_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'fang_xiaoqu' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'fang_desn' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
]
]
]
]
];
$res = $client->indices()->putMapping($params);
dd($res);
}
public function esdoc()
{
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$params = [
'index' => 'fangs',
'type' => '_doc',
//'id' => 100, #可以手动指定id,也可以不指定随机生成
'body' => [
'fang_name' => '兰香湖壹号',
'fang_xiaoqu' => '闵行区兰香湖西路168弄',
'fang_desn' => '项目价格调整,2022年09月06日为2050.0万/套,最新价格为2200.0万/套'
]
];
$res = $client->index($params);
dd($res);
}
public function essearch()
{
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$query = [
'query' => [
'bool' => [
'must' => [
'match' => [
'fang_name' => '中国'
],
'match' => [
'fang_xiaoqu' => '向上'
]
]
]
],
/*'query' => [
'match_phrase' => [
'fang_name' => [
'query' => '底色锃亮',
//'slop' => 1
]
]
],*/
/* 'query' => [
'match' => [
'fang_name' => [
'query' => '中国网络文明大会'
]
]
],*/
/*'query' => [
'multi_match' => [
'query' => '中国网络文明大会',
'fields' => [
'fang_name',
'fang_xiaoqu'
]
]
],
'highlight' => [
'pre_tags' => ["<em>"],
'post_tags' => ["</em>"],
'fields' => [
"fang_name" => new \stdClass()
]
]*/
];
$params = [
'index' => 'fangs',
// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至这两个参数都是可选的
'type' => '_doc',
//'_source' => ['fang_name','fang_xiaoqu', 'fang_desn'], // 请求指定的字段
'body' => array_merge([
'from' => 0,
'size' => 5
], $query)
];
$res = $client->search($params);
print_r($res);
}
public function delDoc()
{
$id = 'U2szN30BFnSrfszRzPqr';
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$params = [
'index' => 'fangs',
'type' => '_doc',
'id' => $id
];
$res = $client->delete($params);
dd($res);
}
public function updateDoc() {
$client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
$id = 'VGs4N30BFnSrfszRivo5';
$params = [
'index' => 'fangs',
'type' => '_doc',
'id' =>$id,
'body' => [
'doc' => [
'fang_name' => '地上人间',
'fang_xiaoqu' => '五四公路3389号'
]
]
];
$res = $client->update($params);
dd($res);
}