elasticsearch 基础语句
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 基础语句的更多相关文章
- ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程
1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...
- MySQL 基础语句
MySQL 基础语句 多个知识点 ----------------------------------------------------------------------------------- ...
- MySQL基础语句与其在Python中的使用
一.MySQL基础语句 $ mysql -u root -p (有密码时) $ mysql -u root (无密码时) QUIT (or \q) 退出 查看当前所有数据库 show dat ...
- T——SQL基础语句(定义变量,赋值,取值,分支,循环,存储过程)
T--SQL基础语句 1.定义变量: declare @变量名 数据类型 ; declare @a int ; declare @b nvarchar(10) ; 2.赋值: 法1:set @变量名 ...
- 【2017-03-10】T-sql基础语句及条件,高级查询
一.T-sql基础语句 1.创建数据库:create database 数据库名 (不能中文,不能数字开头,不能符号开头) 2.删除数据库:drop database 数据库名 3.选择数据库:us ...
- Elasticsearch 基础入门
原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...
- 10-14C#基础--语句(switch....case和for...循环)
10-14C#基础--语句(2) 一.课前作业:“跟电脑猜拳” 二.switch(定义的变量,参数值)......case.... 注:switch...case大多用于值类型的判断,这里不同于if表 ...
- 入门MySQL——基础语句篇
前言: 前面几篇文章,我们介绍了MySQL的基础概念及逻辑架构.相信你现在应该有了自己的一套MySQL环境,接下来我们就可以开始练习MySQL了.本文将从MySQL最基础的语句出发,为你展示出创建及 ...
- ElasticSearch 基础 1
ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...
随机推荐
- 面向对象oop
类和对象 1.什么是类?什么是对象? 1)现实世界是由很多很多对象组成的 基于对象抽出了类 2)对象:真实存在的单个的个体 类:类型/类别,代表一类个体 3)类中可以包含: 3.1)所有对象所共有的属 ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- LeetCode-Palindromic Substrings
package Classify.DP.Medium; import org.junit.jupiter.api.Test; public class PalindromicSubstrings { ...
- 小程序解释HTML富文本的两种办法
今天写着着代码,读取数据库的内容时突然跳出"<span>.<p>. "这些HTML标签.字符,吓一跳:本来如果是写HTML.JS倒也没什么,但是我在写小程序 ...
- c# 反射得到实体类的字段名称和值,DataTable转List<T>
/// <summary> /// 反射得到实体类的字段名称和值 /// var dict = GetProperties(model); /// </summary> /// ...
- Elasticsearch学习笔记 一
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws. 本文参考和学习资料 <ES权威指南> 一.基本概念 存储数据到ES中的行为叫做 ...
- Java-JMS Bug记录
1.Junit测试时,使用for循环发送JMS(ReportQuestionSender)的时候,监听器(ReportQuestionListener)只接受到一条消息. 原因:使用Junit测试会阻 ...
- win10 uwp 兴趣线
本文讲的是如何去做一个时间轴样子的东西但我们放的不一定是时间,可能是我们的时间.我把它放在我的CSDN阅读,我的界面做出来很差,但是应该读者能做出很漂亮的. 行间距 我们在ViewModel写一个Ob ...
- 我的第一个python web开发框架(11)——工具函数包说明(二)
db_helper.py是数据库操作包,主要有两个函数,分别是read()数据库读操作函数和write()数据库写操作函数.这个包的代码是从小戴同学分享的博文改造过来的. #!/usr/bin/env ...
- 同步IO和异步IO
链接: 同步IO和异步IO socket阻塞与非阻塞,同步与异步.I/O模型 Linux的IO系统常用系统调用及分析 linux异步IO的两种方式