簡單操作elasticsearch(es版本7.6)


簡單操作elasticsearch(es版本7.6)

es 官方文檔 https://www.elastic.co/guide/index.html

簡單操作elasticsearch主要是指管理索引,對數據進行增刪改查的操作。通常情況下我們使用es head進行這些操作,也可以通過postman或者是其它的http請求工具進行操作。

以下主要是基於es head操作

1、索引管理

a、創建索引(索引名稱必須小寫!!!)

請求地址:http://localhost:9200/{IndexName}

http方法:PUT

參數示例(創建address索引):

{
    "settings":{
        "number_of_shards":2,    
        "number_of_replicas":1
    },
    "mappings":{
            "properties":{
                "id":{"type":"keyword"},
                "country":{"type":"keyword"},
                "province":{"type":"keyword"},
                "city":{"type":"keyword"},
                "address1":{"type":"text"},
                "remark":{"type":"text"}
            }
        }
}

其中settings是設置索引分片信息:分片數,副本數。

mapping是設置索引的屬性信息(屬性名稱,類型----es數據類型請參照https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

es head實現示例::

 

 

也可以使用Postman或者其他http請求工具實現:

postman實現示例

 

 b、查看索引

請求地址:http://localhost:9200/{IndexName}/_mappings

http方法:GET

示例(查看address的mapping):

 

 c、查看索引的配置(settings)

請求地址:http://localhost:9200/{IndexName}/_settings

http方法:GET

示例(查看address的settings):

2、添加數據

注意:默認情況下添加數據時,如果指定的某個字段不存在時,es會自動創建該字段

 

a、指定id,不存在則創建,存在則更新

請求地址:http://localhost:9200/{IndexName}/_doc/{id}

http方法:PUT

參數示例:

{
  "country": "CN",
  "province": "廣東",
  "City": "廣州",
  "address1": "天河區獵德村123"
}

 

示例(更新address中id為1的數據--存在則更新,不存在則新增):

 

 

b、不指定id,直接創建,id由es自動生成

路徑:http://localhost:9200/IndexName/_doc

http方法:POST

參數示例:

{

  “country”:"CN",

  "province":"廣東",

  "City":"廣州",

  "address1":"白雲區xxx"

}

 

 

示例(創建一條新的address記錄)

 

 

3、查詢數據

請求地址:http://localhost:9200/{IndexName}/_search

方法:POST

參數示例

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "廣東"
          }
        }
      ]
    }
  }
}

 

示例(查詢province是“廣東”的address記錄)

 

4、修改數據

請求地址:http://localhost:9200/{IndexName}/_update_by_query

方法:POST

參數示例

{
  "script": {
    "source": "ctx._source.province='廣東省';ctx._source.address1=''"
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "廣東"
          }
        }
      ]
    }
  }
}

示例(更新province是“廣東”的記錄:修改province為“廣東省”、address1為空)

 

 

5、刪除數據

請求地址:http://localhost:9200/{IndexName}/_delete_by_query

方法:POST

參數示例:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "廣東省"
          }
        }
      ]
    }
  }
}

  

示例(刪除province是“廣東省”的記錄)

 

 

 

----------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM