熬夜爆肝整理的一份elasticsearch中文文件手冊

7small7發表於2022-07-02
由於本文篇幅較長,想要獲取PDF,請關注‘公眾號-菜鳥成長學習筆記’回覆"es手冊"即可領取檔案。

es概括

Elaticsearch,簡稱為 ES, ES 是一個開源的高擴充套件的分散式全文搜尋引擎,Elasticsearch 是面向文件型資料庫,一條資料在這裡就是一個文件。

基本要素

ES是一個文件型資料庫,在與傳統的關係型資料庫上,存在著一定的差異。下面將ES裡面涉及到的元素與關係型資料庫進行一一對應。

ElasticSearch索引(index)型別(type)文件(document)欄位(field)
MySQL資料庫(database)資料表(table)資料行(row)資料列(column)

索引操作

建立索引

向 ES 伺服器發 PUT 請求 : http://127.0.0.1:9200/shopping。建立索引只能使用PUT請求,PUT是冪等性的,也就是說不存在的時候就會建立,存在的時候就不會重新建立而是返回索引已經存在的資訊。

{
    "acknowledged": true,//響應結果
    "shards_acknowledged": true,//分片結果
    "index": "shopping"//索引名稱
}

查詢索引

向 ES 伺服器發 GET 請求 : http://127.0.0.1:9200/shopping

{
    "shopping": {//索引名
        "aliases": {},//別名
        "mappings": {},//對映
        "settings": {//設定
            "index": {//設定 - 索引
                "creation_date": "1617861426847",//設定 - 索引 - 建立時間
                "number_of_shards": "1",//設定 - 索引 - 主分片數量
                "number_of_replicas": "1",//設定 - 索引 - 主分片數量
                "uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//設定 - 索引 - 主分片數量
                "version": {//設定 - 索引 - 主分片數量
                    "created": "7080099"
                },
                "provided_name": "shopping"//設定 - 索引 - 主分片數量
            }
        }
    }
}

檢視所有索引

向 ES 伺服器發 GET 請求 : http://127.0.0.1:9200/_cat/indices?v

這裡請求路徑中的_cat 表示檢視的意思, indices 表示索引,所以整體含義就是檢視當前 ES伺服器中的所有索引,就好像 MySQL 中的 show tables 的感覺,伺服器響應結果如下 :

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping J0WlEhh4R7aDrfIc3AkwWQ   1   1          0            0       208b           208b

刪除索引

向 ES 伺服器發 DELETE 請求 : http://127.0.0.1:9200/shopping

返回結果如下:

{
    "acknowledged": true
}

文件操作

文件建立

假設索引已經建立好了,接下來我們來建立文件,並新增資料。這裡的文件可以類比為關係型資料庫中的表資料,新增的資料格式為 JSON 格式

在 Postman 中,向 ES 伺服器發 POST 請求 : http://127.0.0.1:9200/shopping/_doc,請求體JSON內容為:

{
    "title":"小米手機",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

返回結果

{
    "_index": "shopping",//索引
    "_type": "_doc",//型別-文件
    "_id": "ANQqsHgBaKNfVnMbhZYU",//唯一標識,可以類比為 MySQL 中的主鍵,隨機生成
    "_version": 1,//版本
    "result": "created",//結果,這裡的 create 表示建立成功
    "_shards": {//
        "total": 2,//分片 - 總數
        "successful": 1,//分片 - 總數
        "failed": 0//分片 - 總數
    },
    "_seq_no": 0,
    "_primary_term": 1
}
注意,此處傳送文件建立請求的方式必須為 POST,不能是 PUT,否則會發生錯誤 。

上面的資料建立後,由於沒有指定資料唯一性標識(ID),預設情況下, ES 伺服器會隨機生成一個。

如果想要自定義唯一性標識,需要在建立時指定: http://127.0.0.1:9200/shopping/_doc/1,請求體JSON內容為:

{
    "title":"小米手機",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

返回結果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",//<-----自定義唯一性標識
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

文件查詢

檢視文件時,需要指明文件的唯一性標識,類似於 MySQL 中資料的主鍵查詢
在 Postman 中,向 ES 伺服器發 GET 請求 : http://127.0.0.1:9200/shopping/_doc/1

返回結果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手機",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999
    }
}

