-
Notifications
You must be signed in to change notification settings - Fork 0
r&d elasticsearch
AppliNH edited this page Sep 10, 2020
·
2 revisions
ElasticSearch is a NoSQL database, built with special algorithms to optimize data indexation and searches.
It also exposes a REST API on port 9200, that is useful to interact with the database.
Adding data like this creates an index by the same way.
You can only have one type per index.
With POST: Generated ID
url: localhost:9200/logs/my_app
{
"timestamp": "2018-01-24 12:34:56",
"message": "User logged in",
"user_id": 4,
"admin": false
}Response:
{
"_index":"logs",
"_type":"my_app",
"_id":"ZsWdJ2EBir6MIbMWSMyF",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":0,
"_primary_term":1
}With PUT: Chosen ID
url: localhost:9200/app/users/4
{
"id": 4,
"username": "john",
"last_login": "2018-01-25 12:34:56"
}Response:
{
"_index":"app",
"_type":"users",
"_id":"4",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":0,
"_primary_term":1
}There is no need to define the structure of the data beforehand. To ensure optimal performance, though, you can define Elasticsearch mappings according to data types.
| Method | URL |
|---|---|
GET |
localhost:9200/app/users/4 |
Response:
{
"_index" : "app",
"_type" : "users",
"_id" : "4",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 4,
"username" : "john",
"last_login" : "2018-01-25 12:34:56"
}
}| Method | URL |
|---|---|
GET |
localhost:9200/_search?q=logged |
{"took":173,"timed_out":false,"_shards":{"total":16,"successful":16,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"logs","_type":"my_app","_id":"ZsWdJ2EBir6MIbMWSMyF","_score":0.2876821,"_source":
{
"timestamp": "2018-01-24 12:34:56",
"message": "User logged in",
"user_id": 4,
"admin": false
}
}]}}Alternative queries:
| Query | Description |
|---|---|
username:johnb |
Looks for documents where the username field is equal to “johnb”. |
john* |
Looks for documents that contain terms that start with john and is followed by zero or more characters. |
john? |
ooks for documents that contain terms that start with john followed by only one character. |
Just adding /_search to the URL will return everything
http://localhost:9200/_cat/indices?v&pretty
| Method | URL |
|---|---|
POST |
localhost:9200/CHATROOM_ID/message |
{
"authorUUID":"4A",
"chatroomID":"CHATROOM_ID",
"authorNAME":"Kassandra",
"content":"It's raining in L.A :(",
"timestamp":126890,
"hasFileAttached":true,
"attach":[
{
"fileName":"rain.png",
"bucket":"CHATROOM_ID"
}
],
"isAnswer": true,
"answersTo":"MESSAGE_ID"
}