請參考:
總結——》【Elasticsearch】
Elasticsearch——》kibana操作索引:增删改查
Elasticsearch——》kibana操作資料:增删改查
資料準備:
POST _bulk
{“index”:{"_index":“test_001”,"_id":1}}
{“id”:“1”,“goodsName”:“test”,“goodsInfo”:“xian”,“shopId”:1,“putSaleTime”:“2020-09-08 14:29:06”,“isDelete”:false,“couponIds”:“1,2”}
{“index”:{"_index":“test_001”,"_id":2}}
{“id”:2,“goodsName”:“test随意”,“goodsInfo”:“xian_002”,“shopId”:110,“putSaleTime”:“2020-09-09 14:29:06”,“isDelete”:false,“couponIds”:“3”}
{“index”:{"_index":“test_001”,"_id":3}}
{“id”:3,“goodsName”:“我是誰”,“goodsInfo”:“測試”,“shopId”:220,“putSaleTime”:“2020-02-22 12:29:06”,“isDelete”:false,“couponIds”:“1,3”}
{“index”:{"_index":“test_001”,"_id":4}}
{“id”:4,“goodsName”:“克而瑞”,“goodsInfo”:“克而瑞_001”,“shopId”:330,“putSaleTime”:“2020-11-01 14:29:06”,“isDelete”:false,“couponIds”:“4”}
{“index”:{"_index":“test_001”,"_id":5}}
{“id”:5,“goodsName”:“測試”,“goodsInfo”:“測試資訊”,“shopId”:330,“putSaleTime”:“2019-11-01 14:29:06”,“isDelete”:false,“couponIds”:“1,4”}
{“index”:{"_index":“test_001”,"_id":6}}
{“id”:6,“goodsName”:“流水測試_xian”,“goodsInfo”:“CREA”,“shopId”:330,“putSaleTime”:“2020-11-01 14:29:06”,“isDelete”:false,“couponIds”:“2”}
一、查詢所有資料
GET test_001/_search
1、_source:傳回指定字段
單個字段:多個字段:
"_source": "id" 或 "_source": ["id"]
"_source": ["id","goodsName"]
GET test_001/_search
{
"_source": ["id","goodsName"]
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPn5ENNpXTxEleNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1QTMwQzNxUTMxITOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、ids:指定_id
GET test_001/_search
{
"query": {
"ids":{
"values": [1,2]
}
}
}
3、range:指定範圍
GET test_001/_search
{
"query": {
"range":{
"shopId": {
"gte": 10,
"lte": 200
}
}
}
}
4、term:精準比對
goodsName:text類型的
比對:經過分詞、小寫化處理之後,隻能比對到解析之後的單獨 token
結果:“test”,“test随意”,“測TEst”,“teSt測試”
GET test_001/_search
{
"query": {
"term": {
"goodsName": {
"value": "test"
}
}
}
}
5、terms:清單比對
根據檢索詞清單來批量搜尋文檔,每個檢索詞在搜尋的時候相當于or的關系,隻要一個比對就行了。Elasticsearch 最多允許 65,536 個 term 同時查詢。
GET test_001/_search
{
"query": {
"terms": {
"shopId": [1,2,3]
}
}
}
}
二、分頁
GET test_001/_search
{
//開始位置的索引
"from": 0,
//查詢總大小
"size": 2
}
三、排序
正序:asc(大小寫均可)
倒序:desc(大小寫均可)
預設是按
排序的,支援
_score倒序
多個排序
GET test_001/_search
{
"sort": [
{
"_score": "desc"
}
]
}
GET test_001/_search
{
"sort": [
{
"_score": "desc"
},
{
"shopId": "asc"
}
]
}
四、高亮
1、預設高亮樣式
GET test_001/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"should": [
{
"match": {
"goodsName": {
"query": "xian"
}
}
},
{
"match": {
"goodsInfo": {
"query": "xian"
}
}
}
]
}
},
"highlight": {
"require_field_match": false,
"fields": {
"goodsName": {},
"goodsInfo": {}
}
}
}
2、自定義高亮樣式
嘻嘻,為了測試,每個字段設定不同的樣式,看一下效果~
GET test_001/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"should": [
{
"match": {
"goodsName": {
"query": "xian"
}
}
},
{
"match": {
"goodsInfo": {
"query": "xian"
}
}
}
]
}
},
"highlight": {
"require_field_match": false,
"fields": {
"goodsName": {
"pre_tags": "<mark>",
"post_tags": "</mark>"
},
"goodsInfo": {
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>"
}
}
}
}