Skip to content

Instantly share code, notes, and snippets.

@linnv
Created February 12, 2022 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnv/dace3ff3edd76ddb51c58be50d74e99d to your computer and use it in GitHub Desktop.
Save linnv/dace3ff3edd76ddb51c58be50d74e99d to your computer and use it in GitHub Desktop.
all in one operation of elastic search CRUD, DDL by curl
#!/usr/bin/env bash
getAddr()
{
cat <<EOF
http://elastic:Qnzs2020@172.26.50.105:9230
EOF
}
# curl -XDELETE "$(getAddr)/mappingdetectdemo?pretty"
# echo "delete index done"
#
# curl -XGET "$(getAddr)/mappingdetectdemo/_mapping?pretty"
# echo "check index done"
#
# curl -XPUT "$(getAddr)/mappingdetectdemo/_doc/1?pretty" -d'{"name":"Michael","age":"22.5","birth":"2099-11-15T13:12:00"}' -H 'Content-Type: application/json'
# echo "add one date done"
#
# curl -XGET "$(getAddr)/mappingdetectdemo/_mapping?pretty"
# echo "check index doc done"
#
# curl -XPOST "$(getAddr)/mappingdetectdemo/_doc/1/_update?pretty" -d'{
# "doc": {
# "age": "24",
# "birth":"abc",
# "city":"Wyano"
# }
# }
# ' -H 'Content-Type: application/json'
# echo "update index doc done with number, birth won't be updated because it's type id date on the begining(on insert step)"
curl -XGET "$(getAddr)/_cat/indices?pretty"
curl -XDELETE "$(getAddr)/mappingdetectdemo?pretty"
echo "delete index done"
#to create mapping must put mappings in json body
#to update mapping must put mappings in uri like /mappingdetectdemo/_mapping
# "numeric_detection": false, no need to set to false: because '11' then 22, or 22 hten '11', both will work no matter numberic_detection is set or not
# should have concern for date_detection, like this case won't work is its true(default), '2099-11-15T13:12:00' then 'abc'
curl -XPUT "$(getAddr)/mappingdetectdemo?pretty" -H 'Content-Type: application/json' -d'
{
"mappings": {
"date_detection": false,
"properties": {
"name" : {
"type" : "keyword"
}
}
}
}
'
curl -XGET "$(getAddr)/mappingdetectdemo/_mapping?pretty"
echo "check index done"
curl -XPUT "$(getAddr)/mappingdetectdemo/_doc/1?pretty" -d'{"name":"Michael","age":"22.5","birth":"2099-11-15T13:12:00"}' -H 'Content-Type: application/json'
echo "add one date done"
curl -XGET "$(getAddr)/mappingdetectdemo/_mapping?pretty"
echo "check index doc done"
curl -XPOST "$(getAddr)/mappingdetectdemo/_doc/1/_update?pretty" -d'{
"doc": {
"age": 24,
"birth":"abc",
"city":"Wyano"
}
}
' -H 'Content-Type: application/json'
echo "update index doc done with number"
#it may take a while to be query
sleep 1s
curl -XGET "$(getAddr)/mappingdetectdemo/_search?size=10&pretty" -H 'Content-Type: application/json' -d'{ "_source": true,
"sort" : [
{ "name" : "desc" }],
"query": { "match_all": {} }}'
echo "query done"
curl -XGET "$(getAddr)/mappingdetectdemo/_search?size=10&pretty" -H 'Content-Type: application/json' -d'{ "_source": true , "sort" : [
{ "name" : "desc" }],
"query": { "bool" : {
"should": [
{ "match": { "birth": "abc" } }
]
} }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment