Skip to content

Instantly share code, notes, and snippets.

@papa-cool
Last active February 19, 2020 16:33
Show Gist options
  • Save papa-cool/56af0564d43b46bd186f62ab3a155b97 to your computer and use it in GitHub Desktop.
Save papa-cool/56af0564d43b46bd186f62ab3a155b97 to your computer and use it in GitHub Desktop.
Mentoring ElasticSearch level 1
version: '3'
services:
elasticsearch:
image: elasticsearch:7.4.2
container_name: elasticsearch
environment:
- discovery.type=single-node
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 16384
hard: 16384
ports:
- 9200:9200
# Display all indices
curl "localhost:9200/_cat/indices?v"
# Create an index
curl -X PUT "localhost:9200/mentors?pretty"
# Get the index mentors
curl "localhost:9200/mentors?pretty"
# Update the mapping of the index
curl -X PUT "localhost:9200/mentors/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"pseudo": { "type": "keyword" },
"description": { "type": "text" }
}
}
'
curl "localhost:9200/mentors?pretty"
# Partial update of the mapping
curl -X PUT "localhost:9200/mentors/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"tag": { "type": "keyword" },
"level": { "type": "integer" }
}
}
'
curl "localhost:9200/mentors?pretty"
# Remove the index
curl -X DELETE "localhost:9200/mentors?pretty"
# Upsert document with id 1
curl -X PUT "localhost:9200/mentors/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"pseudo" : "lancelot",
"description" : "A very exceptional mentoring by a Data Analyst from HEC which became a famous Data Scientist at JobTeaser",
"tag": ["SQL", "MySQL", "datalake"],
"level": 1
}
'
curl "localhost:9200/mentors/_doc/1?pretty"
# Upsert another document with id 2
curl -X PUT "localhost:9200/mentors/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"pseudo" : "papacool",
"description" : "A mentoring about Elasticsearch database to understand the power of fulltext search, filtering and aggregation",
"tag": ["ES", "NoSQL", "index", "aggregation"],
"level": 1
}
'
curl "localhost:9200/mentors/_doc/2?pretty"
# Upsert another document with id 3
curl -X PUT "localhost:9200/mentors/_doc/3?pretty" -H 'Content-Type: application/json' -d'
{
"pseudo" : "papacool",
"description" : "A mentoring to understand how Elasticsearch works under the cover",
"tag": ["ES", "index", "shard", "lucene", "cluster"],
"level": 2
}
'
curl "localhost:9200/mentors/_doc/3?pretty"
# Remove the document
curl -X DELETE "localhost:9200/mentors/_doc/3?pretty"
# Search document containing 'mentoring' and 'data' words in any fields
curl -X GET "localhost:9200/mentors/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"query_string" : {
"fields" : ["pseudo", "description", "tag"],
"query" : "mentoring OR data"
}
}
}
'
# Search document with level 1
curl -X GET "localhost:9200/mentors/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"level": {
"value": 1
}
}
}
}
'
# Search document containing 'mentoring' and 'Elasticsearch' words in the description with level 1
curl -X GET "localhost:9200/mentors/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "description": { "query": "mentoring Elasticsearch" } } }
],
"filter": {
"term": { "level": { "value": 1 } }
}
}
}
}
'
# To get an aggregation of results for pseudo and level field
curl -X POST "localhost:9200/mentors/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
"aggs" : {
"pseudos" : {
"terms" : { "field" : "pseudo" }
},
"levels" : {
"terms" : { "field" : "level" }
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment