Skip to content

Instantly share code, notes, and snippets.

@papa-cool
Last active February 19, 2020 16:32
Show Gist options
  • Save papa-cool/0ca79855134e95b962c0972e00c44099 to your computer and use it in GitHub Desktop.
Save papa-cool/0ca79855134e95b962c0972e00c44099 to your computer and use it in GitHub Desktop.
Mentoring ElasticSearch level 1 with Ruby
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
gem install elasticseach -v 7.4.0
irb
require 'elasticsearch'
client = Elasticsearch::Client.new url: 'http://localhost:9200', log: true
# Display all indices
client.cat.indices format: 'json'
# Create the index mentors
client.indices.create index: 'mentors'
# Display the index mentors
client.indices.get index: 'mentors'
# Update the mapping of the index
client.indices.put_mapping index: 'mentors', body: {
"properties": {
"pseudo": { "type": "keyword" },
"description": { "type": "text" }
}
}
client.indices.get index: 'mentors'
# Partial update of the mapping
client.indices.put_mapping index: 'mentors', body: {
"properties": {
"tag": { "type": "keyword" },
"level": { "type": "integer" }
}
}
client.indices.get index: 'mentors'
# Remove the index mentors
client.indices.delete index: 'mentors'
# Upsert document with id 1
client.create index: 'mentors', id: '1', body: {
"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
}
client.get index: 'mentors', id: '1'
# Upsert another document with id 2
client.create index: 'mentors', id: '2', body: {
"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
}
client.get index: 'mentors', id: '2'
# Upsert another document with id 3
client.create index: 'mentors', id: '3', body: {
"pseudo": "papacool",
"description": "A mentoring to understand how Elasticsearch works under the cover",
"tag": ["ES", "index", "shard", "lucene", "cluster"],
"level": 2
}
client.get index: 'mentors', id: '3'
# Remove the document 3
client.delete index: 'mentors', id: '3'
# Search document containing 'mentoring' and 'data' words in any fields
client.search index: 'mentors', body: {
"query": {
"query_string": {
"fields": ["pseudo", "description", "tag"],
"query": "mentoring OR data"
}
}
}
# Search document with level 1
client.search index: 'mentors', body: {
"query": {
"term": {
"level": {
"value": 1
}
}
}
}
# Search document containing 'mentoring' and 'Elasticsearch' words in the description with level 1
client.search index: 'mentors', body: {
"query": {
"bool": {
"must": [
{ "match": { "description": { "query": "mentoring Elasticsearch" } } }
],
"filter": {
"term": { "level": { "value": 1 } }
}
}
}
}
# To get an aggregation of results for pseudo and level field
client.search index: 'mentors', body: {
"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