ElasticSearch 6.5 基本用法

zmisgod 发布于 2018-12-18 16:06:06 阅读 63 评论 0

集群健康

检查集群健康

curl -X GET "192.168.170.17:9200/_cat/health?v"

获取集群节点

curl -X GET "192.168.170.17:9200/_cat/nodes?v"

索引

显示所有索引

curl -X GET "192.168.170.17:9200/_cat/indices?v"

创建索引

curl -X PUT "192.168.170.17:9200/teacher?pretty"

删除索引

curl -X DELETE "192.168.170.17:9200/teacher?pretty"

文档

获取文档

curl -X GET "192.168.170.17:9200/teacher/_doc/1?pretty"

创建、修改文档

创建文档

curl -X POST "192.168.170.17:9200/teacher/_doc/1" -H 'Content-Type: application/json' -d'
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}'

指定ID修改、新增

curl -X PUT "192.168.170.17:9200/teacher/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe1"
}'

不指定ID新增

下面这个curl没有指定id,此时es会自动生成一个随机id,保存在es中,在返回时会给一个_id字段,此时为生成的随机id(_0AXwGcBceVY5KMbdFW7)

curl -X POST "192.168.170.17:9200/teacher/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "zmisgod"
}'

上面返回回来的_id可以根据获取文档。_0AXwGcBceVY5KMbdFW7为上面返回的_id字段

curl -X GET "192.168.170.17:9200/teacher/_doc/_0AXwGcBceVY5KMbdFW7?pretty"

修改文档

curl -X POST "192.168.170.17:9200/teacher/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "starzmisgod","age":12 }
}'

可以使用小脚本来修改数据,比如自增age + 5岁

curl -X POST "192.168.170.17:9200/teacher/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}'

删除文档

curl -X DELETE "192.168.170.17:9200/teacher/_doc/2?pretty"

批量处理文档(需要注意,每个需要参数后需要一个\n,末尾也需要\n)

批量新增、修改操作
curl -X POST "192.168.170.17:9200/teacher/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
批量修改、删除操作
curl -X POST "192.168.170.17:9200/teacher/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'

搜索文档

搜索全部信息

curl -X GET "192.168.170.17:9200/teacher/_doc/_search"

轻量搜索

curl -X GET "192.168.170.17:9200/teacher/_doc/_search?q=first_name:Jane"

查询表达式搜索

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}'

复杂搜索

搜索姓氏为 Smith 的雇员且年龄大于 30 的

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}'

全文搜索

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "about" : "like"
        }
    }
}'

短语搜索

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock albums"
        }
    }
}'

高亮搜索

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock albums"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}'

分析文档

挖掘出雇员中最受欢迎的兴趣爱好

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}'

Smith 的雇员中最受欢迎的兴趣爱好

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}'

查询特定兴趣爱好员工的平均年龄

curl -X GET "192.168.170.17:9200/teacher/_doc/_search" -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}'