1. 程式人生 > 其它 >ElasticSearch學習七 ES簡單操作

ElasticSearch學習七 ES簡單操作

7.1、Restful

rest是Representational State Transfer三個單詞的縮寫,表現層狀態轉移,或者表述性狀態轉移。

Rest是web服務的一種架構風格,一種設計風格,是一種思想;同時Rest不是針對某一種程式語言的。

以webService為例通俗解釋。

非Rest設計,以往我們都會這麼寫:

http://localhost:8080/admin/getUser (查詢使用者)
http://localhost:8080/admin/addUser (新增使用者)
http://localhost:8080/admin/updateUser (更新使用者)
http://localhost:8080/admin/deleteUser (刪除使用者)

以不同的URL(主要為使用動詞)進行不同的操作。

 

Rest架構:

GET http://localhost:8080/admin/user (查詢使用者)
POST http://localhost:8080/admin/user (新增使用者)
PUT http://localhost:8080/admin/user (更新使用者)
DELETE http://localhost:8080/admin/user (刪除使用者)

URL只指定資源,以HTTP方法動詞進行不同的操作。用HTTP STATUS/CODE定義操作結果。

7.2、操作索引

操作語法

PUT|GET|POST|DELETE http://ip:port/索引名稱

以"_"開頭的表示內建的一些命令或者內建的一些屬性,例如每個索引都有mappings、settings等,可以採用以下的語法訪問內建的屬性。

例如:http://ip:port/索引名稱/_mappings ,表示查詢某個索引下的mappings屬性的值。

http://ip:port/_all ,表示查詢所有的索引

新增索引

PUT http://ip:port/索引名稱

新增所以在ES中是Put請求,並非Post請求。下面是新增索引的操作

查詢索引

GET http://ip:port/索引名稱

在ES的操作中,GET請求是查詢索引,用GET請求查詢剛才建立的索引

GET http://192.168.2.135:9200/goods_index

返回結果

{
    "goods_index": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1651392797166",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "qPbYkcIGTyKsFi0dI0JLEg",
                "version": {
                    "created": "7040299"
                },
                "provided_name": "goods_index"
            }
        }
    }
}

刪除索引

DELETE http://ip:port/索引名稱

在ES的操作中,DELETE是刪除索引

DELETE http://192.168.2.135:9200/goods_index

{
    "acknowledged": true
}

關閉索引

POST http://ip:port/索引名稱/_close

在ES的操作中,POST請求是修改,關閉索引其實是修改索引,用POST請求。

POST http://192.168.2.135:9200/goods_index/_close

返回結果

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "indices": {
        "goods_index": {
            "closed": true
        }
    }
}

 

注意:在ES中,以"_"為開頭的命令都是ES內建的命令,例如_close、_open、_all

 

開啟索引

POST http://ip:port/索引名稱/_open

在ES的操作中,POST請求是修改,關閉索引其實是修改索引,用POST請求。

POST http://192.168.2.135:9200/goods_index/_open

返回結果

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

 

7.3、對映Mappings

ES的mapping非常類似於靜態語言中的資料型別:宣告一個變數為int型別的變數, 以後這個變數都只能儲存int型別的資料。

同樣的, 一個number型別的mapping欄位只能儲存number型別的資料。

同靜態語言的資料型別相比,mapping還有一些其他的含義,mapping不僅告訴ES一個field中是什麼型別的值, 它還告訴ES如何索引資料以及資料是否能被搜尋到。

mapping中欄位型別一旦設定後 禁止直接修改。因為lucene實現的倒排索引生成後不允許修改。除非重建索引對映,然後做reindex操作。

mapping欄位型別

