Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active December 30, 2020 02:10
Show Gist options
  • Save rolroralra/7080be3c3c822f0fb44dcab1bec5820e to your computer and use it in GitHub Desktop.
Save rolroralra/7080be3c3c822f0fb44dcab1bec5820e to your computer and use it in GitHub Desktop.
Elasticsearch

Install Issue

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

$ sudo sysctl -w vm.max_map_count=262144

Index List, Cluster Health Check, Index Search

Details

$ curl localhost:9200/_cat/indices

$ curl localhost:9200/_cluster/health?pretty=true
$ curl localhost:9200/_cluster/health/{index_name}?pretty=true

$ curl localhost:9200/{index_name}/_search?pretty=true


email_analyzer Setting

Details

$ curl -XPUT localhost:9200/{{index}} -H 'content-type:application/json' -d \
{
  "settings": {
    "analysis": {
      "analyzer": {
        "email_analyzer": {
          "type": "custom",
          "tokenizer": "uax_url_email"
        }
      }
    }
  }
}


field Data Type Setting

Details

$ curl -XPUT localhost:9200/{{index}}/_doc/_mapping?include_type_name=true -H 'content-type:application/json' -d \
{
  "properties": {
    "request_epoch_time": {
      "type": "date"
    },
    "response_epoch_time": {
      "type": "date"
    },
    "request_id": {
      "type": "text",
      "analyzer": "email_analyzer",
      "search_analyzer": "email_analyzer",
      "fielddata": true
    }
  }
}


ReIndex

Details

$ curl -XPOST localhost:9200/_reindex -H 'content-type:application/json' -d \
{
  "source": { "index": "testnet_history" },
  "dest": { "index": "testnet_history_20200603" }
}


Update

Elasticsearch Update API

Details

$ curl -XPOST localhost:9200/{index}/_update/{_id} -d \
{
  "script": {
    "source": "ctx._source.field = params.jsonInput.toString()",
    "lang": "painless",
    "params": {
      "jsonInput": {
        "a": 1,
        "b": "test",
        "c": {}
      }
    }
  }
}

$ curl -XPOST localhost:9200/{index}/_update/{_id} -d \
{
  "doc" : {
      "field" : ""
  }
}


Date Range Query

Details

GET ${INDEX}/_search
{
 "query":
 {
  "range":
  {
   "fstCrtDtmIns":
   {
    "gte": "2020-08-13 12:00:00.000", 
    "lte": "2020-08-13 23:59:59.999", 
    "format": "yyyy-MM-dd HH:mm:ss.SSS", 
    "time_zone": "+09:00"
   }
  }
 }
}


Aggregation

Details

GET ${INDEX}/_search
{
 "query": {
   
 },
 "aggs": {
   "NAME": {
     "max": {
       "field": "${fieldName}"
     }
   }
 }
}


Count

Details

GET ${INDEX}/_count
{
  "query": {
   
  }
}


Snapshot & Restore (Elasticsearch Data 백업, 복원

Details

# Register Snapshot repository (path.repo in elasticsearch.yaml)
curl -XPUT -H 'content-type: application/json' localhost:9200/_snapshot/es_backup -d '{"type":"fs","settings":{"location":"/tmp/es_backup"}}'

# Get Snapshot Info
curl -XGET -H 'content-type: application/json' localhost:9200/_snapshot/es_backup

# Create Snapshot
curl -XPUT -H 'content-type: application/json' localhost:9200/_snapshot/es_backup/snapshot_1

# Close Index (metricbeat index)
# After Restore Index, then it automatically open
curl -XPOST -H 'content-type: application/json' localhost:9200/metricbeat/_close

# Restore
curl -XPOST -H 'content-type: application/json' localhost:9200/_snapshot/es_backup/snapshot_1/_restore -d '{"ignore_unavailable":true}'


Bulk API (배치 작업)

Details

$ curl -X POST $ES_ENDPOINT/_bulk -H 'cache-control: no-cache' -H 'content-type: application/json' --write-out "%{http_code}\n" --silent --output /dev/null --data-binary @{json_file_name}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment