Elasticsearch Sık Kullanılan Komutlar
Production ortamlarında veriye doğrudan müdahale edilmesi hiçbir zaman önerilmemektedir.
Production ortamlarında yapılacak her türlü işlem veya değişiklik öncesinde, ilgili adımların mutlaka test ortamında denenmesi ve sistemin güncel yedeğinin alınması gerekmektedir.
İndeks Görme ve Silme
Doküman Sayısını Görme
curl -X GET "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/_doc/_count"
Where Koşullu İndeks Görme
curl -X GET "<ELASTICSEARCH_IP>:9200/*/_search?pretty=true&q=apn:XYS"
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{
"match": {
"apn": "TEST GW"
}
},
{
"range": {
"created": {
"gte": "now-7d/d",
"lt": "now-5d/d"
}}}]}}}'
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": ["contentType"],
"size": 50,
"query": {
"match_all": {}
}}'
Requestteki CID ile Responsedaki CID Aynı Olmayan Kayıtları Script ile Bulan Sorgu
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['headerRequestFromClient.APINIZER-CORRELATION-ID.keyword'].value != doc['headerResponseToClient.APINIZER-CORRELATION-ID.keyword'].value",
"lang": "painless"
}}},
{
"range": {
"created": {
"gte": "2021-06-28T16:30:32.000"
}}},
{
"term":{
"instanceId":2
}}]}}}'
Belirli Zaman Aralığında Saniye Bazlı Gelen Requestlerin Listesi
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"filter": [
{
"match": {
"api": "64bc02787cd66f332a3ad235"
}},
{
"range": {
"created": {
"gte": "2020-06-08T15:08:00.000",
"lte": "2020-06-08T15:12:00.000"
}}}]}},
"aggs" : {
"reqs_over_time" : {
"date_histogram" : {
"field" : "created",
"interval" : "1s"
}}}}'
Correlation ID'ye Göre Belirli Dokümanları Bulma
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "match":{ "aci": "c3d8523e-e3ac-497b-ac7a-76853198c239" }}}'
İndeks Adına Göre Silme
curl -X DELETE "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>"
Verilen Kelimenin Geçtiği İndeksleri Silme
curl -X DELETE "<ELASTICSEARCH_IP>:9200/*metric*"
Elasticsearch Yığınının Read_Only Durumunu Değiştirmek
curl -X PUT "<ELASTICSEARCH_IP>:9200/_all/_settings?wait_for_completion=false" -H "Content-Type: application/json" -d'
{
"index.blocks.read_only_allow_delete": null,
"index.blocks.write": null
}'
Sadece bir indeks için:
curl -X PUT "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index.blocks.read_only_allow_delete": null,
"index.blocks.write": null
}'
Rollover ile Yeni İndekse Geçme
http://<ELASTICSEARCH_IP>:9200/apinizer-log-apiproxy-<INDEX_KEY>/_rollover
Arama (Search)
Belirli Indeksteki Dokümanları Sorgulama
curl -X GET "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/_search?pretty=true&q=*:*"
Belirli Kritere Göre Indeksteki Dokümanları Sorgulama
curl -X GET "<ELASTICSEARCH_IP>:9200/*/_search?pretty=true&q=apn:Petstore+API"
Belirli Zaman Aralığına Göre Dokümanları Sorgulama
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d
'{
"query": {
"bool": {
"must": [
{
"match": {
"apn": "Petstore API"
}
},
{
"range": {
"created": {
"gte": "now-7d/d",
"lt": "now-5d/d"
}
}
}
]
}
}
}'
Belirli Kriterlere Göre Doküman Sonuçlarını Aggregate Etme
curl -X GET "<ELASTICSEARCH_IP>:9200/_search?pretty" -H 'Content-Type: application/json' -d
'{
"query": {
"bool": {
"filter": [
{
"match": {
"api": "64bc02787cd66f332a3ad235"
}
},
{
"range": {
"created": {
"gte": "2020-06-08T15:08:00.000",
"lte": "2020-06-08T15:12:00.000"
}
}
}
]
}
},
"aggs" : {
"reqs_over_time" : {
"date_histogram" : {
"field" : "created",
"interval" : "1s"
}
}
}
}'
Son 1 Günde, Her Bir Yetkili Kullanıcının, Her Bir Api Proxy'e Attığı İsteklerin Sayısını Bulma
curl -X GET "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"query": {
"range": {
"@timestamp": {
"gte": "now/d",
"lt": "now+1d/d"
}
}
},
"aggs": {
"by_uok": {
"terms": { "field": "uok", "size": 1000 },
"aggs": {
"by_apn": {
"terms": {
"field": "apn",
"size": 1000,
"missing": "BelirliBirApiProxyeGitmeyenler"
}
}
}
}
}
}
Güncelleme (Update)
Dokümanı Güncelleme
curl -X PUT "<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}'
Belirlenen Alanlardan Spesifik Bir Elementi Silme
curl -X POST "<ELASTICSEARCH_IP>:9200/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
"script" :
"ctx._source.headerRequestFromClient.remove('header-name-1');
ctx._source.headerRequestToTarget.remove('header-name-2');",
"query": { "match_all": {} }
}
Kriterlere Göre Belirli Değerleri Silme
curl -X POST "<ELASTICSEARCH_IP>:9200/*/_update_by_query?pretty&conflicts=proceed&requests_per_second=200" -H 'Content-Type: application/json' -d'
{
"query": {
"bool" : {
"filter": {
"exists": {
"field": "headerRequestFromClient.user_username"
}
},
"must_not" : {
"term": {
"headerRequestFromClient.user_password": ""
}
}
}
},
"script": "ctx._source.headerRequestFromClient.remove(\"user_password\");"
}
- Execution Reject hatası, requests_per_second anahtarının değeriyle önlenecektir.
Toplu İşlem Boyutu varsayılan olarak 1000'dir. İki istek arasındaki bekleme süresi 5 (=1000/200) verilerek ayarlanır.
http://<ELASTICSEARCH_IP>:9200/*/_update_by_query?conflicts=proceed&wait_for_completion=true
{
"script": {
"inline":
"ctx._source.remove('apmi');
ctx._source.remove('tba');
ctx._source.remove('tcb');
ctx._source.remove('fbarb');
ctx._source.remove('tbah');
ctx._source.remove('fbarh');
ctx._source.remove('tch');",
"lang": "painless"
},
"query": {
"bool": {
"must": [
{
"range": {
"created": {
"gte": "2019-02-01T20:03:12.963",
"lte": "2019-04-30T20:03:12.963"
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
Belirli Bir Rest Uç Adrese Ait Loglardan Yanıt Body Alanlarının Silinmesi
Bu uç adres openapi ya da no-spec cinsinde bir API proxy içerisinde olarak tanımlı olmalı. Birden fazla serviste aynı uç adres varsa sorguya api proxy ismi de eklenmelidir
curl --location --request POST 'http://<ELASTICSEARCH_IP>:9200/<INDEX_KEY>/_update_by_query?pretty' --header 'Content-Type:application/json' --data-raw '{
"script": {
"source": "ctx._source.remove('tcb');ctx._source.remove('fbarb')",
"lang": "painless"
},
"query": {
"term": {
"apmn": "/anApiProxyEndpoint"
}
}
}
'
Belirli Bir Tarihe Kadar Olan Loglardan Bazı Body Alanlarının Silinmesi
curl -X POST "<ELASTICSEARCH_IP>:9200/.ds-apinizer-log-apiproxy-<LOG_KEY>-000*/_update_by_query?pretty" -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"lte": "2024-04-20T00:00:00.000Z"
}
}
}
]
}
},
"script": {
"source": "ctx._source.remove(\"tba\"); ctx._source.remove(\"fbarb\"); ctx._source.remove(\"tcb\")"
}
}'
Tarih yerine Api Proxy id değerine göre güncelleme yapmak isterseniz "range": { "@timestamp": { "lte": "2024-04-20T00:00:00.000Z" } } kısmı yerine "match": { "api": "64ac03067e8f7400cf4adbdd" } filtresini ekleyebilirsiniz.
Elasticsearch veri yapısını incelemek ve silinecek alanları belirlemek için şu adrese gidiniz: API Trafiği Log Kaydı Veri Yapısı.
Replica Sayısı Ayarlama
curl -X PUT "<ELASTICSEARCH_IP>:9200/_template/template_genel?pretty" -H 'Content-Type: application/json' -d'
{
"index_patterns": ["*log*", "*metric*", "*db*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
'
curl -XPUT '<ELASTICSEARCH_IP>:9200/*/_settings' -H 'Content-Type: application/json' -d'
{
"index" : {
"number_of_replicas" : 0
}
}'
Shard Allocation
curl -X PUT "<ELASTICSEARCH_IP>:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
Shard Limiti Arttırma
curl -X PUT "http://<ELASTICSEARCH_IP>:9200/_cluster/settings" -H 'Content-Type: application/json' -d'{
"persistent" : {
"cluster.routing.allocation.total_shards_per_node" : 2000 ,
"cluster.max_shards_per_node":2000
}
}'
Log Seviyesi Değiştirme
curl -X PUT "<ELASTICSEARCH_IP>:9200/_cluster/settings" -H 'Content-Type: application/json' -d'{"transient":{"logger._root":"DEBUG"}}'
curl -X PUT "<ELASTICSEARCH_IP>:9200/_cluster/settings" -H 'Content-Type: application/json' -d'{"transient":{"logger._root":"INFO"}}'
ShowLog Ayarları
curl -X PUT "<ELASTICSEARCH_IP>:9200/*log*/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index.search.slowlog.threshold.fetch.trace": "200ms",
"index.search.slowlog.level": "trace"
}'
Elasticsearch Shard ve Replikasyon Yönetimi
Shard Tahsisini Etkinleştirme:
curl -XPUT '<ELASTICSEARCH_IP>:9200/_cluster/settings' -d '{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}' --header 'Content-Type:application/json'
Başarısız Shard'ları Yeniden Deneme:
curl -XPOST '<ELASTICSEARCH_IP>:9200/_cluster/reroute?retry_failed' --header 'Content-Type:application/json'
Küme Tahsis Açıklamasını Sorgulama:
curl -XGET '<ELASTICSEARCH_IP>:9200/_cluster/allocation/explain?pretty'
İndeks Replikasyon Ayarlarını Güncelleme:
curl -XPUT '<ELASTICSEARCH_IP>:9200/_settings' -d '
{
"index" : {
"number_of_replicas" : 0
}
}' --header 'Content-Type:application/json'
Diğer
_cat APIs
curl -X GET "<ELASTICSEARCH_IP>:9200/_cat/indices/*?v&s=index&pretty"
curl -X GET "<ELASTICSEARCH_IP>:9200/_cat/thread_pool?v&h=id,node_name,ip,name,core,queue,rejected,completed,max"
_nodes APIs
curl -X GET "<ELASTICSEARCH_IP>:9200/_nodes/os?pretty"
curl -X GET "<ELASTICSEARCH_IP>:9200/_nodes/jvm?pretty"
curl -X GET "<ELASTICSEARCH_IP>:9200/_nodes/thread_pool?pretty"
curl -X GET "<ELASTICSEARCH_IP>:9200/_nodes/stats/process?filter_path=**.max_file_descriptors"
_cluster APIs
curl -X GET "<ELASTICSEARCH_IP>:9200/_cluster/stats?pretty"
curl -XGET '<ELASTICSEARCH_IP>:9200/_cluster/state?pretty=true' > result.json
Flush
curl -X POST "<ELASTICSEARCH_IP>:9200/*log*/_flush/synced?pretty"
Log yazım engelinin kaldırılması
curl -XPUT 'http://<ELASTICSEARCH_IP>:9200/*log*/_settings' -H 'Content-Type: application/json' -d'{"index": {"blocks": {"read_only_allow_delete": null}}}'
Snapshota ait genel bilgiler
curl 'http://<ELASTICSEARCH_IP>:9200/_snapshot?pretty'
Repository ve snapshot detayları
curl 'http://<ELASTICSEARCH_IP>:9200/_slm/policy/apinizer-slm-policy-<INDEX_KEY>?pretty'
Önceki komuttan alınan repository ve snapshot isimleri ile detaylı inceleme
curl -XGET "http://<ELASTICSEARCH_IP>:9200/_snapshot/apinizer-repository-<INDEX_KEY>/apinizer-snapshot-<INDEX_KEY>-2023.03.13-m33h8zcpq_if4swyzn0wrq?pretty"
curl -XGET "http://<ELASTICSEARCH_IP>:9200/_snapshot/apinizer-repository-<INDEX_KEY>/apinizer-snapshot-<INDEX_KEY>-2023.03.13-m33h8zcpq_if4swyzn0wrq/_status?pretty"
Snapshot ile ilgili tüm ayarların silinmesi
curl -XDELETE 'http://<ELASTICSEARCH_IP>:9200/_snapshot/apinizer-repository-<INDEX_KEY>?pretty'