版本說明
- 本文是在es8.4.1下進行操作,同時已經(jīng)安裝了ik分詞器。
- php操作es的庫為?elasticsearch-php,github地址為:
GitHub - elastic/elasticsearch-php: Official PHP client for Elasticsearch. - 所有的操作都基于本地服務器進行,本地安裝時關(guān)閉了安全驗證
- index為goods,mapping結(jié)構(gòu)如下:
{
"goods": {
"mappings": {
"properties": {
"brand": {
"type": "keyword"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
},
"id": {
"type": "integer"
},
"sort": {
"type": "integer"
},
"title": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
- 進行操作時,新建es對象
use Elastic\Elasticsearch\ClientBuilder;
$client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build();
一些查詢操作
我們使用es的情景大部分都是查詢,下面列舉一些常用的操作場景以及對應的代碼文章來源:http://www.zghlxwxcb.cn/news/detail-410337.html
獲取mapping結(jié)構(gòu)
$response = $client->indices()->getMapping(['index'=>'goods_bak']);
批量批量插入數(shù)據(jù)
//$count 為從數(shù)據(jù)庫中取的總數(shù),$result為對應的結(jié)果集
for($i = 0; $i < $count; $i++) {
$add_params['body'][] = [
'index' => [
'_index' => "goods",
'_id' => ($i+1) . ""
],
];
$add_params['body'][] = [
'id' => $result[$i][0] + 1,
'title' => $result[$i][1],
'content' => $result[$i][2],
'sort' => $result[$i][3],
'brand' => $result[$i][4],
];
}
$responses = $client->bulk($add_params);
刪除文檔數(shù)據(jù)
$delete_params = [
'index' => 'goods',
'body' => [
'query' => [
'range' => [
'id' => [
//刪除id > 1的文檔記錄
'gte' =>1
]
]
]
]
];
$responses = $client->deleteByQuery($delete_params);
查詢title中包含關(guān)鍵詞懶蟲的文檔記錄
// match 會對查詢的關(guān)鍵詞進行分詞,term不會。例如查詢?nèi)A為手機,match會查詢title中包含華為或者手機或者華為手機的,term不會對查詢的關(guān)鍵詞做拆分
//不支持對多個字段進行同時查詢
//es默認返回10條記錄,可以用from(偏移量)和size(pagesize)進行分頁
$params = [
'index' => 'goods',
'body' => [
'query' => [
'term' => [
// 'match' => [
'title' => '懶蟲天鵝'
]
]
],
'from' => 0, //分頁的位置
'size' => 20 //返回多少條
];
$response = $client->search($params)->asArray();
模糊查詢之wildcard、前綴查詢
$params = [
'index' => 'goods',
'body' => [
'query' => [
//wildcard會對查詢條件進行分詞,后面可以接* ? 通配符進行匹配
// prefix前綴匹配,匹配到分詞中以設(shè)置的關(guān)鍵詞開頭的分詞
// 'wildcard' => [
'prefix' => [
'brand' => '星'
]
]
],
'from' => 0,
'size' => 20
];
范圍查詢
$params = [
'index' => 'goods',
'body' => [
'query' => [
'range' => [
'sort' => [
'gte' => 10,
'lte' => 100,
]
]
]
],
'from' => 0,
'size' => 20
];
bool查詢
//bool查詢,多個條件共同成立,有四種選擇,must(計算得分,性能較低) filter(過濾條件,不計算得分,性能高),must_not(條件必須不成立),should(條件可以成立)
//查詢title包含懶蟲且brand中含有華為的文檔記錄
$params = [
'index' => 'goods',
'body' => [
'query'=>[
'bool' => [
'filter' => [
//多個篩選條件時,每個term是數(shù)組形式
['term' => ['title' => '懶蟲',]],
['term' => ['brand' =>'華為',]]
]
]
]
],
'from' => 0,
'size' => 30
];
多個字段不同關(guān)鍵詞查詢的并集操作
$params = [
'index' => 'goods',
'body' => [
'query'=>[
//查詢title包含懶蟲或者brand包含華為的文檔記錄
'dis_max' => [
'queries' => [
//多個篩選條件時,每個term是數(shù)組形式
['term' => ['title' => '懶蟲',]],
['term' => ['brand' =>'華為',]]
]
]
],
//對結(jié)果進行聚合操作
'aggs' => [
//最大得分的記錄,返回的結(jié)果字段為max_sort
'max_sort' => [
'max' => ['field' => 'sort']
],
//對查詢結(jié)果按brand進行聚合匯總,取出前size條記錄
'goods_brand_rank' => [
'terms' => [
'field' => 'brand',
'size' => 10
]
]
],
//對結(jié)果進行高亮顯示
'highlight' => [
//這里定義高亮顯示的字段以及高亮顯示的tag,前端拿到tag后進行處理
'fields' => [
'title' => [
'pre_tags' => '<font color="red">',
'post_tags' => '</font>',
]
]
]
],
'from' => 0,
'size' => 30
];
?文章來源地址http://www.zghlxwxcb.cn/news/detail-410337.html
到了這里,關(guān)于elasticsearch 簡單使用【php版本】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!