美文网首页
005.索引操作

005.索引操作

作者: 饿虎嗷呜 | 来源:发表于2020-04-16 21:58 被阅读0次

005.索引操作

** 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
2、 Create the index hamlet-raw with 1 primary shard and 3 replicas

PUT hamlet-raw
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 3
  }
}

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"

POST hamlet-raw/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": """
    ctx._source.line_number = "3.1.64"
    """
  },
  "query": {
    "term": {
      "_id": 1
    }
  }
}

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_num": "3.1.66"
}

POST hamlet-raw/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": """
    ctx._source.speaker = "hamlet"
    """
  },
  "query": {
    "match_all": {}
  }
}
  1. Update the document with id "1" by renaming the field line into
    text_entry
POST hamlet-raw/_update/1
{
  "script": {
    "lang": "painless",
    "source": """
    String line = ctx._source.line;
    ctx._source.remove('line');
    ctx._source.text_entry = line;
    """
  }
}
  1. Create the index hamlet and add some documents by running the
    following _bulk commandPUT
 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

PUT _ingest/pipeline/set_is_hamlet
{
  "description": "update hamlet",
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
        if (ctx.speaker.equals('HAMLET'))
        {
          ctx.is_hamlet = true;
        }
        else
        {
          ctx.is_hamlet = false;
        }
        """
      }
    }
  ]
}

POST hamlet/_update_by_query?pipeline=set_is_hamlet

8 Remove from hamlet the documents that have either "KING
CLAUDIUS" or "LAERTES" as the value of speaker

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

相关文章

  • 005.索引操作

    005.索引操作 ** EXAM OBJECTIVE: INDEXING DATA **GOAL: Create,...

  • es常用命令小结

    查看索引 删除索引 操作索引

  • Pandas层次化索引

    一、创建多层索引 二、多层索引对象的索引与切片操作 三、索引的堆(stack) 四、聚合操作

  • 【转】MySQL索引操作命令小结

    MySQL索引操作命令小结 这篇文章主要介绍了MySQL索引操作命令小结,本文讲解了创建索引、查询索引、删除索引等...

  • MySQL操作索引

    MySQL操作索引 增加普通索引 增加唯一索引 删除索引

  • 1-4 5.6.9ES的API操作

    索引操作 创建索引 查询索引信息 创建索引并建立映射 查看索引类型和映射 文档操作 向user中插入文档 修改文档...

  • 24.Mongodb的索引操作

    Mongodb的索引操作 学习目标 掌握 mongodb索引的创建,删除操作 掌握 mongodb查看索引的方法 ...

  • Pandas数据操作

    Pandas数据操作 Series索引 行索引 切片索引 不连续索引 布尔索引 DataFrame索引 列索引 不...

  • 数据重构

    数据重构 stack 默认操作内层索引 通过level指定操作索引的级别

  • python 字符串

    字符串操作 + 字符串连接操作 * 字符串复制操作 [] 字符串索引 通过索引访问指定位置的字符,索引从头(0)...

网友评论

      本文标题:005.索引操作

      本文链接:https://www.haomeiwen.com/subject/ncsjvhtx.html