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