<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          熬夜爆肝整理的elasticsearch中文手冊(cè)文檔

          共 81919字,需瀏覽 164分鐘

           ·

          2022-07-07 20:55

          由于本文篇幅較長(zhǎng),特意整理成了一份PDF文檔,想要獲取PDF,請(qǐng)回復(fù)"es手冊(cè)"即可領(lǐng)取文件。

          es概括

          Elaticsearch,簡(jiǎn)稱為 ES, ES 是一個(gè)開源的高擴(kuò)展的分布式全文搜索引擎,Elasticsearch 是面向文檔型數(shù)據(jù)庫(kù),一條數(shù)據(jù)在這里就是一個(gè)文檔。

          基本要素

          ES是一個(gè)文檔型數(shù)據(jù)庫(kù),在與傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)上,存在著一定的差異。下面將ES里面涉及到的元素與關(guān)系型數(shù)據(jù)庫(kù)進(jìn)行一一對(duì)應(yīng)。

          ElasticSearch索引(index)類型(type)文檔(document)字段(field)
          MySQL數(shù)據(jù)庫(kù)(database)數(shù)據(jù)表(table)數(shù)據(jù)行(row)數(shù)據(jù)列(column)

          索引操作

          創(chuàng)建索引

          向 ES 服務(wù)器發(fā) PUT 請(qǐng)求 : http://127.0.0.1:9200/shopping。創(chuàng)建索引只能使用PUT請(qǐng)求,PUT是冪等性的,也就是說(shuō)不存在的時(shí)候就會(huì)創(chuàng)建,存在的時(shí)候就不會(huì)重新創(chuàng)建而是返回索引已經(jīng)存在的信息。

          {
              "acknowledged"true,//響應(yīng)結(jié)果
              "shards_acknowledged"true,//分片結(jié)果
              "index""shopping"//索引名稱
          }

          查詢索引

          向 ES 服務(wù)器發(fā) GET 請(qǐng)求 : http://127.0.0.1:9200/shopping。

          {
              "shopping": {//索引名
                  "aliases": {},//別名
                  "mappings": {},//映射
                  "settings": {//設(shè)置
                      "index": {//設(shè)置 - 索引
                          "creation_date""1617861426847",//設(shè)置 - 索引 - 創(chuàng)建時(shí)間
                          "number_of_shards""1",//設(shè)置 - 索引 - 主分片數(shù)量
                          "number_of_replicas""1",//設(shè)置 - 索引 - 主分片數(shù)量
                          "uuid""J0WlEhh4R7aDrfIc3AkwWQ",//設(shè)置 - 索引 - 主分片數(shù)量
                          "version": {//設(shè)置 - 索引 - 主分片數(shù)量
                              "created""7080099"
                          },
                          "provided_name""shopping"//設(shè)置 - 索引 - 主分片數(shù)量
                      }
                  }
              }
          }

          查看所有索引

          向 ES 服務(wù)器發(fā) GET 請(qǐng)求 : http://127.0.0.1:9200/_cat/indices?v。

          這里請(qǐng)求路徑中的_cat 表示查看的意思, indices 表示索引,所以整體含義就是查看當(dāng)前 ES服務(wù)器中的所有索引,就好像 MySQL 中的 show tables 的感覺,服務(wù)器響應(yīng)結(jié)果如下 :

          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 服務(wù)器發(fā) DELETE 請(qǐng)求 : http://127.0.0.1:9200/shopping。

          返回結(jié)果如下:

          {
              "acknowledged"true
          }

          文檔操作

          文檔創(chuàng)建

          假設(shè)索引已經(jīng)創(chuàng)建好了,接下來(lái)我們來(lái)創(chuàng)建文檔,并添加數(shù)據(jù)。這里的文檔可以類比為關(guān)系型數(shù)據(jù)庫(kù)中的表數(shù)據(jù),添加的數(shù)據(jù)格式為 JSON 格式

          在 Postman 中,向 ES 服務(wù)器發(fā) POST 請(qǐng)求 : http://127.0.0.1:9200/shopping/_doc,請(qǐng)求體JSON內(nèi)容為:

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

          返回結(jié)果

          {
              "_index""shopping",//索引
              "_type""_doc",//類型-文檔
              "_id""ANQqsHgBaKNfVnMbhZYU",//唯一標(biāo)識(shí),可以類比為 MySQL 中的主鍵,隨機(jī)生成
              "_version"1,//版本
              "result""created",//結(jié)果,這里的 create 表示創(chuàng)建成功
              "_shards": {//
                  "total"2,//分片 - 總數(shù)
                  "successful"1,//分片 - 總數(shù)
                  "failed"0//分片 - 總數(shù)
              },
              "_seq_no"0,
              "_primary_term"1
          }

          注意,此處發(fā)送文檔創(chuàng)建請(qǐng)求的方式必須為 POST,不能是 PUT,否則會(huì)發(fā)生錯(cuò)誤 。

          上面的數(shù)據(jù)創(chuàng)建后,由于沒有指定數(shù)據(jù)唯一性標(biāo)識(shí)(ID),默認(rèn)情況下, ES 服務(wù)器會(huì)隨機(jī)生成一個(gè)。

          如果想要自定義唯一性標(biāo)識(shí),需要在創(chuàng)建時(shí)指定: http://127.0.0.1:9200/shopping/_doc/1,請(qǐng)求體JSON內(nèi)容為:

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

          返回結(jié)果如下:

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

          文檔查詢

          查看文檔時(shí),需要指明文檔的唯一性標(biāo)識(shí),類似于 MySQL 中數(shù)據(jù)的主鍵查詢 在 Postman 中,向 ES 服務(wù)器發(fā) GET 請(qǐng)求 : http://127.0.0.1:9200/shopping/_doc/1 。

          返回結(jié)果如下:

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

          查找不存在的內(nèi)容,向 ES 服務(wù)器發(fā) GET 請(qǐng)求 :http://127.0.0.1:9200/shopping/_doc/1001。返回結(jié)果如下:

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

          查看索引下所有數(shù)據(jù),向 ES 服務(wù)器發(fā) GET 請(qǐng)求 : http://127.0.0.1:9200/shopping/_search。

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""1",
                          "_score"1,
                          "_source": {
                              "title""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      }
                  ]
              }
          }

          文檔刪除

          刪除一個(gè)文檔不會(huì)立即從磁盤上移除,它只是被標(biāo)記成已刪除(邏輯刪除)。

          在 Postman 中,向 ES 服務(wù)器發(fā) DELETE 請(qǐng)求 : http://127.0.0.1:9200/shopping/_doc/1 返回結(jié)果:

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

          文檔修改

          全量修改

          和新增文檔一樣,輸入相同的 URL 地址請(qǐng)求,如果請(qǐng)求體變化,會(huì)將原有的數(shù)據(jù)內(nèi)容覆蓋

          在 Postman 中,向 ES 服務(wù)器發(fā) POST 請(qǐng)求 : http://127.0.0.1:9200/shopping/_doc/1 請(qǐng)求體JSON內(nèi)容為:

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

          修改成功后,服務(wù)器響應(yīng)結(jié)果:

          {
              "_index""shopping",
              "_type""_doc",
              "_id""1",
              "_version"2,
              "result""updated",//<---updated 表示數(shù)據(jù)被更新
              "_shards": {
                  "total"2,
                  "successful"1,
                  "failed"0
              },
              "_seq_no"2,
              "_primary_term"1
          }

          局部更新

          修改數(shù)據(jù)時(shí),也可以只修改某一給條數(shù)據(jù)的局部信息

          在 Postman 中,向 ES 服務(wù)器發(fā) POST 請(qǐng)求 : http://127.0.0.1:9200/shopping/_update/1。

          請(qǐng)求體JSON內(nèi)容為:

          {
           "doc": {
            "title":"小米手機(jī)",
            "category":"小米"
           }
          }

          返回結(jié)果如下:

          {
              "_index""shopping",
              "_type""_doc",
              "_id""1",
              "_version"3,
              "result""updated",//<----updated 表示數(shù)據(jù)被更新
              "_shards": {
                  "total"2,
                  "successful"1,
                  "failed"0
              },
              "_seq_no"3,
              "_primary_term"1
          }

          URL待條件查詢

          查找category為小米的文檔,在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search?q=category:小米,返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      ......
                  ]
              }
          }

          上述為URL帶參數(shù)形式查詢,這很容易讓不善者心懷惡意,或者參數(shù)值出現(xiàn)中文會(huì)出現(xiàn)亂碼情況。為了避免這些情況,我們可用使用帶JSON請(qǐng)求體請(qǐng)求進(jìn)行查詢。

          請(qǐng)求體帶參查詢

          接下帶JSON請(qǐng)求體,還是查找category為小米的文檔,在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      ......
                  ]
              }
          }

          帶請(qǐng)求體方式的查找所有內(nèi)容

          查找所有文檔內(nèi)容,也可以這樣,在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下

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

          則返回所有文檔內(nèi)容:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      ......
                  ]
              }
          }

          查詢指定字段

          如果你想查詢指定字段,在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)"
                          }
                      },
                      ......
                  ]
              }
          }

          分頁(yè)查詢

          在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                  ]
              }
          }

          查詢排序

          如果你想通過(guò)排序查出價(jià)格最高的手機(jī),在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          },
                          "sort": [
                              3999
                          ]
                      },
                      ......
                  ]
              }
          }

          多條件查詢

          假設(shè)想找出小米牌子,價(jià)格為3999元的。(must相當(dāng)于數(shù)據(jù)庫(kù)的&&),在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      }
                  ]
              }
          }

          假設(shè)想找出小米和華為的牌子。(should相當(dāng)于數(shù)據(jù)庫(kù)的||)在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      .....
                  ]
              }
          }

          范圍查詢

          假設(shè)想找出小米和華為的牌子,價(jià)格大于2000元的手機(jī)。在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      }
                  ]
              }
          }

          全文檢索

          這功能像搜索引擎那樣,如品牌輸入“小華”,返回結(jié)果帶回品牌有“小米”和華為的。在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      ......
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""BtR6sHgBaKNfVnMbX5Y5",
                          "_score"0.6931471,
                          "_source": {
                              "title""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      ......
                  ]
              }
          }

          完全匹配

          在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      ......
                  ]
              }
          }

          高亮查詢

          在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          },
                          "highlight": {
                              "category": [
                                  "華<em>為</em>"//<------高亮一個(gè)為字。
                              ]
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""B9R6sHgBaKNfVnMbZpZ6",
                          "_score"0.6931471,
                          "_source": {
                              "title""華為手機(jī)",
                              "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""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          },
                          "highlight": {
                              "category": [
                                  "華<em>為</em>"
                              ]
                          }
                      }
                  ]
              }
          }

          分組查詢

          在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"3999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""A9R5sHgBaKNfVnMb25Ya",
                          "_score"1,
                          "_source": {
                              "title""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""BNR5sHgBaKNfVnMb7pal",
                          "_score"1,
                          "_source": {
                              "title""小米手機(jī)",
                              "category""小米",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""BtR6sHgBaKNfVnMbX5Y5",
                          "_score"1,
                          "_source": {
                              "title""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""B9R6sHgBaKNfVnMbZpZ6",
                          "_score"1,
                          "_source": {
                              "title""華為手機(jī)",
                              "category""華為",
                              "images""http://www.gulixueyuan.com/xm.jpg",
                              "price"1999
                          }
                      },
                      {
                          "_index""shopping",
                          "_type""_doc",
                          "_id""CdR7sHgBaKNfVnMbsJb9",
                          "_score"1,
                          "_source": {
                              "title""華為手機(jī)",
                              "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
                          }
                      ]
                  }
              }
          }

          上面返回結(jié)果會(huì)附帶原始數(shù)據(jù)的。若不想要不附帶原始數(shù)據(jù)的結(jié)果,在 Postman 中,向 ES 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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 服務(wù)器發(fā) GET請(qǐng)求 : http://127.0.0.1:9200/shopping/_search,附帶JSON體如下:

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

          返回結(jié)果如下:

          {
              "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
                  }
              }
          }

          映射關(guān)系

          有了索引庫(kù),等于有了數(shù)據(jù)庫(kù)中的 database。接下來(lái)就需要建索引庫(kù)(index)中的映射了,類似于數(shù)據(jù)庫(kù)(database)中的表結(jié)構(gòu)(table)。創(chuàng)建數(shù)據(jù)庫(kù)表需要設(shè)置字段名稱,類型,長(zhǎng)度,約束等;索引庫(kù)也一樣,需要知道這個(gè)類型下有哪些字段,每個(gè)字段有哪些約束信息,這就叫做映射(mapping)。先創(chuàng)建一個(gè)索引:

          # PUT http://127.0.0.1:9200/user

          返回結(jié)果:

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

          創(chuàng)建映射

          # 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
                  }
              }
          }

          返回結(jié)果如下:

          {
              "acknowledged"true
          }

          查詢映射

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

          返回結(jié)果如下:

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

          增加數(shù)據(jù)

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

          返回結(jié)果如下:

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

          查找name含有”小“數(shù)據(jù):

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

          返回結(jié)果如下:

          {
              "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含有”男“數(shù)據(jù):

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

          返回結(jié)果如下:

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

          找不想要的結(jié)果,只因創(chuàng)建映射時(shí)"sex"的類型為"keyword"。"sex"只能完全為”男的“,才能得出原數(shù)據(jù)。

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

          返回結(jié)果如下:

          {
              "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"
            }
           }
          }

          返回結(jié)果如下:

          {
              "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
          }

          報(bào)錯(cuò)只因創(chuàng)建映射時(shí)"tel"的"index"為false。



          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人性生交大片免费看网站2023年 | 夜色在线影院 无码 | 先锋资源男人 | 成人无码视频在线看 | 999国产精品999久久久久久 |