Field datatypes 欄位的資料型別

  • 核心資料型別

    • 字串型別: text(分詞),keyword(不分詞)一個用於全文檢索,一個用於聚合和排序。

    • 數值型: long,integer,short,byte,double,float,half_float,scaled_float

    • 日期:date

    • 布林:boolean

    • 二進位制:binary

    • 範圍型別:integer_range,float_range,long_range,double_range,date_range

  • 複雜資料型別

    • 陣列 array

    • 巢狀型別 nested object

    • PUT test_index
      {
        "mappings": {
          "doc": {
            "properties": {
              "man":{            #設定man欄位為nested型別
                "type": "nested",  
                "properties": {
                  "age":{
                    "type":"integer"
                   },
                  "name":{
                    "type":"text"
                  }
                }}}}}}}
      PUT test_index/doc/1
      {
        "man":[
          {
            "name":"alice white",
            "age":34
          },
          {
            "name":"peter brown",
            "age":26
          }
          ]
      }
      # 巢狀型別的欄位的查詢和聚合:
      GET test_index/_search
      {
        "query": {       #查詢
          "nested": {         #關鍵字
            "path": "man", 
            "query": {
              "match": {
                "man.name": "peter"  
              }
            }
          }
        },
        "size": 0, 
        "aggs": {
          "man": {   
            "nested": {    #聚合關鍵字
              "path": "man"
            },
            "aggs": {
              "avg_age": {
                "avg": {
                  "field": "man.age"
                }
              }
            }}}}

       

    • 物件 object

    • PUT test_index
      {
        "mappings": {
          "doc": {
            "properties": {
              "obj":{              #obect型別欄位
                "properties": {
                  "age":{
                    "type":"integer"
                   },
                  "name":{
                    "type":"text"
                  }
                }
              }
            }
          }
        }
      }
      PUT test_index/doc/1
      {
        "obj":[
          {
            "name":"alice white",
            "age":34
          },
          {
            "name":"peter brown",
            "age":26
          }
          ]
      }
      GET test_index/_search
      {
        "query": {
          "match": {
            "obj.name": "peter"
            }
        }
      }
      ​

 

  • 地理型別

    • geo_point

    • geo_shape

  • 專用型別

    • 記錄ip地址 ip

    • 實現自動補全 completion

    • 記錄分詞數 token_count

    • 記錄字串hash值 murmur3

    • percolator

    • join

mapping引數

  • dynamic 引數動態新增新欄位

    • -true 允許自動將檢測到的新欄位加到對映中(預設的)

    • -false 不允許自動新增欄位,文件可以寫入,但無法對欄位進行搜尋等操作。不會新增在對映中。且在kibana上面看到的新欄位上會顯示一個黃色感嘆號,重新整理index pattern也無效。

    • -strict 文件不能寫入,寫入會報錯

  • analyzer 指定分詞器

  • ignore_above 超過ignore_above的字串將不會被索引或儲存

PUT test_index
{
  "mappings": {
    "doc":{
      "properties": {
        "message":{
          "type": "keyword",
          "ignore_above": 20  #欄位值超過20個字元的字串不會被索引或者儲存
        }
      }
    }
  }
}
POST test_index/doc/_bulk
{"index":{"_id":1}}
{"message":"test message"}
{"index":{"_id":2}}
{"message":"test message  with some long stacktrace messages test test"}
​
GET test_index/_search
{
  "size": 0, 
  "aggs": {
    "message": {
      "terms": {
        "field": "message",
        "size": 10
      }
    }
  }
}----------------------->只能得到第一個桶
      "buckets": [
        {
          "key": "test message",
          "doc_count": 1
        }
      ]
​
GET test_index/_search    
{
  "query": {
    "query_string": {
      "default_field": "message",
      "query": "*message*"
    }
  }
}
------------->只能搜尋到id為1的文件
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "message": "test message"
        }
      }
    ]
  • index true | false 控制欄位是否被索引,預設為true。

  • doc_values 本質是一個序列化的列式儲存 。列式儲存適用於聚合,排序,指令碼操作。

    • true 預設對除了analyzed strings以外的所有欄位開啟。

    • false 不能用於聚合、排序和指令碼操作

  • fields 對同一欄位採用不同型別配置。比如string欄位對映為text做全文搜尋,對映為keyword做聚合和排序

