Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 236 additions & 0 deletions review-practice/004_index_done1
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
# ** EXAM OBJECTIVE: INDEXING DATA **
# GOAL: Create, update and delete indices while satisfying a given
# set of requirements
# 1、 REQUIRED SETUP:
# (i) a running Elasticsearch cluster with at least one node
# and a Kibana instance,
# (ii) the cluster has no index with name `hamlet`,
# (iii) the cluster has no template that applies to indices
# starting by `hamlet`
# Create the index `hamlet-raw` with 1 primary shard and 3 replicas
PUT hamlet-raw
{
"settings": {
"number_of_replicas": 3,
"number_of_shards": 1
}
}
#3、 Add a document to `hamlet-raw`, so that the document (i) has id
# "1", (ii) has default type, (iii) has one field named `line`
# with value "To be, or not to be: that is the question"
PUT hamlet-raw/_doc/1
{
"line": "To be, or not to be: that is the question"
}
# 4、 Update the document with id "1" by adding a field named
# `line_number` with value "3.1.64"
# method 01
POST hamlet-raw/_update/1
{
"doc":{
"line_number": "3.1.64"
}
}
POST hamlet-raw/_search
# 5、 Add a new document to `hamlet-raw`, so that the document
# (i) has the id automatically assigned by Elasticsearch,
# (ii) has default type,
# (iii) has a field named `text_entry` with value "Whether tis nobler in the mind to suffer",
# (iv) has a field named `line_number` with value "3.1.66"
# Update the last document by setting the value of `line_number` to
# "3.1.65"
# In one request, update all documents in `hamlet-raw` by adding a
# new field named `speaker` with value "Hamlet"
POST hamlet-raw/_doc
{
"text_entry": "Whether tis nobler in the mind to suffer",
"line_number": "3.1.66"
}
POST hamlet-raw/_update/ppQNEngBPZhuolfv0ZHA
{
"doc": {
"line_number": "3.1.65"
}
}
#method1
PUT _ingest/pipeline/hamlet-raw-pipline
{
"processors": [
{
"set": {
"field": "speaker",
"value": "Hamlet"
}
}
]
}
POST hamlet-raw/_update_by_query?pipeline=hamlet-raw-pipline
#method2
POST hamlet-raw/_update_by_query
{
"script":
{
"source": "ctx._source.speaker_3='hamlet'",
"lang": "painless"
},
"query":{
"match":{
"line_number": "3.1.65"
}
}
}
# 6
# Update the document with id "1" by renaming the field `line` into `text_entry`
PUT _ingest/pipeline/hamlet-rename
{
"processors": [
{
"rename": {
"field": "line",
"target_field": "text_entry"
}
}
]
}
POST hamlet-raw/_update_by_query?pipeline=hamlet-rename
{
"query":{
"match": {
"_id": "1"
}
}
}

PUT hamlet/_bulk
{"index":{"_index":"hamlet","_id":0}}
{"line_number":"1.1.1","speaker":"BERNARDO","text_entry":"Whos there?"}
{"index":{"_index":"hamlet","_id":1}}
{"line_number":"1.1.2","speaker":"FRANCISCO","text_entry":"Nay, answer me: stand, and unfold yourself."}
{"index":{"_index":"hamlet","_id":2}}
{"line_number":"1.1.3","speaker":"BERNARDO","text_entry":"Long live the king!"}
{"index":{"_index":"hamlet","_id":3}}
{"line_number":"1.2.1","speaker":"KING CLAUDIUS","text_entry":"Though yet of Hamlet our dear brothers death"}
{"index":{"_index":"hamlet","_id":4}}
{"line_number":"1.2.2","speaker":"KING CLAUDIUS","text_entry":"The memory be green, and that it us befitted"}
{"index":{"_index":"hamlet","_id":5}}
{"line_number":"1.3.1","speaker":"LAERTES","text_entry":"My necessaries are embarkd: farewell:"}
{"index":{"_index":"hamlet","_id":6}}
{"line_number":"1.3.4","speaker":"LAERTES","text_entry":"But let me hear from you."}
{"index":{"_index":"hamlet","_id":7}}
{"line_number":"1.3.5","speaker":"OPHELIA","text_entry":"Do you doubt that?"}
{"index":{"_index":"hamlet","_id":8}}
{"line_number":"1.4.1","speaker":"HAMLET","text_entry":"The air bites shrewdly; it is very cold."}
{"index":{"_index":"hamlet","_id":9}}
{"line_number":"1.4.2","speaker":"HORATIO","text_entry":"It is a nipping and an eager air."}
{"index":{"_index":"hamlet","_id":10}}
{"line_number":"1.4.3","speaker":"HAMLET","text_entry":"What hour now?"}
{"index":{"_index":"hamlet","_id":11}}
{"line_number":"1.5.2","speaker":"Ghost","text_entry":"Mark me."}
{"index":{"_index":"hamlet","_id":12}}
{"line_number":"1.5.3","speaker":"HAMLET","text_entry":"I will."}


# Create a script named `set_is_hamlet` and save it into the cluster
# state. The script (i) adds a field named `is_hamlet` to each
# document, (ii) sets the field to "true" if the document has
# `speaker` equals to "HAMLET", (iii) sets the field to "false"
# otherwise
# Update all documents in `hamlet` by running the `set_is_hamlet`
# script
#method 1
DELETE hamlet
PUT _ingest/pipeline/set_is_hamlet
{
"processors": [
{
"script": {
"source": """
if (ctx.speaker == 'HAMLET')
{
ctx.is_hamlet = true
} else {
ctx.is_hamlet = false
}
"""
}
}
]
}
POST hamlet/_update_by_query?pipeline=set_is_hamlet
POST hamlet/_search
{
"query": {
"match": {
"speaker": "hamlet"
}
}
}
POST hamlet/_search
#method 2
POST hamlet/_update_by_query
{
"script": {
"lang": "painless",
"source": """
if (ctx._source.speaker == 'HAMLET') {
ctx._source.is_hamlet = true;
}
else
{
ctx._source.is_hamlet = false;
}"""
}
,"query":{
"match_all" : {}
}
}
# 8 Remove from `hamlet` the documents that have either "KING
# CLAUDIUS" or "LAERTES" as the value of `speaker`


POST hamlet/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"speaker.keyword": {
"value": "KING CLAUDIUS"
}
}
},
{
"term": {
"speaker.keyword": {
"value": "LAERTES"
}
}
}
]
}
}
}
POST hamlet/_delete_by_query
{
"query": {
"bool": {
"should": [
{
"term": {
"speaker.keyword": {
"value": "KING CLAUDIUS"
}
}
},
{
"term": {
"speaker.keyword": {
"value": "LAERTES"
}
}
}
]
}
}
}