当前位置:网站首页>Es operation command

Es operation command

2022-07-23 11:42:00 AA master Zhao

# Global operation 
# View cluster health 
GET /_cat/health?v

# Check the node status 
GET /_cat/nodes?v

# View the status of each index 
GET /_cat/indices?v

# Create index  PUT  Index name ?pretty
PUT /movie_index

# Delete index 
DELETE /movie_index

# Check the fragmentation of an index 
GET /_cat/shards/movie_index

# create documents 
PUT /movie_index/movie/1
{ "id":100,
 "name":"operation red sea",
 "doubanScore":8.5,
 "actorList":[ 
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/2
{
 "id":200,
 "name":"operation meigong river",
 "doubanScore":8.0,
 "actorList":[ 
{"id":3,"name":"zhang han yu"}
]
}


PUT /movie_index/movie/3
{
 "id":300,
 "name":"incident red sea",
 "doubanScore":5.0,
 "actorList":[ 
{"id":4,"name":"zhang san feng"}
]
}

# Query all documents in an index  size Control number 
GET /movie_index/_search
{
"size":3
}

# According to the document ID To view the document 
GET /movie_index/movie/1?pretty

# According to the document ID Delete the document 
DELETE /movie_index/movie/3

# After deletion, it is not really deleted , New record mark deletion , Need merger 
# It can also be performed manually   Merge trigger 
POST /_forcemerge 

#put  Replace existing documents 

PUT /movie_index/movie/3
{
 "id":300,
 "name":"incident red sea",
 "doubanScore":5.0,
 "actorList":[ 
{"id":4,"name":"zhang cuishan"}
]
}

#post New operation , Idempotency cannot be guaranteed 
# Ensure idempotency according to the primary key 
# According to the document  id  Update the document  doc  Fixed writing 
POST /movie_index/movie/3/_update?pretty
{
 "doc": {"name":"wudang"}
}

# Update documents according to conditions 
POST /movie_index/_update_by_query
{
"query": {
 "match":{
 "actorList.id":1
 } 
},
"script": {
 "lang": "painless",
 "source":"for(int i=0;i<ctx._source.actorList.length;i++){if(ctx._source.actorList[i].id==1){ctx._source.actorList[i].name='tttt'}}"
}
}

# Delete document properties 
POST /movie_index/movie/1/_update
{
 "script" : "ctx._source.remove('name')"
}

# Delete the document according to the conditions 
POST /movie_index/_delete_by_query
{
  "query":{
    "match": {"id":100}
  }
}

# Delete the document according to the conditions 
POST /movie_index/_delete_by_query
{
  "query":{
    "match_all": {}
  }
}

GET /movie_index/_search

# The batch   Create two documents in batch in the index 
POST /movie_index/movie/_bulk
{"index":{"_id":66}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}
{"index":{"_id":88}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}


# The batch   In a batch operation , Update the first document first (ID  by  66), Delete the second document (ID  by  88)
pOST /movie_index/movie/_bulk
{"update":{"_id":"66"}}
{"doc": { "name": "wudangshanshang" } }
{"delete":{"_id":"88"}}


#---------------------------------- Query operation 
# Query all the data in the current index 

GET /movie_index/_search?q=_id:66


GET /movie_index/_search


# Search according to the movie name 
GET /movie_index/_search
{
  "query": {
    "match": {
      "name": "operation red sea"
    }
  }
}

# Query according to the sub attribute of word segmentation 
GET /movie_index/_search
{
  "query": {
    "match": {
      "actorList.name": "zhang han yu"
    }
  }
}

# Query by phrase ( amount to like %%)
GET /movie_index/_search
{
  "query": {
    "match_phrase": {
      "actorList.name": "zhang han yu"
    }
  }
}

# No participle , Match by precise search ,term Precise matching , Use keyword complete 
GET /movie_index/_search
{
  "query": {
    "term": {
      "actorList.name.keyword": {
        "value": "zhang han yu"
      }
    }
  }
}

# Fault tolerant matching 
GET /movie_index/_search
{
  "query": {
    "fuzzy": {
      "name": "operati"
    }
  }
}

# Filter , First match , Re filtration 
GET /movie_index/_search
{
  "query": {
    "match": {
      "name": "red"
    }
  }
  ,"post_filter": {
    "term": {
      "actorList.id": 3
    }
  } 
}

# Filter - Recommended     Matching and filtering are carried out at the same time 
GET /movie_index/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "red"
        }}
      ],
      "filter": {
        "term": {
          "actorList.id": "3"
        }
      }
    }
  }
}


# Range queries   The Douban score is in 6-9 Query between 
GET /movie_index/_search
{
  "query": {
    "range": {
      "doubanScore": {
        "gte": 6,
        "lte": 9
      }
    }
  }
}

# Sort according to the descending order of Douban score 
GET /movie_index/_search
{
  "query": {
    "match": {
      "name": "red"
    }
  }, 
"sort": [
  {
    "doubanScore": {
      "order": "desc"
    }
  }
]
}

# Paging query   From what , Show several 
GET /movie_index/_search
{
  "from": 0,
  "size": 2
}

# Query the specified field 
GET /movie_index/_search
{
  "_source": ["id","name"]
  
}

# The highlighted , Highlight the hit word 

GET /movie_index/_search
{
  "query": {
    "match": {
      "name": "red"
    }
  },
  "highlight": {
    "fields": {"name": {}},
    "pre_tags": "<a>",
    "post_tags": "</a>"
  }
}


#-------------- Aggregate functions ----------------
# Need one , Take out how many movies each actor has participated in 
#aggs  Represent aggregation 
#term  Precise matching 
#terms  Aggregation operation , amount to group by
GET /movie_index/movie/_search
{
  "aggs": {
    "myAggs": {
      "terms": {
        "field": "actorList.name.keyword",
        "size": 10
      }
    }
  }
}


# Demand two , The average score of each actor in the film , And sort by the score 
GET /movie_index/_search
{
  "aggs": {
    "groupByname": {
      "terms": {
        "field": "actorList.name.keyword",
        "size": 10
        , "order": {
          "avg_score": "asc"
        }
      },
  "aggs": {
    "avg_score": {
      "avg": {
        "field": "doubanScore"
    }
  }
}
      }
    }
  }
}


# Chinese word segmentation  ik_smart  A simple word breaker 
#ik_max_word
GET /_analyze
{
  "text": " No card ",
  "analyzer": "ik_max_word"
}


#mapping 
# Create directly Document, Automatically create when creating documents index, meanwhile mapping Will automatically define 

PUT /movie_chn_1/movie/1
{ "id":1,
 "name":" The red sea action ",
 "doubanScore":8.5,
 "actorList":[ 
 {"id":1,"name":" Zhang Yi "},
 {"id":2,"name":" Hai Qing "},
 {"id":3,"name":" Zhang Hanyu "}
]
}
PUT /movie_chn_1/movie/2
{
 "id":2,
 "name":" Operation Mekong ",
 "doubanScore":8.0,
 "actorList":[ 
{"id":3,"name":" Zhang Hanyu "}
]
}
PUT /movie_chn_1/movie/3
{
 "id":3,
 "name":" The Red Sea incident ",
 "doubanScore":5.0,
 "actorList":[ 
{"id":4,"name":" Zhang Sanfeng "}
]
}


GET /movie_chn_1/_search

# View automatically defined mapping
GET /movie_chn_1/_mapping


GET /movie_chn_1/movie/_search
{
 "query": {
 "match": {
 "name": " Sea travel "
 }
 }
}

# Build an index based on Chinese word segmentation - Manual definition  mapping
# Definition  Index, Appoint  mapping
PUT movie_chn_2
{
 "mappings": {
 "movie":{
 "properties": {
 "id":{
 "type": "long"
 },
 "name":{
 "type": "text", 
 "analyzer": "ik_smart"
 },
 "doubanScore":{
 "type": "double"
 },
 "actorList":{
 "properties": {
 "id":{
 "type":"long"
 },
 "name":{
 "type":"keyword"
 }
 }
 }
 }
 }
 }
}

