Elastic Search (ES)基本使用

基於kibana的DevTool可以很方便的進行ES的測試。當然,你可以用curl命令或者Postman等工具進行測試。

 

下面是一些基本的數據操作,可以參考一下。

# 獲取ES的基本信息
GET _search
{
  "query": {
    "match_all": {}
  }
}

# 添加數據:
# megacorp是index索引名稱
# employee是類型
# 1是文檔唯一ID
PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

# 獲取對應索引megacorp,類型employee,ID爲2的文檔
GET /megacorp/employee/1

# 查看對應索引megacorp,類型employee,ID爲2的文檔是否存在
HEAD /megacorp/employee/1

# 添加數據:
# megacorp是index索引名稱
# employee是類型
# 2是文檔唯一ID
PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

# 獲取對應索引megacorp,類型employee,ID爲2的文檔
GET /megacorp/employee/2

# 添加數據:
# megacorp是index索引名稱
# employee是類型
# 3是文檔唯一ID
PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

# 獲取對應索引megacorp,類型employee,ID爲3的文檔
GET /megacorp/employee/3

# 獲取對應索引megacorp,類型employee的所有文檔
GET /megacorp/employee/_search

# 搜索對應索引megacorp,類型employee的last name 爲“Smith”的文檔
GET /megacorp/employee/_search?q=last_name:"Smith"

# 搜索對應索引megacorp,類型employee的last name 爲“Smith” 且about字段中包含“albums”的文檔
GET /megacorp/employee/_search?q=last_name:"Smith" AND about:albums

# 搜索對應索引megacorp,類型employee的last name 爲“Smith”的文檔
GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "last_name": "Smith"
    }
  }
}

# 搜索對應索引megacorp,類型employee的last name 爲“Smith”的文檔中年齡大於25的部分
GET /megacorp/employee/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "last_name": "Smith"
        }
      },
      "filter": {
        "range": {
          "age": {
            "gt": 25
          }
        }
      }
    }
  }
}

# 搜索對應索引megacorp,類型employee的about字段中包括“rock”或者“climbing” 全字的文檔
GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "about": "rock climbing"
    }
  }
}

# 搜索對應索引megacorp,類型employee的about字段中包括“rock climbing”全字的文檔
GET /megacorp/employee/_search
{
  "query": {
    "match_phrase": {
      "about": "rock climbing"
    }
  }
}

# 搜索對應索引megacorp,類型employee的about字段中包括“rock climb”爲前綴的文檔
GET /megacorp/employee/_search
{
  "query": {
    "match_phrase_prefix": {
      "about": "rock climb"
    }
  }
}

# 搜索對應索引megacorp,類型employee的about字段中包括“rock climbing”全字的文檔,並且高亮搜索到的詞
GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "about": "rock climbing"
    }
  },
  "highlight": {
    "fields": {
      "about": {}
    }
  }
}

# 聚合對應索引megacorp,類型employee的文檔中interests字段。ES 5.0版本以上不再支持
GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章