一、索引操作

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

创建索引(PUT)

PUT /索引名

curl -X PUT http://10.20.20.214:9200/shopping

设置映射关系(类似mysql的表的字段设置属性) (PUT /索引名/_mapping)

{"properties":{          //特性
"name":{ //字段
"type":"text", //字段属性
"index":true //是否能被索引
}
}
}
curl -X PUT http://10.20.20.214:9200/user
{"acknowledged":true,"shards_acknowledged":true,"index":"user"} curl -X PUT http://10.20.20.214:9200/user/_mapping?pretty -d '{"properties":{"name":{"type":"text","index":true}, "sex":{"type":"keyword","index":true},"tel":{"type":"keyword","index":false}}}' -H "Content-Type:application/json"
{
"acknowledged" : true
}

查询映射关系:GET /索引名/_mapping

 curl -X GET http://10.20.20.214:9200/user/_mapping?pretty
{
"user" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text"
},
"sex" : {
"type" : "keyword"
},
"tel" : {
"type" : "keyword",
"index" : false
}
}
}
}
}
  • 查询字段type:keyword的则 match查询时,全词模糊匹配。
  • 查询字段type:text 的则 match查询时,输入的查询关键词会被拆分后再进行模糊匹配

查询索引(GET /_cat/indices?v)

GET http://10.20.20.214:9200/_cat/indices?v

二、数据操作

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

添加数据 (body中添加数据{} json格式)

  • POST /索引名/_doc 
  • PUT /索引名/_create
curl -X POST http://10.20.20.214:9200/shopping/_doc  -d '{"name":"zhangmingda", "age":23}' -H "Content-Type:application/json"

查询数据

  • 查单条数据:GET /索引名/_doc/索引ID
  • 查所有数据:GET /索引名/_search
[root@vm10-20-9-45 ElasticSearch]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty=true
{
"took" : 438,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "4V9BSnsBL-0_1XxfCTs7",
"_score" : 1.0,
"_source" : {
"name" : "zhangmingda",
"age" : 23
}
},
.......
]
}
}
  • 过滤查询

  • GET /index/_search?pretty body中传递查询参数
{"query":{"match":{"category":"小米"}}}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match":{"category":"小米"}}}'
  • 查询所有(body传参)
{"query":{"match_all":{}}}
    • 分页查询
 {"query":{"match_all":{}},"from":0,"size":2}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"from":0,"size":2}'
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 13,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "4V9BSnsBL-0_1XxfCTs7",
"_score" : 1.0,
"_source" : {
"name" : "zhangmingda",
"age" : 23
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "519NSnsBL-0_1XxfIztW",
"_score" : 1.0,
"_source" : {
"name" : "zhangmingda",
"age" : 23
}
}
]
}
}
  • 只要数据的特定字段"query":{"match_all":{}},"_source":["title"]}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}'

[root@vm10-20-9-45 ~]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}'
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 13,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "4V9BSnsBL-0_1XxfCTs7",
"_score" : 1.0,
"_source" : { }
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "519NSnsBL-0_1XxfIztW",
"_score" : 1.0,
"_source" : { }
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "6F9NSnsBL-0_1XxfMjvj",
"_score" : 1.0,
"_source" : { }
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10001",
"_score" : 1.0,
"_source" : {
"title" : "华为V8"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10002",
"_score" : 1.0,
"_source" : {
"title" : "小米手机4"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10003",
"_score" : 1.0,
"_source" : {
"title" : "小米手机4"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10004",
"_score" : 1.0,
"_source" : {
"title" : "小米手机4"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10005",
"_score" : 1.0,
"_source" : {
"title" : "小米手机6"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10008",
"_score" : 1.0,
"_source" : {
"title" : "小米手机8"
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "20008",
"_score" : 1.0,
"_source" : {
"title" : "华为V1"
}
}
]
}
}

输出

  • 查询&排序 & 分页

    "sort": {"price":{"order":"desc"}},"from":2,"size":3
    {"query":{"match_all":{}},"_source":["title","price"],"sort": {"price":{"order":"desc"}},"from":2,"size":3}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title","price"],"sort": {"price":{"order":"desc"}},"from":2,"size":3}'
  • 全文查询,匹配即可,不做字符拆分"match_phrase"
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"小米"}}}'

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}}}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.50209194,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10002",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10003",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10004",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10005",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机6",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2999.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10008",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机8",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 8999.0
}
}
]
}
}

输出

  • 高亮显示字段 "highlight":{"fields":{"字段":{}}}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}},"highlight":{"fields":{"category":{}}}}'

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}},"highlight":{"fields":{"category":{}}}}'
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.50209194,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10002",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
},
"highlight" : {
"category" : [
"小<em>米</em>"
]
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10003",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
},
"highlight" : {
"category" : [
"小<em>米</em>"
]
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10004",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机4",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2993.0
},
"highlight" : {
"category" : [
"小<em>米</em>"
]
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10005",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机6",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 2999.0
},
"highlight" : {
"category" : [
"小<em>米</em>"
]
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "10008",
"_score" : 0.50209194,
"_source" : {
"title" : "小米手机8",
"category" : "小米",
"images" : "http://www.xiaomi.com",
"price" : 8999.0
},
"highlight" : {
"category" : [
"小<em>米</em>"
]
}
}
]
}
}

输出

  • 分组查询
{
"aggs":{ //聚合操作
"price_group":{ //名称随意起名
"terms":{ //分组
"field": "price" //分组字段
}
}
}
}

