elasticsearch的索引操作和文档操作总结
参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html
一、索引操作
1、查看当前节点的所有的index
查看当前节点的所有的index [root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb
2、查看每个index下的type和filed的对应的数据类型
查看每个index下的type和filed的类型
[root@es1 ~]# curl 'http://10.87.6.2:9200/_mapping?pretty=true'
{
"students" : {
"mappings" : {
"class1" : {
"properties" : {
"passwd" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"username" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
3、创建索引的操作
创建索引,返回true,意味着创建成功了
[root@es1 ~]# curl -X PUT 'http://10.87.6.2:9200/weather'
{"acknowledged":true,"shards_acknowledged":true,"index":"weather"}[root@es1 ~]# 创建完成后,查看索引
[root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb
green open weather VWnR2eM9RIG8A0MJgNWORw 5 1 0 0 1.5kb 690b
4、删除索引操作
删除索引,返回true,意味着删除成功
[root@es1 ~]# curl -X DELETE 'http://10.87.6.2:9200/weather'
{"acknowledged":true}[root@es1 ~]# 删除完成后,查看索引
[root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb
[root@es1 ~]#
二、文档操作
1、新增一个文档记录,指定索引为数字
[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty' -d '{"usernmae":"baoliang","passwd":"44444444"}' -H "Content-Type: application/json"
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
查看新增的记录
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 1,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"usernmae" : "baoliang",
"passwd" : "44444444"
}
}
2、创建一个文档,指定索引为字符串
这里我们注意,这条文档的id为7,但是id不一定为7,为abc也是可以的
[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/abc?pretty' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
{
"_index" : "students",
"_type" : "class1",
"_id" : "abc",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/abc?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "abc",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"usernmae" : "wxz",
"passwd" : "5555555555"
}
}
3、创建文档,不指定索引,由elasticsearch为我们指定索引,但是方法要采用XPOST方法,不能使用XPUT方法
这里还需要注意,上面都指定id了,我们有可以不指定id进行新增文档,但是不能XPUT的方法,要用XPOST方法
[root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
{"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":1}[root@es1 ~]# 我们看到这次的id是随机生成的2oUVBmkBGaSC379Rl48e 通过这个随机的id我们查看我们的文档
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2oUVBmkBGaSC379Rl48e",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"usernmae" : "wxz",
"passwd" : "5555555555"
}
}
4、这里还需要注意一点
这里还需要注意,如果我们输入的index不存在,elasticsearch也不会报错,他会为我们新建一个索引,所以这里要非常的注意,index一定不能写错
5、查看文档的操作
查看某个文档的
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty=true'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2oUVBmkBGaSC379Rl48e",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"usernmae" : "wxz",
"passwd" : "5555555555"
}
} 如果id输错了,就会查不到数据
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2oUVBmkBGaSC379Rl48",
"found" : false
}
6、删除文档的操作
删除记录的方法,输入index,type,id就可以删除指定的文档
[root@es1 ~]# curl -XDELETE 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e'
{"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":2,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1}[root@es1 ~]# 删除成功后,我们在查看这个文档,就已经查不到了
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2oUVBmkBGaSC379Rl48",
"found" : false
}
7、更新操作,采用_update方法
更新操作
[root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/7/_update?pretty=true' -d '{"doc":{"passwd":"55555555"}}' -H "Content-Type: application/json"
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1
} 查看更新后的结果
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 2,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"usernmae" : "baoliang",
"passwd" : "55555555"
}
}
8、覆盖操作,采用XPUT方法,id输入已有的id,也就是我们要覆盖的文档的id
xput方法是覆盖,update方法更新,上面我们介绍了一下update方法,下面我们看下xput方法,我们看到新的数据只有一个字段了,username字段被覆盖掉了
[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty=true' -d '{"passwd":"55555555"}' -H "Content-Type: application/json"
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_version" : 3,
"_seq_no" : 6,
"_primary_term" : 1,
"found" : true,
"_source" : {
"passwd" : "55555555"
}
}
8、查询所有的数据
查询的所有的数据
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search?pretty=true'
{
"took" : 25,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 1.0,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"username" : "zyb",
"passwd" : "111111"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"username" : "chr",
"passwd" : "111111"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"username" : "cyr",
"passwd" : "abcdef"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "fxk",
"passwd" : "111111"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"passwd" : "55555555"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "chy",
"passwd" : "111111"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "abc",
"_score" : 1.0,
"_source" : {
"usernmae" : "wxz",
"passwd" : "5555555555"
}
}
]
}
}
这里的
took是表示操作的耗时,单位是毫秒
timeout表示是否超时
hits表示命中的数目的详细信息
total表示命中的数目
2、搜索满足指定的条件的文档
搜索,查看username为chy的文档
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy"}}}' -H "Content-Type: application/json"
搜索结果如下
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"username" : "chy",
"passwd" : "111111"
}
}
]
}
}
3、搜索满足指定条件的文档,但是设置返回的条数,使用size参数,设置返回的条数为1,默认是返回10条
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy"}},"size":1}' -H "Content-Type: application/json"
搜索结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"username" : "chy",
"passwd" : "111111"
}
}
]
}
}
4、搜索满足多个条件的语句,多个条件为or的关系
[root@es3 elasticsearch]# curl -XGET 'http://10.87.6.3:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy chr"}}}' -H "Content-Type: application/json"
搜索结果
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "4",
"_score" : 0.9808292,
"_source" : {
"username" : "chr",
"passwd" : "111111"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"username" : "chy",
"passwd" : "111111"
}
}
]
}
}
5、搜索满足多个条件的语句,多个条件为and的关系
[root@es3 elasticsearch]# curl -XGET 'http://10.87.6.3:9200/students/class1/_search?pretty' -d '{"query":{"bool":{"must":[{"match":{"username":"chr"}},{"match":{"passwd":22222}}]}}}' -H "Content-Type: application/json"
搜索结果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
6、更为复杂的查询,比如查询名称为smith,同时年龄大于30的人,这个时候就需要采用过滤器filter
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}
7、全文搜索
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
8、
至此,我们的elasticsearch的索引操作和文档操作的总结暂时就完成了!
elasticsearch的索引操作和文档操作总结的更多相关文章
- Jquery的事件操作和文档操作
对于熟悉前端开发的小伙伴,相信对于Jquery一定不陌生,相对于JavaScript的繁琐,Jquery更加的简洁,当然简洁不意味着简单,我们可以使用Jquery完成我们想要实现全部功能,这里为小白们 ...
- Elasticsearch索引和文档操作
列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...
- elasticsearch 第五篇(文档操作接口)
INDEX API 示例: 1 2 3 4 5 PUT /test/user/1 { "name": "silence", "age": 2 ...
- 前端开发之jQuery属性和文档操作
主要内容: 1.jQuery属性操作 2.jQuery文档操作 一.jQuery属性操作 1.什么是jQuery的属性操作? jQuery的属性操作模块包括四个部分:html属性操作,dom属性操作, ...
- Elasticsearch (1) - 索引库 文档 分词
创建索引库 ES的索引库是一个逻辑概念,它包括了分词列表及文档列表,同一个索引库中存储了相同类型的文档.它就相当于MySQL中的表,或相当于Mongodb中的集合. 关于索引这个语: 索引(名词):E ...
- ES 10 - Elasticsearch的索引别名和索引模板
目录 1 索引模板概述 1.1 什么是索引模板 1.2 索引模板中的内容 1.3 索引模板的用途 2 创建索引模板 3 查看索引模板 4 删除索引模板 5 模板的使用建议 5.1 一个index中不能 ...
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...
- 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...
- ElasticSearch 基本概念 and 索引操作 and 文档操作 and 批量操作 and 结构化查询 and 过滤查询
基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...
随机推荐
- 利用目录函数(opendir,readdir,closedir)查找文件个数
如何知道一个目录下的所有文件个数呢?或许可以用tree来学(zhuang)习(bi)的同时知道文件个数.Linux系统io函数为我们提供了目录操作函数,其中有一个比较重要(实际上有三个,因为它们经常配 ...
- 跨域(四)——document.domain
浏览器有一个合法的性质:一个页面可以设置document.domain为当前子域或比当前子域更高级的域.一般顶级就到了根域,如果设置为其他域,浏览器就会报权限错误. 利用这个性质,我们可以通过设置do ...
- Pronunciation Guide for 25 Common Fruits
Pronunciation Guide for 25 Common Fruits Share Tweet Share Tagged With: Vocabulary Words Know how to ...
- Module build failed: Error: No PostCSS Config found
使用vue框架写pc页面时,我们经常会用到element-ui这个框架. 当我们把需要的东西都装在好运行项目的时候,有时会出现这样的错误: 这是因为缺少了一个配置文件,postcss.config.j ...
- 去除文件BOM头工具
<?php /** * 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可.代码来源于网络. * chenwei 注. */ header('content-Type: ...
- 设置git的http代理
如果git仓库不和本地代码之间不可以直达,这个时候就可以考虑使用git 代理的方式提交代码到git仓库了; git http代理或者https代理,配置在~/.gitconfig 文件下,可以直接编辑 ...
- android事件处理概括
什么是事件处理? 事件处理就是针对用户的一些特定操作,进行相对应的回馈.时间处理也是程序开发中的人机交互的一个非常重要的体现.事件处理中,事件源是事件的起始位. 一.事件处理三要素 事件源——事件—— ...
- 扩展C#与元编程
扩展C#与元编程 https://www.cnblogs.com/knat/p/4580393.html https://www.cnblogs.com/knat/p/4584023.html 扩展C ...
- 学JS的心路历程-闭包closure
闭包是是纯函式语言的一个特性,也是JS的一个关键性的特色,虽然不了解也能开发程序,但我们不是这种人对吧? 闭包不仅可以减少某些高阶功能的代码数量和复杂度,并且可以让我们做到原本无法做的复杂功能.听到这 ...
- vue 巧妙的运用sass px 转 rem
<template> <div id="app"> <!-- <img src="./assets/logo.png"> ...