# towards  Index  put  Document
PUT /movie_chn_2/movie/1
{ "id":1,
 "name":" The red sea action ",
 "doubanScore":8.5,
 "actorList":[ 
 {"id":1,"name":" Zhang Yi "},
 {"id":2,"name":" Hai Qing "},
 {"id":3,"name":" Zhang Hanyu "}
]
}
PUT /movie_chn_2/movie/2
{
 "id":2,
 "name":" Operation Mekong ",
 "doubanScore":8.0,
 "actorList":[ 
{"id":3,"name":" Zhang Hanyu "}
]
}
PUT /movie_chn_2/movie/3
{
 "id":3,
 "name":" The Red Sea incident ",
 "doubanScore":5.0,
 "actorList":[ 
{"id":4,"name":" Zhang Sanfeng "}
]
}

GET /movie_chn_2/movie/_search
{
 "query": {
 "match": {
 "name": " Sea travel "
 }
 }
}



# Index data copy 
POST _reindex
{
 "source": {
 "index": "my_index_name"
 },
 "dest": {
 "index": "my_index_name_new"
 }
}



PUT  Index name 
{ 
"aliases": {
 " The index alias ": {}
 }
}
# When creating an index , Manual  mapping, And specify an alias 
PUT movie_chn_3
{
 "aliases": {
 "movie_chn_3_aliase": {}
 },
 "mappings": {
 "movie":{
 "properties": {
 "id":{
 "type": "long"
 },
 "name":{
 "type": "text", 
 "analyzer": "ik_smart"
 },
 "doubanScore":{
 "type": "double"
 },
 "actorList":{
 "properties": {
 "id":{
 "type":"long"
 },
 "name":{
 "type":"keyword"
 }
 }
 }
 }
 }
 }
}

GET /movie_chn_3_a2/_search


# Add aliases to existing indexes 

POST _aliases
{
 "actions": [
 { "add":{ "index": " Index name ", "alias": " The index alias " }}
 ]
}
# to  movie_chn_3  Add alias 
POST _aliases
{
 "actions": [
 { "add":{ "index": "movie_chn_3", "alias": "movie_chn_3_a2" }}
 ]
}

# Query alias list 
GET _cat/aliases?v


# Delete the alias of an index 
POST _aliases
{
 "actions": [
 { "remove": { "index": "movie_chn_3", "alias": "movie_chn_3_a2" }}
 ]
}

# Use scenarios - Group multiple indexes 
POST _aliases
{
 "actions": [
 { "add": { "index": "movie_chn_1", "alias": "movie_chn_query" }},
 { "add": { "index": "movie_chn_2", "alias": "movie_chn_query" }}
 ]
}
GET movie_chn_query/_search

# Creates a view for a subset of the index 
POST _aliases
{
 "actions": [
 { 
 "add": 
 { 
 "index": "movie_chn_1", 
 "alias": "movie_chn_1_sub_query",
 "filter": {
 "term": { "actorList.id": "4"}
 }
 }
 }
 ]
}
GET movie_chn_1_sub_query/_search


# You can seamlessly switch from one index to another in a running cluster 
POST /_aliases
{
 "actions": [
 { "remove": { "index": "movie_chn_1", "alias": "movie_chn_query" }},
 { "remove": { "index": "movie_chn_2", "alias": "movie_chn_query" }},
 { "add": { "index": "movie_chn_3", "alias": "movie_chn_query" }}
 ]
}


# Create index template 
PUT _template/template_movie2020
{
 "index_patterns": ["movie_test*"], 
 "settings": { 
 "number_of_shards": 1
 },
 "aliases" : { 
 "{index}-query": {},
 "movie_test-query":{}
 },
 "mappings": { 
 "_doc": {
 "properties": {
 "id": {
 "type": "keyword"
 },
 "movie_name": {
 "type": "text",
 "analyzer": "ik_smart"
 }
 }
 }
 }
}

# test   Add data to the index 
POST movie_test_202011/_doc
{
 "id":"333",
 "name":"zhang3"
}

GET /_cat/aliases

GET movie_test_202011-query/_mapping

GET movie_test_202011/_search

#  Check the list of existing templates in the system 
GET /_cat/templates


# View the details of a template 
GET _template/template_movie2020
# perhaps 
GET _template/template_movie*

原网站

版权声明
本文为[AA master Zhao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230538310358.html