测试分组查询:

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}}}' -H "Content-Type:application/json"

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}}}' -H "Content-Type:a
{
"took" : 23,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 13,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "4V9BSnsBL-0_1XxfCTs7",
"_score" : 1.0,
"_source" : {
"name" : "zhangmingda",
"age" : 23
}
},
.......
]
},
"aggregations" : {
"price_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 2993.0,
"doc_count" : 4
},
{
"key" : 4999.0,
"doc_count" : 2
},
{
"key" : 8999.0,
"doc_count" : 2
},
{
"key" : 2999.0,
"doc_count" : 1
},
{
"key" : 3999.0,
"doc_count" : 1
}
]
}
}
}

输出

不显示原始数据:加 "size":0

{"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0}

结果

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0}' -H "Content-Type:application/json"
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 13,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 2993.0,
"doc_count" : 4
},
{
"key" : 4999.0,
"doc_count" : 2
},
{
"key" : 8999.0,
"doc_count" : 2
},
{
"key" : 2999.0,
"doc_count" : 1
},
{
"key" : 3999.0,
"doc_count" : 1
}
]
}
}
}

更新数据

  • 全量更新 PUT  /index/_doc/id  -d {'数据'}
curl -X PUT http://10.20.20.214:9200/shopping/_doc/1002  -d '{"name":"zhangmingda", "age":22}' -H "Content-Type:application/json"
  • 局部数据更新 POST /index/_update/id -d {"doc":{数据}}
curl -X POST http://10.20.20.214:9200/shopping/_update/1002  -d '{"doc":{"age":33}}' -H "Content-Type:application/json"

删除数据

  • DELETE /index/_doc/数据id
curl -X DELETE http://10.20.20.214:9200/shopping/_doc/1002
{"_index":"shopping","_type":"_doc","_id":"1002","_version":4,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":9,"_primary_term":1}

ElasticSearch 使用的更多相关文章

  1. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  2. Elasticsearch 5.0 中term 查询和match 查询的认识

    Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...

  3. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  4. Ubuntu 14.04中Elasticsearch集群配置

    Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...

  5. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  6. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  7. .net Elasticsearch 学习入门笔记

    一. es安装相关1.elasticsearch安装  运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...

  8. 自己写的数据交换工具——从Oracle到Elasticsearch

    先说说需求的背景,由于业务数据都在Oracle数据库中,想要对它进行数据的分析会非常非常慢,用传统的数据仓库-->数据集市这种方式,集市层表会非常大,查询的时候如果再做一些group的操作,一个 ...

  9. 如何在Elasticsearch中安装中文分词器(IK+pinyin)

    如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...

  10. jar hell & elasticsearch ik 版本问题

    想给es 安装一个ik 的插件, 我的es 是 2.4.0, 下载了一个版本是 1.9.5, [2016-10-09 16:56:26,248][INFO ][node ] [node-2] init ...

随机推荐

  1. 雇工模式(Employee Pattern)

    本文节选自<设计模式就该这样学> 1 雇工模式的定义 雇工模式(Employee Pattern)也叫作仆人模式(Servant Pattern),属于行为型设计模式,它为一组类提供通用的 ...

  2. Java编程之学习技巧

    **本人博客网站 **IT小神 www.itxiaoshen.com 找到技术点 首先得知道自己要学习技术是什么?不管是来自同事.技术大牛推荐还是通过搜索引擎得到,或者另有出处如.技术交流群.技术论坛 ...

  3. docker版本演变,安装,基本命令

    1.docker 版本信息 Docker CE在17.03版本之前叫Docker Engine,版本号从0.1.0(2013-03-23)~1.13.1(2017-02-08),详见https://d ...

  4. 洛谷 P7879 -「SWTR-07」How to AK NOI?(后缀自动机+线段树维护矩乘)

    洛谷题面传送门 orz 一发出题人(话说我 AC 这道题的时候,出题人好像就坐在我的右侧呢/cy/cy) 考虑一个很 naive 的 DP,\(dp_i\) 表示 \([l,i]\) 之间的字符串是否 ...

  5. Harbour.Space Scholarship Contest 2021-2022 题解

    多好的上分机会啊,要是换个时间(指改在 NOI 之后)我说不定就能上 2500 了(做白日梦 ing) A 签到题不多说,显然只有末尾为 \(9\) 的数是 interesting 的,因此答案就是 ...

  6. pip 与 conda

    pip 与 conda 简介 pip 是接触 python 后最早认识的包管理工具.通过使用 pip 能够自动下载和解决不同 python 模块的依赖问题,使 python 的配置过程变得简单. 与 ...

  7. 数仓day01

    1. 该项目适用哪些行业? 主营业务在线上进行的一些公司,比如外卖公司,各类app(比如:下厨房,头条,安居客,斗鱼,每日优鲜,淘宝网等等) 这类公司通常要针对用户的线上访问行为.消费行为.业务操作行 ...

  8. [web安全] 利用pearcmd.php从LFI到getshell

    有一段时间没写blog了,主要是事多,加上学的有些迷茫,所以内耗比较大.害,沉下心好好学吧. 漏洞利用背景: 允许文件包含,但session等各种文件包含都已经被过滤了.ctf题中可以关注regist ...

  9. C语言把数字转换为字符串的函数

    博主原文 C语言itoa()函数和atoi()函数详解(整数转字符C实现) C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to st ...

  10. STL学习笔记1

    STL六大部件 容器.分配器.算法.迭代器.适配器.仿函数 他们的关系如下