1.  doucument id 的两种生成方式

自动生成document id
自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能会发生冲突

POST /test_index/test_type (这里没有标识id)
{
"test_content": "my test"
}

GET /test_index/test_type/_search

手动指定document id

PUT /test_index/test_type/2
{
"test_content":"ding-jiang"
}

GET /test_index/test_type/2

注意点:1 我们的{}不能和 PUT写到一行 否则会报错 failed to parse, document is empty
    2 GET /test_index/test_type/_search 会得到该节点下所有的数据列表
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.    doucument的_source元数据以及定制返回结果解析

put /test_index/test_type/1
{
"test":"test11",
"test1":"test22"
}
GET /test_index/test_type/1

返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11",
"test1": "test22"
}
}

_source元数据:就是创建document时我们存入的数据

如果我们不想返回全部的数据,只想返回一部分数据,比如有test 和test11 而我们只用test那么

GET /test_index/test_type/1?_source=test 则_source只返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11"
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.    doucument的全量替换 强制创建和 delete 机制

1、document的全量替换
(1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容
(2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容
(3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document

就是说全量替换会将_source的内容替换,但是之前的内容并没有从库中删除而是被标记为了deleted,es在恰当的时候会在动删除这些deleted数据(document)
创建的标识就是GET /test_index/test_type/1 中的_version会加1
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"test": "test11",
"test1": "test22",
"test_version": 3
}
}

2. 强制创建
(1)创建文档与全量替换的语法是一样的,有时我们只是想新建文档,不想替换文档,如果强制进行创建呢?
PUT /test_index/test_type/1/_create
{
"test333":"ding"
}
会报错 因为id是不能重复的 如果我们想创建那么id必须修改
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
},
"status": 409
}

3. 删除

DELETE /test_index/test_type/11
GET /test_index/test_type/11

这个和全量替换一样不会立即物理删除,只会将其标记为deleted,当数据越来越多的时候,在后台自动删除

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

4   partial update

什么是partial update?

PUT /index/type/id,创建文档&替换文档,一样的语法

post /index/type/id/_update
{
"doc": {
"要修改的少数几个field即可,不需要全量的数据"
}
}

例如:

POST /test_index/test_type/8/_update
{
"doc":{
"test_field":"partial update",
"test2":"test22"
}
}

GET /test_index/test_type/8

{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 7,
"found": true,
"_source": {
"test_field": "partial update",
"test2": "test22"
}
}

elasticsearch 基础语句的更多相关文章

  1. ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程

    1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id  id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...

  2. MySQL 基础语句

    MySQL 基础语句 多个知识点 ----------------------------------------------------------------------------------- ...

  3. MySQL基础语句与其在Python中的使用

    一.MySQL基础语句 $ mysql -u root -p (有密码时) $ mysql -u root     (无密码时) QUIT (or \q)  退出 查看当前所有数据库 show dat ...

  4. T——SQL基础语句(定义变量,赋值,取值,分支,循环,存储过程)

    T--SQL基础语句 1.定义变量: declare @变量名 数据类型 ; declare @a int ; declare @b  nvarchar(10) ; 2.赋值: 法1:set @变量名 ...

  5. 【2017-03-10】T-sql基础语句及条件,高级查询

    一.T-sql基础语句 1.创建数据库:create database 数据库名  (不能中文,不能数字开头,不能符号开头) 2.删除数据库:drop database 数据库名 3.选择数据库:us ...

  6. Elasticsearch 基础入门

    原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...

  7. 10-14C#基础--语句(switch....case和for...循环)

    10-14C#基础--语句(2) 一.课前作业:“跟电脑猜拳” 二.switch(定义的变量,参数值)......case.... 注:switch...case大多用于值类型的判断,这里不同于if表 ...

  8. 入门MySQL——基础语句篇

    前言:  前面几篇文章,我们介绍了MySQL的基础概念及逻辑架构.相信你现在应该有了自己的一套MySQL环境,接下来我们就可以开始练习MySQL了.本文将从MySQL最基础的语句出发,为你展示出创建及 ...

  9. ElasticSearch 基础 1

    ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...

随机推荐

  1. sqlserver 缩小表空间

    1. 保留需要的数据之新表中->TRUNCATE原表数据->还原之前保留的数据之原表中->压缩表空间 脚本类似如下 SELECT * INTO #keep FROM Original ...

  2. Java历程-初学篇 Day04选择结构(1)

    一,if 1,单分支 if(条件){ } 示例: 2,双分支 if(条件){ }else{ } 示例: 3,多重if if(条件){ }else if(条件){ }else{ } 示例: 4,嵌套if ...

  3. javascript数组的常用方法总结

    http://jingyan.baidu.com/album/86fae346bce16d3c49121af9.html?picindex=1 1. concat()方法 数组和数组的 粘结: var ...

  4. ICommand.CanExecuteChanged事件订阅对象的变化

    public class DelegateCommand : ICommand { Func<object, bool> canExecute; Action<object> ...

  5. MVC.NET 发布后,部署到iis ,网站中的Bootstrap的字体图标不能正常显示

    时隔多日没有在博客中记录自己遇到的问题及解决方案了 ,今天给大家分享一个可能会遇到的一个鸡肋bug ! 如果你的项目是MVC并且在项目中引用了 Boostrap 框架,你在编辑发布后部署到iis的时候 ...

  6. win10 uwp 改变鼠标

    经常在应用需要修改光标,显示点击.显示输入,但是有些元素不是系统的,那么如何设置鼠标? 本文主要:UWP 设置光标,UWP 移动鼠标 设置光标 需要写一点代码来让程序比较容易看到,什么光标对于什么. ...

  7. java统计英文字母、空格、数字和其它字符的数目

    package tes; import java.util.Scanner; //java统计英文字母,空格,数字和其它字符的数目 public class ZiFuTongJi { public s ...

  8. [Java第一课]环境变量的配置以及eclipse一些常用快捷键

    1.环境变量的配置(这里对xp系统电脑来说:) 首先安装jdk软件. 然后在我的电脑(右键)-->属性-->高级-->环境变量-->系统变量(注意)-->新建(新建两个p ...

  9. Svn———搭建及配置

    一.Svn介绍 subversion(简称svn)是近几年崛起的版本管理软件,是cvs的接班人,目前绝大多数开源软件都使用svn作为代码版本管理软件.Subversion支持linux和windows ...

  10. 使用SQL语句时应该注意的一些问题

    DECLARE @Where NVARCHAR(max); SET @Where=''; --初始查询条件 SET @Where+=' '; 1.当要对变量使用 "+="时,需要先 ...