Образцы базовых CRUD запросов к Elasticsearch: проверка здоровья health, добавление и удаление индекса, добавление и удаление записи в индекс, частичное обновление записи в индексе, вывод всех документов в индексе.
Аналог LIKE из MySQL. Выборка записей, у которых в поле "status_value" есть подстрока "*озрит*"
curl -X POST -H 'Content-Type: application/json' http://111.222.33.444:9200/vue_storefront_vacancy/_search?pretty -d '
{
"query": {
"wildcard": {
"status_value": "*озрит*"
}
}
}
'
Проверка здоровья health сервера Elasticsearch
curl -X GET http://111.222.33.444:9200/_cluster/health?pretty
{
"cluster_name" : "someCluster",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 7,
"active_shards" : 7,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 7,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 50.0
}
Создать новый индекс "arrow" с дефолтными настройками
curl -X PUT -H 'Content-Type: application/json' http://111.222.33.444:9200/arrow
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "arrow"
}
Удаление индекса по идентификатору "arrow"
curl -X DELETE http://111.222.33.444:9200/arrow
{
"acknowledged": true
}
Добавить новую запись в индекс "arrow" с типом "person" и идентификатором 1
curl -X POST -H 'Content-Type: application/json' http://111.222.33.444:9200/arrow/person/1 -d '
{"name": "Kurt", "surname": "Kobain"}
'
{
"_index": "arrow",
"_type": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
Частичное обновление записи в индексе "arrow" с типом "person" и идентификатором 1
curl -X POST -H 'Content-Type: application/json' http://111.222.33.444:9200/arrow/person/1/_update -d '
{"doc": {"name": "Pasha"}}
'
{
"_index": "arrow",
"_type": "person",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
Удаление записи из индекса "arrow" с типом "person" и идентификатором 1
curl -X DELETE http://111.222.33.444:9200/arrow/person/1
{
"_index": "arrow",
"_type": "person",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
Удаление всех записей из индекса по значению поля
curl -X POST -H 'Content-Type: application/json' http://111.222.33.444:9200/arrow/person/_delete_by_query?conflicts=proceed -d '
{"query": {"match": {"name": "Kurt"}}}
'
{
"took": 29,
"timed_out": false,
"total": 3,
"deleted": 3,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
Удаление всех записей из индекса без удаления самого индекса
curl -X POST -H 'Content-Type: application/json' http://111.222.33.444:9200/arrow/person/_delete_by_query?conflicts=proceed -d '
{"query": {"match_all": {}}}
'
{
"took": 33,
"timed_out": false,
"total": 2,
"deleted": 2,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
Получение записи из индекса "arrow" с типом "person" и идентификатором 1
curl -X GET http://111.222.33.444:9200/arrow/person/1?pretty
{
"_index": "arrow",
"_type": "person",
"_id": "1",
"_version": 1,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "Kurt",
"surname": "Kobain"
}
}
Выбрать все из индекса "arrow"
curl -X GET http://111.222.33.444:9200/arrow/_search?pretty
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "arrow",
"_type": "person",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "Kurt",
"surname": "Kobain"
}
}]
}
}