查詢不存在的內容,向 ES 伺服器發 GET 請求 : http://127.0.0.1:9200/shoppin...。返回結果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "found": false
}

檢視索引下所有資料,向 ES 伺服器發 GET 請求 : http://127.0.0.1:9200/shopping/_search

返回結果如下:

{
    "took": 133,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

文件刪除

刪除一個文件不會立即從磁碟上移除,它只是被標記成已刪除(邏輯刪除)。

在 Postman 中,向 ES 伺服器發 DELETE 請求 : http://127.0.0.1:9200/shopping/_doc/1
返回結果:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted",//<---刪除成功
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

文件修改

全量修改

和新增文件一樣,輸入相同的 URL 地址請求,如果請求體變化,會將原有的資料內容覆蓋

在 Postman 中,向 ES 伺服器發 POST 請求 : http://127.0.0.1:9200/shopping/_doc/1
請求體JSON內容為:

{
    "title":"華為手機",
    "category":"華為",
    "images":"http://www.gulixueyuan.com/hw.jpg",
    "price":1999.00
}

修改成功後,伺服器響應結果:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",//<---updated 表示資料被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

區域性更新

修改資料時,也可以只修改某一給條資料的區域性資訊

在 Postman 中,向 ES 伺服器發 POST 請求 : http://127.0.0.1:9200/shopping/_update/1

請求體JSON內容為:

{
    "doc": {
        "title":"小米手機",
        "category":"小米"
    }
}

返回結果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",//<----updated 表示資料被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

URL待條件查詢

查詢category為小米的文件,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search?q=category:小米,返回結果如下:

{
    "took": 94,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

上述為URL帶引數形式查詢,這很容易讓不善者心懷惡意,或者引數值出現中文會出現亂碼情況。為了避免這些情況,我們可用使用帶JSON請求體請求進行查詢。

請求體帶參查詢

接下帶JSON請求體,還是查詢category為小米的文件,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match":{
            "category":"小米"
        }
    }
}

返回結果如下:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

帶請求體方式的查詢所有內容

查詢所有文件內容,也可以這樣,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下

{
    "query":{
        "match_all":{}
    }
}

則返回所有文件內容:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

查詢指定欄位

如果你想查詢指定欄位,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match_all":{}
    },
    "_source":["title"]
}

返回結果如下:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手機"
                }
            },
            ......
        ]
    }
}

分頁查詢

在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":2
}

返回結果如下:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
        ]
    }
}

查詢排序

如果你想通過排序查出價格最高的手機,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match_all":{}
    },
    "sort":{
        "price":{
            "order":"desc"
        }
    }
}

返回結果如下:

{
    "took": 96,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": null,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            ......
        ]
    }
}

多條件查詢

假設想找出小米牌子,價格為3999元的。(must相當於資料庫的&&),在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "price":3999.00
                }
            }]
        }
    }
}

返回結果如下:

{
    "took": 134,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.3862944,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 2.3862944,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

假設想找出小米和華為的牌子。(should相當於資料庫的||)在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "category":"華為"
                }
            }]
        },
        "filter":{
            "range":{
                "price":{
                    "gt":2000
                }
            }
        }
    }
}

返回結果如下:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            .....
        ]
    }
}

範圍查詢

假設想找出小米和華為的牌子,價格大於2000元的手機。在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "category":"華為"
                }
            }],
            "filter":{
                "range":{
                    "price":{
                        "gt":2000
                    }
                }
            }
        }
    }
}

返回結果如下:

{
    "took": 72,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

全文檢索

這功能像搜尋引擎那樣,如品牌輸入“小華”,返回結果帶回品牌有“小米”和華為的。在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match":{
            "category" : "小華"
        }
    }
}

返回結果如下:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 0.6931471,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            ......
        ]
    }
}

完全匹配

在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match_phrase":{
            "category" : "為"
        }
    }
}

返回結果如下:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            ......
        ]
    }
}

高亮查詢

在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "query":{
        "match_phrase":{
            "category" : "為"
        }
    },
    "highlight":{
        "fields":{
            "category":{}//<----高亮這欄位
        }
    }
}

