在config目录下增加 elasticsearch.php配置文件,内容如下
<?php
return [
'host'=>env('ELASTICSEARCH_HOST','127.0.0.1'),
'port'=>9200
];
$rs = Elastic::db()->createIndex([
'index'=>'testdb',
'type'=>'news',
'properties'=>[
'id'=>[
'type'=>'long',
],
'theme_id'=>[
'type'=>'long',
],
'cat_unique_name'=>[
'type'=>"text",
],
'title'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
],
'summary'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
],
'keywords'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
],
'unique_name'=>[
'type'=>'text',
],
'html'=>[
'type'=>'text',
'analyzer'=>'ik_max_word',
'search_analyzer'=>'ik_max_word',
]
]
]);
dd($rs);
Result:
array:2 [▼
"status" => 1
"data" => array:3 [▼
"acknowledged" => true
"shards_acknowledged" => true
"index" => "testdb"
]
]
Elastic::db()->index('testdb')->deleteIndex();
#或者
Elastic::db('testdb')->deleteIndex();
$rs = Elastic::db('theme')->type('article')->search([
'query'=>[
'match'=>['title'=>'git']
],
'size'=>10
]);
$rs = Elastic::db('theme')->type('article')->search([
'query'=>[
"bool"=>[
'must'=>[
"term"=>["title"=>"redis"]
],
"must_not"=>[
"term"=>["keywords"=>"get"]
]
]
],
'size'=>10
]);
$rs = Elastic::db('theme')->type('article')->search([
'query'=>[
"bool"=>[
'should'=>[
"match"=>["title"=>"redis"]
],
"should"=>[
"match"=>["html"=>"redis"]
]
]
],
'size'=>10
]);
Result 搜索结果以分页形式返回
dump($rs->items()); #数据内容
dump($rs->links()); #分页