ES基本語法
阿新 • • 發佈:2020-10-20
上一篇講的是安裝,本篇記錄常用的命令
https://www.cnblogs.com/xiaozhang666/p/13845617.html
與mysql相比下對應關係
#1.新增 PUT /root01 #2.檢視 GET /root01 #3.刪除 DELETE /root01
PUT /索引庫名稱/_mapping/型別名稱 或者 /索引庫名稱/型別名稱/_mapping { "properties":{ "欄位名稱":{ "type":"型別", "index": true,"store": false, "analyzer": "分詞器" } } }
-
型別:對應的是mysql中的欄位型別
-
String
-
text:
可以分詞,但是不可以參與分組聚合查詢
-
keyword:
不可以分詞,但是可以參與分組聚合查詢
-
-
數值型別
-
double,long,float,int.....
-
-
陣列
arrays
-
日期
date: 日期型別不常用,日期型別佔用空間較大,一般使用毫秒值---->long
-
物件
-
..........
-
-
index:
是否索引,預設值是true,代表會索引
-
store:
是否獨立儲存,預設值是false,不獨立儲存,除非是比較重要的欄位可以設定。
-
analyzer:
分詞器,ik_max_word(最細粒度分詞)
舉例
#必須先建立heima索引庫 PUT /root01 #建立型別 PUT /root01/_mapping/goods { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "subtitle": { "type": "text", "analyzer": "ik_max_word" }, "images": { "type": "keyword", "index": "false" }, "price": { "type": "float" } } } #檢視型別 GET /root01/_mapping/goods
#1.新增文件 POST /root01/goods/ { "title":"大紅手機", "images":"http://image.leyou.com/12479122.jpg", "price":2699.00 } #2.根據id檢視文件 GET /root01/goods/a7S5P3UBnwV3pvMMA4Sr #3.指定id新增 POST /root01/goods/1 { "title":"小紅手機", "images":"http://image.leyou.com/12479122.jpg", "price":3399.00 } #4.根據id修改文件 如果資料庫中沒有id=3會新增,如果有則修改 POST /heima/goods/3 { "title":"超超超大紅手機", "images":"http://image.leyou.com/12479122.jpg", "price":99999.00 } #5.根據id刪除文件 DELETE /root01/goods/3 #6.根據查詢刪除所有 POST /root01/_delete_by_query { "query": { "match_all": {} } }
基本查詢
POST /root01/_search { "query": { "match_all": {} } } #2.條件模糊查詢 POST /root01/_search { "query": { "match": { "title": "大紅" } } } #3.多欄位匹配查詢 #準備資料 POST /root01/goods/1 { "title": "超大紅手機", "images": "http://image.leyou.com/12479122.jpg", "price": 5288, "subtitle": "華為手機" } POST /heima/_search { "query": { "multi_match": { "query": "手機", "fields": [ "title", "subtitle" ] } } } #4.精確查詢 查詢時欄位是不分詞 POST /root01/_search { "query": { "term": { "price": { "value": "2699" } } } } #5.多條件精確查詢 POST /root01/_search { "query": { "terms": { "price": [ "2699", "5288" ] } } }
結果過濾
預設是包含(includes),可以設定excludes不包含
#結果過濾 包含 POST /root01/_search { "_source": [ "subtitle", "title" ], "query": { "match_all": {} } } #結果過濾 不包含 POST /root01/_search { "_source": { "excludes": ["price","title"] }, "query": { "match_all": {} } }
高階查詢(擴充套件)
#布林組合查詢 與或非 GET /root01/_search { "query":{ "bool":{ "must": { "match": { "title": "小米" }}, "must_not": { "match": { "title": "電視" }}, "should": { "match": { "title": "手機" }} } } } #範圍查詢 gte 大於等於 lte 小於等於 POST /root01/_search { "query": { "range": { "price": { "gte": 2699, "lte": 3288 } } } } #模糊查詢,預設係數為1,可以寫錯一個字母 POST /root01/_search { "query": { "fuzzy": { "title": "appla" } } } #模糊查詢,最多支援兩個字母寫錯 POST /root01/_search { "query": { "fuzzy": { "title": { "value": "appaa", "fuzziness": 2 } } } }