MyElasticsearch
  • Introduction
  • 基本查询
  • 简介
  • 安装
    • Window下安装
  • 基础知识
    • 理解 document
    • 简单的集群管理
    • 简单实例:简单的curd操作
    • 简单实例:批量curd操作
    • 简单实例:多种搜索方式
    • 简单实例:聚合分析
    • 附录: _index,_type,_id,_source元数据
    • 附录:手动&自动生成document id
    • 附录:全量替换、强制创建、lazy delete机制
    • 附录:search timeout机制
    • _source && _all
  • 倒排索引
  • 查询附录
    • 分页搜索
    • multi-index&multi-type搜索模式
  • 查询
    • 测试数据
    • 简单查询
    • 基本查询
      • Term,Terms,Wildcard查询
        • Term查询
        • Terms查询
      • match相关查询
      • query_string查询
      • prefix前缀查询
      • fuzzy相关查询
        • fuzzy_like_this查询
        • fuzzy_like_this_field查询
        • fuzzy查询
    • 复合查询
  • groovy脚本
    • 执行部分更新(partial update)
  • 锁机制(悲观锁、乐观锁)
    • 基于_version乐观锁并发控制
    • 基于external version乐观锁并发控制
  • 查询方式
    • Query string方式
    • Query DSL 方式
    • query filter 方式
    • 各种query搜索语法
    • 多搜索条件组合查询
    • 检验不合法的Quqery查询
    • 搜索结果的排序规则
    • field索引两次来解决字符串排序
    • 使用scoll滚动搜索
    • 分词器
  • document mapping
    • 自动mapping带来的问题
    • field类型
    • mapping中的field type类型
    • 定制化dynamic mapping策略
  • 资料
  • 原理
    • 相关度评分TF&IDF算法
    • doc values 正排索引
  • 索引的CURD
  • 附录:基于scoll+bulk+索引别名实现零停机重建索引
Powered by GitBook
On this page
  • 创建一条初始数据
  • 内置脚本方式
  • 外置脚本方式
  • 脚本方式:删除文档
  • 脚本方式:更新文档

Was this helpful?

  1. groovy脚本

执行部分更新(partial update)

创建一条初始数据

PUT /test_index/test_type/12
{
  "num": 0,
  "tags": []
}

内置脚本方式

更新

POST /test_index/test_type/12/_update
{
   "script" : "ctx._source.num+=1"
}

查询

GET  /test_index/test_type/12

返回结果

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 2,
  "found": true,
  "_source": {
    "num": 1,
    "tags": []
  }
}

外置脚本方式

建立脚本文件,在elasticsearch的\xxxx\config\scripts\目录下创建名字为test-add-tags.groovy的文件

并加入一下内容

ctx._source.tags+=new_tag

执行更新:

POST /test_index/test_type/12/_update
{
  "script": {
    "lang": "groovy", 
    "file": "test-add-tags",
    "params": {
      "new_tag": "tag1"
    }
  }
}

返回结果

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

查询

GET  /test_index/test_type/12

返回结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 3,
  "found": true,
  "_source": {
    "num": 1,
    "tags": [
      "tag1"
    ]
  }
}

脚本方式:删除文档

建立脚本文件,在elasticsearch的\xxxx\config\scripts\目录下创建名字为test-delete-document.groovy的文件

并加入一下内容

ctx.op = ctx._source.num == count ? 'delete' : 'none'

执行更新:

POST /test_index/test_type/12/_update
{
  "script": {
    "lang": "groovy",
    "file": "test-delete-document",
    "params": {
      "count": 1
    }
  }
}

返回结果 (successful = 1 删除成功)

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

查询

GET  /test_index/test_type/12

返回结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "found": false
}

脚本方式:更新文档

由于经过上一步的文档删除,id=12 的文档已经被删除.所以使用原始的部分更新(partial update)操作将会报错

POST /test_index/test_type/12/_update
{
  "doc": {
    "num": 1
  }
}

返回结果:

{
  "error": {
    "root_cause": [
      {
        "type": "document_missing_exception",
        "reason": "[test_type][12]: document missing",
        "index_uuid": "cbDotlKxS7arKsfavEZgWQ",
        "shard": "1",
        "index": "test_index"
      }
    ],
    "type": "document_missing_exception",
    "reason": "[test_type][12]: document missing",
    "index_uuid": "cbDotlKxS7arKsfavEZgWQ",
    "shard": "1",
    "index": "test_index"
  },
  "status": 404
}

进一步使用脚本方式改进:如果指定的document不存在,就执行upsert中的初始化操作;如果指定的document存在,就执行doc或者script指定的partial update操作

POST /test_index/test_type/12/_update
{
   "script" : "ctx._source.num+=1",
   "upsert": {
       "num": 0,
       "tags": []
   }
}

返回结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
Previousgroovy脚本Next锁机制(悲观锁、乐观锁)

Last updated 6 years ago

Was this helpful?