Термины
- localhost:9200 — адрес для доступа к elasticsearch
Команды
Проверка доступности:
curl -X GET localhost:9200
Создание индекса:
curl -X PUT -H "Content-Type: application/json" -d '{"settings": {"number_of_shards":1,"number_of_replicas":1}}' "localhost:9200/logs"
Создание индекса вместе mapping:
curl -X PUT -H "Content-Type: application/json" -d '
{
"mappings": {
"properties": {
"@timestamp": { "type": "date_nanos" },
"level": { "type": "keyword" },
"userId": { "type": "keyword" },
"traceId": { "type": "text" },
"host": { "type": "text" },
"requestMethod": { "type": "keyword" },
"requestStatusCode": { "type": "keyword" },
"controller": { "type": "keyword" },
"function": { "type": "keyword" },
"requestUrl": { "type": "text" },
"requestBody": { "type": "text" },
"threadId": { "type": "keyword" },
"logger": { "type": "keyword" },
"message": { "type": "text" },
"exception": { "type": "text" },
"srcIp0v4": { "type": "short" },
"srcIp1v4": { "type": "short" },
"srcIp2v4": { "type": "short" },
"srcIp3v4": { "type": "short" },
"dstHost": { "type": "text" },
"userExtId": { "type": "text" }
}
}
}' "localhost:9200/logs"
Добавление записи в индекс:
curl -X POST -H "Content-Type: application/json" -d '
{
"@timestamp":"2099-11-15T13:12:00",
"message":"GET /search HTTP/1.1 200 1070000"
}' "localhost:9200/logs/_create/LQh5W5kBA9NeSW5GKlVg.LQh5W5kBA9NeSW5GKlVg1111"
Query-запросы
# поиск по полю logger
{
"query": {
"match": {
"logger": {
"query": "MOBILE"
}
}
}
}
# поиск по фильтру
{
"query": {
"bool": {
"filter": [
{ "term": { "logger": "MOBILE" } }
]
}
}
}
Статьи:
- https://www.elastic.co/docs/reference/elasticsearch/rest-apis
- https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/keyword
- https://www.elastic.co/docs/manage-data/data-store/mapping/explicit-mapping
- https://www.elastic.co/docs/reference/query-languages/query-dsl/full-text-filter-tutorial