返回結果如下:

{
    "took": 100,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "華<em>為</em>"//<------高亮一個為字。
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "B9R6sHgBaKNfVnMbZpZ6",
                "_score": 0.6931471,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "華<em>為</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "CdR7sHgBaKNfVnMbsJb9",
                "_score": 0.6931471,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "華<em>為</em>"
                    ]
                }
            }
        ]
    }
}

分組查詢

在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "aggs":{//聚合操作
        "price_group":{//名稱,隨意起名
            "terms":{//分組
                "field":"price"//分組欄位
            }
        }
    }
}

返回結果如下:

{
    "took": 63,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "A9R5sHgBaKNfVnMb25Ya",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BNR5sHgBaKNfVnMb7pal",
                "_score": 1,
                "_source": {
                    "title": "小米手機",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 1,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "B9R6sHgBaKNfVnMbZpZ6",
                "_score": 1,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "CdR7sHgBaKNfVnMbsJb9",
                "_score": 1,
                "_source": {
                    "title": "華為手機",
                    "category": "華為",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1999,
                    "doc_count": 5
                },
                {
                    "key": 3999,
                    "doc_count": 1
                }
            ]
        }
    }
}

上面返回結果會附帶原始資料的。若不想要不附帶原始資料的結果,在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "aggs":{
        "price_group":{
            "terms":{
                "field":"price"
            }
        }
    },
    "size":0
}

返回結果如下:

{
    "took": 60,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1999,
                    "doc_count": 5
                },
                {
                    "key": 3999,
                    "doc_count": 1
                }
            ]
        }
    }
}

查詢平均值

在 Postman 中,向 ES 伺服器發 GET請求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

{
    "aggs":{
        "price_avg":{//名稱,隨意起名
            "avg":{//求平均
                "field":"price"
            }
        }
    },
    "size":0
}

返回結果如下:

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 2332.3333333333335
        }
    }
}

對映關係

有了索引庫,等於有了資料庫中的 database。接下來就需要建索引庫(index)中的對映了,類似於資料庫(database)中的表結構(table)。建立資料庫表需要設定欄位名稱,型別,長度,約束等;索引庫也一樣,需要知道這個型別下有哪些欄位,每個欄位有哪些約束資訊,這就叫做對映(mapping)。
先建立一個索引:

# PUT http://127.0.0.1:9200/user

返回結果:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}

建立對映

# PUT http://127.0.0.1:9200/user/_mapping

{
    "properties": {
        "name":{
            "type": "text",
            "index": true
        },
        "sex":{
            "type": "keyword",
            "index": true
        },
        "tel":{
            "type": "keyword",
            "index": false
        }
    }
}

返回結果如下:

{
    "acknowledged": true
}

查詢對映

#GET http://127.0.0.1:9200/user/_mapping

返回結果如下:

{
    "user": {
        "mappings": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "sex": {
                    "type": "keyword"
                },
                "tel": {
                    "type": "keyword",
                    "index": false
                }
            }
        }
    }
}

增加資料

#PUT http://127.0.0.1:9200/user/_create/1001
{
    "name":"小米",
    "sex":"男的",
    "tel":"1111"
}

返回結果如下:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

查詢name含有”小“資料:

#GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "name":"小"
        }
    }
}

返回結果如下:

{
    "took": 495,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.2876821,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "1111"
                }
            }
        ]
    }
}

查詢sex含有”男“資料:

#GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "sex":"男"
        }
    }
}

返回結果如下:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

找不想要的結果,只因建立對映時"sex"的型別為"keyword"。"sex"只能完全為”男的“,才能得出原資料。

#GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "sex":"男的"
        }
    }
}

返回結果如下:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.2876821,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "1111"
                }
            }
        ]
    }
}

查詢電話

# GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "tel":"11"
        }
    }
}

返回結果如下:

{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
                "index": "user"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "user",
                "node": "4P7dIRfXSbezE5JTiuylew",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                    "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
                    "index": "user",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Cannot search on field [tel] since it is not indexed."
                    }
                }
            }
        ]
    },
    "status": 400
}

報錯只因建立對映時"tel"的"index"為false。

相關文章