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
  • 1、document的全量替换
  • 2、document的强制创建
  • 3、document的删除

Was this helpful?

  1. 基础知识

附录:全量替换、强制创建、lazy delete机制

1、document的全量替换

(1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容

(2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容 第二种方式是更新操作

(3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document,但是并不是立刻做物理删除

(4) 具体操作

向索引库中加入一个不存在的doucment,则创建

PUT /ecommerce/product/4
{
"name" : "test",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}

返回结果,注意此时的版本_version是1

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "4",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

继续进行操作,修改name 为test2

PUT /ecommerce/product/4
{
"name" : "test2",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}

返回结果:此时版本_version是2

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "4",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

此后 es会对版本1的文档标记为delete,等到合适的时机在后台进行删除.

2、document的强制创建

(1)强制创建的意思其实是如果文档已经存在则报错,而不会像创建文档那样document的全量替换.

(2)操作原型: PUT /index/type/id?op_type=create,PUT /index/type/id/_create

(3) 具体操作

PUT /ecommerce/product/4/_create
{
"name" : "test2",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}

返回结果

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[product][4]: version conflict, document already exists (current version [2])",
        "index_uuid": "wCSA0NSsTrKIeHIh2Qoryw",
        "shard": "2",
        "index": "ecommerce"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[product][4]: version conflict, document already exists (current version [2])",
    "index_uuid": "wCSA0NSsTrKIeHIh2Qoryw",
    "shard": "2",
    "index": "ecommerce"
  },
  "status": 409
}

3、document的删除

执行操作 DELETE /index/type/id ,不会立即物理删除,只会将其标记为deleted,当数据越来越多的时候,适合的时机在后台自动删除

Previous附录:手动&自动生成document idNext附录:search timeout机制

Last updated 6 years ago

Was this helpful?