PUT test_index
{
  "mappings": {
    "doc":{
      "properties": {
        "name":{
          "type": "text",     #text型別,用於全文檢索
          "fields": {
            "keyword":{    #name.keyword
              "type": "keyword"   #keyword型別,用於排序和聚合
            }
          }
        }
      }
    }
  }
}
PUT test_index/doc/1
{
  "name":"Jack smis"
}
PUT test_index/doc/2
{
  "name":"Jack"
}
GET test_index/_search
{
  "query": {
    "match": {
      "name": "jack"      #text型別欄位
    }
  },
  "sort": [
    {
      "name.keyword": {    #keyword型別欄位
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "name_count": {
      "value_count": {
        "field": "name.keyword"   #keyword型別欄位
      }
    }
  }
}
  • properties object欄位或nested欄位包含子欄位,稱為properties。properties可以是任何資料型別

  • PUT test_index
    {
      "mappings": {
        "doc": {
          "properties": {
            "dev":{                #object型別欄位
              "properties": {
                "name":{
                  "type":"text"
                },
                "age":{
                  "type":"integer"
                }
              }
            },
            "rel":{
              "type": "nested",    #nested型別欄位
              "properties": {
                "age":{
                  "type":"integer"
                },
                "name":{
                  "type":"text"
                }
              }
            }
          }
        }
      }
    }
    PUT test_index/doc/1
    {
      "dev":{
        "name":"john smith",
        "age":23
      },
      "rel":[
        {
          "name":"alice white",
          "age":34
        },
        {
          "name":"peter brown",
          "age":26
        }
        ]
    }
  • norms 時間評分因子,如果不關心評分可以禁用

mapping操作

對映建立完成之後,ES是不允許直接修改欄位名字或者刪除對映。因為後面需要對欄位進行檢索或者查詢。

操作語法

PUT|GET http://ip:port/索引/_mapping

新增對映

建立完索引之後新增對映

PUT http://192.168.2.128:9200/person/_mappings  
{
  "properties": {
    "name": {
      "type": "keyword"
    },
    "age": {
      "type": "integer"
    }
  }
}

建立索引時新增對映

PUT http://192.168.2.128:9200/person
{
    "mappings":{
        "properties":{
            "name":{
                "type":"keyword"
            },
            "age":{
                "type":"integer"
            }
        }
    }
}

 

查詢對映

PUT http://192.168.2.128:9200/person/_mapping
​
//返回的結果如下
​
{
    "person": {
        "aliases": {},
        // 下面的mappings就是新增的索引
        "mappings": {
            "properties": {
                "age": {
                    "type": "integer"
                },
                "name": {
                    "type": "keyword"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1651547517051",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "t979BGXtS52FtEi7rGy7jg",
                "version": {
                    "created": "7040299"
                },
                "provided_name": "person"
            }
        }
    }
}

 

新增欄位

為已經存在的索引新增欄位

put http://192.168.2.135:9200/person/_mappings
{
    "properties":{
           "address":{
               "type":"text"
           }
        }
}

7.4、操作文件

新增文件

語法

從es7之後,就沒有型別了,所有的型別都是預設為_doc

put|post http://ip:port/索引名稱/_doc/id

指定id

指定id可以用put請求,也可以用post請求

put http://192.168.2.135:9200/person/_doc/1
{
    "name":"張三",
    "age":20,
    "address":"杭州市蕭山區"
}
​
// 執行結果
{
    "_index": "person",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
不指定id

不指定id,只能用post請求

post http://192.168.2.135:9200/person/_doc/
{
    "name":"李四",
    "age":30,
    "address":"杭州市濱江區"
}
​
// 執行結果
{
    "_index": "person",
    "_type": "_doc",
    "_id": "ijEGiIABZy4G6iwbrBf2", //此是id是隨機生成
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

 

查詢文件

指定Id的方式查詢文件

get http://192.168.2.135:9200/person/_doc/1
​
//執行結果
{
    "_index": "person",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "張三",
        "age": 20,
        "address": "杭州市蕭山區"
    }
}

 

查詢所有的文件

get http://192.168.2.135:9200/person/_search
​
{
    "took": 65,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "張三",
                    "age": 20,
                    "address": "杭州市蕭山區"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "ijEGiIABZy4G6iwbrBf2",
                "_score": 1.0,
                "_source": {
                    "name": "李四",
                    "age": 30,
                    "address": "杭州市濱江區"
                }
            }
        ]
    }
}

 

修改文件

修改文件與新增文件一樣,如果id存在就是修改,如果id不存在就是新增

put http://192.168.2.135:9200/person/_doc/1
{
    "name":"張三",
    "age":30,
    "address":"杭州市濱江區"
}
​
​
//執行結果
{
    "_index": "person",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",//修改操作
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

 

刪除文件

delete http://192.168.2.135:9200/person/_doc/1
​
//執行結果
{
    "_index": "person",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}