#文档元数据

一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息。 三个必须的元数据元素如下:
## _index
    文档在哪存放
## _type
    文档表示的对象类别
## _id
   文档唯一标识
# 索引文档

#通过使用

index API ,文档可以被 索引 —— 存储和使文档可被搜索 。但是首先,我们要确定文档的位置。正如我们刚刚讨论的,一个文档的 _index 、 _type 和 _id 唯一标识一个文档。 我们可以提供自定义的 _id 值,或者让 index API 自动生成。

PUT /{index}/{type}/{id}
{
"field": "value",
...
}

1.删除、创建、修改、查看索引

删除索引

curl -XDELETE "http://192.168.0.120:9200/blog_article"
通过ES-Head删除索引操作:
创建索引

curl -XPUT "http://192.168.0.120:9200/blog_article" -H 'Content-Type: application/json' -d'
{
"mappings": {
"article": {
"properties": {
"author": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
},
"category": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
},
"content": {
"properties": {
"content": {
"type": "text",
"fields": {"keyword": {"type": "keyword"}}
},
"sub_content": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
}
}
},
"imgs": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
},
"num": {
"properties": {
"click_num": {
"type": "integer",
"fields": {"keyword": {"type": "keyword"}}
},
"down_num": {
"type": "integer",
"fields": {"keyword": {"type": "keyword"}}
},
"recoment_num": {
"type": "integer",
"fields": {"keyword": {"type": "keyword"}
}
},
"up_num": {
"type": "integer",
"fields": {"keyword": {"type": "keyword"}}
},
"view_num": {
"type": "integer",
"fields": {"keyword": {"type": "keyword"}}
}
}
},
"property": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
},
"tag": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
},
"time": {
"properties": {
"create_time": {"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},
"update_time": {"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
}
},
"title": {
"type": "text",
"fields": {"keyword": {"type": "keyword","ignore_above": }}
}
}
}
}
}'

获取索引定义结构:

curl -XGET "http://192.168.0.120:9200/blog_article/_mapping?pretty"

2.添加一篇文章(ip: port/库/表/id)

curl -XPUT "http://192.168.0.120:9200/blog_article/article/1" -H 'Content-Type: application/json' -d'
{
"title": "虫儿飞",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"冷风吹,冷风吹,你在思念谁。",
"content":"黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"儿歌",
"tag":"儿歌,流行歌曲",
"author":"张三"
}'

3.通过浏览器用id查询article成绩

http://192.168.0.120:9200/blog_article/article/1

4.在linux中通过curl的方式用id查询article的点击数

curl -XGET 'http://192.168.0.120:9200/blog_article/article/1'

操作日志:

[spark@master ~]$ curl -XGET 'http://192.168.0.120:9200/blog_article/article/1'
{"_index":"blog_article","_type":"article","_id":"","_version":,"found":true,"_source":{
"title": "虫儿飞",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"冷风吹,冷风吹,你在思念谁。",
"content":"黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"儿歌",
"tag":"儿歌,流行歌曲",
"author":"张三"
}
}
[spark@master ~]$

5.添加另外一个article

curl -XPUT "http://192.168.0.120:9200/blog_article/article/2" -H 'Content-Type: application/json' -d'
{
"title": "拔萝卜",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content":"拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"儿歌",
"tag":"儿歌,流行歌曲",
"author":"李四"
}'

6.通过_source获取指定的字段

查询1:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source=title'

返回结果

{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"title": "虫儿飞"
}
}

查询2:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source=title,time.create_time'

返回结果

{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"time": {
"create_time": "2017-08-10"
},
"title": "虫儿飞"
}
}

查询3:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source'
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"title": "虫儿飞",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "冷风吹,冷风吹,你在思念谁。",
"content": "黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "张三"
}
}

7.可以通过覆盖的方式更新

curl -XPUT "http://192.168.0.120:9200/blog_article/article/2" -H 'Content-Type: application/json' -d'
{
"title": "拔萝卜",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content":"拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-07-22"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"儿歌",
"tag":"儿歌,流行歌曲",
"author":"李四"
}'

再查看article=2的文章的最小修改时间(time.update_time)就变成"2018-07-22"了.

curl -XGET "http://192.168.0.120:9200/blog_article/article/2?_source"

返回结果:

{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"title": "拔萝卜",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-07-22"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "李四"
}
}

8.通过_updateAPI的方式单独更新你想要更新的

curl -XPOST "http://192.168.0.120:9200/blog_article/article/2/_update" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.num.view_num+=1"
}'

修改后,查看修改状态:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/2?_source=title,num.view_num'
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"num": {
"view_num":
},
"title": "拔萝卜"
}
}

更新作者字段:

curl -XPOST 'http://192.168.0.120:9200/blog_article/article/2/_update' -d '{
"doc": {
"author": "马六"
}
}'

修改后,查看修改状态:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/2?_source=title,author'
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"found": true,
"_source": {
"author": "马六",
"title": "拔萝卜"
}
}

9.删除一个文档

curl -XDELETE 'http://192.168.0.120:9200/blog_article/article/1'

返回结果

{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_version": ,
"result": "deleted",
"_shards": {
"total": ,
"successful": ,
"failed":
},
"_seq_no": ,
"_primary_term":
}

再查看id为1的返回数据为:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source=title,author'
返回
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"found": false
}

10.查询所有

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": { "match_all": {} }
}'

返回:

{
"took": , //执行搜索的时间(以毫秒为单位)
"timed_out": false, //是否超时
"_shards": { //搜索分片,成功和失败的分片
"total": , //总搜索分片
"successful": , //成功搜索分片
"skipped": , //失败搜索分片
"failed":
},
"hits": {
"total": , //符合我们的搜索条件的文档总数
"max_score": , //最高分数
"hits": [ //搜索结果的实际数组(默认为前10个文档)
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": , //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
"_source": {
"title": "拔萝卜",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-07-22"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "马六"
}
}
]
}
}

11.查询第一条

此时重新添加id=1的文档:

curl -XPUT "http://192.168.0.120:9200/blog_article/article/1" -H 'Content-Type: application/json' -d'
{
"title": "虫儿飞",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"冷风吹,冷风吹,你在思念谁。",
"content":"黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"儿歌",
"tag":"儿歌,流行歌曲",
"author":"张三"
}'

查询第一条

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"match_all": { }
},
"size":
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"title": "拔萝卜",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "李四"
}
}
]
}
}

12.分页查询 from表示从几条开始查询, size表示查询的条数 和limit一样,其中第一条是0条

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"match_all": { }
},
"from": ,
"size":
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"title": "拔萝卜",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "李四"
}
},
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"title": "虫儿飞",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "冷风吹,冷风吹,你在思念谁。",
"content": "黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "张三"
}
}
]
}
}

13.排序

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"match_all": { }
},
"sort": {
"num.view_num": "desc"
}
}'

返回:

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": null,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": null,
"_source": {
"title": "虫儿飞",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "冷风吹,冷风吹,你在思念谁。",
"content": "黑黑的天空低垂<br>亮亮的繁星相随<br>虫儿飞虫儿飞<br>你在思念谁<br><br>天上的星星流泪<br>地上的玫瑰枯萎<br>冷风吹冷风吹<br>只要有你陪<br><br>虫儿飞花儿睡<br>一双又一对才美<br>不怕天黑只怕心碎<br>不管累不累<br>也不管东南西北</pre>"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "张三"
},
"sort": [ ]
},
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": null,
"_source": {
"title": "拔萝卜",
"property": "置顶,热点",
"imgs": "",
"content": {
"sub_content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动。",
"content": "拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>小姑娘快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小黄狗快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动<br>小花猫快快来<br>快来帮我们拔萝卜<br>拔萝卜 拔萝卜<br>嗨吆嗨吆拔萝卜<br>嗨吆嗨吆拔不动,<br>老太婆快快来<br>快来帮我们拔萝卜"
},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-09-12"
},
"num": {
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category": "儿歌",
"tag": "儿歌,流行歌曲",
"author": "李四"
},
"sort": [ ]
}
]
}
}

14. 返回部分制字段

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d'{
"query": {
"match_all": { }
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"property": "置顶,热点",
"title": "拔萝卜",
"category": "儿歌"
}
},
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"property": "置顶,热点",
"title": "虫儿飞",
"category": "儿歌"
}
}
]
}
}

15.匹配查询

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"match": {
"title": "拔萝卜"
}
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.8630463,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 0.8630463,
"_source": {
"property": "置顶,热点",
"title": "拔萝卜",
"category": "儿歌"
}
}
]
}
}

16.同时包含查询match_phrase

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": { "match_phrase": { "category": "儿歌" } },
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.5753642,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 0.5753642,
"_source": {
"property": "置顶,热点",
"title": "拔萝卜",
"category": "儿歌"
}
},
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 0.5753642,
"_source": {
"property": "置顶,热点",
"title": "虫儿飞",
"category": "儿歌"
}
}
]
}
}

17.bool查询must 需要全部满足

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"bool": {
"must": [
{ "match": { "category": "儿歌" } },
{ "match": { "title": "虫儿飞" } }
]
}
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 1.4384105,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 1.4384105,
"_source": {
"property": "置顶,热点",
"title": "虫儿飞",
"category": "儿歌"
}
}
]
}
}

18.bool查询should 只要满足一个就行

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"bool": {
"must": [
{ "match": { "category": "流行歌曲" } },
{ "match": { "title": "虫儿飞" } }
]
}
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 1.1507283,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 1.1507283,
"_source": {
"property": "置顶,热点",
"title": "虫儿飞",
"category": "儿歌"
}
}
]
}
}

19.bool查询must_not 全部都不满足

curl -XGET "http://192.168.0.120:9200/blog_article/article/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "category": "流行" } },
{ "match": { "title": "虫儿飞" } }
]
}
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"property": "置顶,热点",
"title": "拔萝卜",
"category": "儿歌"
}
}
]
}
}

20.bool查询must, should, must_not

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"bool": {
"must": [
{ "match": { "category": "儿歌" } }
],
"must_not": [
{ "match": { "title": "虫儿飞" } }
]
}
},
"_source": ["title","property","category"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.5753642,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": 0.5753642,
"_source": {
"property": "置顶,热点",
"title": "拔萝卜",
"category": "儿歌"
}
}
]
}
}

21.过滤查询(它不会去计算分值,因此效率也就更高一些) 这里score是string类型 从左到右做比较 非数字大小

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"num.view_num": {
"gt": "",
"lt": ""
}
}
}
}
},
"_source": ["title","property","category","num.view_num"]
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "blog_article",
"_type": "article",
"_id": "",
"_score": ,
"_source": {
"num": {
"view_num":
},
"property": "置顶,热点",
"title": "虫儿飞",
"category": "儿歌"
}
}
]
}
}

22.聚合查询

新增一个id=3的文档

curl -XPUT "http://192.168.0.120:9200/blog_article/article/3" -H 'Content-Type: application/json' -d'
{
"title": "青花瓷",
"property": "置顶,热点",
"imgs":"",
"content":{
"sub_content":"素胚勾勒出青花笔锋浓转淡<br>瓶身描绘的牡丹一如你初妆<br>",
"content":"素胚勾勒出青花笔锋浓转淡<br>瓶身描绘的牡丹一如你初妆<br>冉冉檀香透过窗心事我了然<br>宣纸上走笔至此搁一半<br>釉色渲染仕女图韵味被私藏<br>而你嫣然的一笑如含苞待放<br>你的美一缕飘散<br>去到我去不了的地方<br>天青色等烟雨 而我在等你<br>炊烟袅袅升起 隔江千万里<br>在瓶底书刻隶仿前朝的飘逸<br>就当我为遇见你伏笔<br>天青色等烟雨 而我在等你<br>月色被打捞起 晕开了结局<br>如传世的青花瓷自顾自美丽<br>你眼带笑意<br>色白花青的锦鲤跃然于碗底<br>临摹宋体落款时却惦记着你<br>你隐藏在窑烧里千年的秘密<br>极细腻犹如绣花针落地<br>帘外芭蕉惹骤雨门环惹铜绿<br>而我路过那江南小镇惹了你<br>在泼墨山水画里<br>你从墨色深处被隐去<br>天青色等烟雨 而我在等你<br>炊烟袅袅升起 隔江千万里<br>在瓶底书刻隶仿前朝的飘逸<br>就当我为遇见你伏笔<br>天青色等烟雨 而我在等你<br>月色被打捞起 晕开了结局<br>如传世的青花瓷自顾自美丽 你眼带笑意<br>天青色等烟雨 而我在等你<br>炊烟袅袅升起 隔江千万里<br>在瓶底书刻隶仿前朝的飘逸<br>就当我为遇见你伏笔<br>天青色等烟雨 而我在等你<br>月色被打捞起 云开了结局<br>如传世的青花瓷自顾自美丽 你眼带笑意<br>"},
"time": {
"create_time": "2017-08-10",
"update_time": "2018-07-22"
},
"num":{
"view_num": ,
"up_num": ,
"down_num": ,
"recoment_num":
},
"category":"古典歌曲",
"tag":"古典歌曲,流行歌曲",
"author":"王五"
}'

执行统计

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"size": ,
"aggs": {
"group_by_state": {
"terms": {
"field": "category"
}
}
}
}'

语句抛出异常:

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [category] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": ,
"index": "blog_article",
"node": "EER2PWclQbOJWslGargNyg",
"reason": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [category] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}
}
]
},
"status":
}

需要先执行:

curl -XPUT 'http://192.168.0.120:9200/blog_article/_mapping/article/' -d '{
"properties": {
"category": {
"type": "text",
"fielddata": true
}
}
}'
#返回
{
"acknowledged": true
}

再执行统计:

curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{
"size": ,
"aggs": {
"group_by_state": {
"terms": {
"field": "category"
}
}
}
}'

返回结果

{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": []
},
"aggregations": {
"group_by_state": {
"doc_count_error_upper_bound": ,
"sum_other_doc_count": ,
"buckets": [
{
"key": "歌",
"doc_count":
},
{
"key": "儿",
"doc_count":
},
{
"key": "典",
"doc_count":
},
{
"key": "古",
"doc_count":
},
{
"key": "曲",
"doc_count":
}
]
}
}
}

之所以统计分类出现次数返回上边结果,因为s默认的分词器是英文分词器。我们需要安装中文分词器插件才可以满足我们的需求,下一章我们将会讲解关于中文分词器的安装与用法。

参考:《https://blog.csdn.net/qq_20641565/article/details/77070934》

Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十八)ES6.2.2 增删改查基本操作的更多相关文章

  1. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(八)安装zookeeper-3.4.12

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  2. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十一)NIFI1.7.1安装

    一.nifi基本配置 1. 修改各节点主机名,修改/etc/hosts文件内容. 192.168.0.120 master 192.168.0.121 slave1 192.168.0.122 sla ...

  3. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十)安装hadoop2.9.0搭建HA

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  4. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(九)安装kafka_2.11-1.1.0

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  5. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十三)kafka+spark streaming打包好的程序提交时提示虚拟内存不足(Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical memory used; 2.2 GB of 2.1 G)

    异常问题:Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical mem ...

  6. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十二)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网。

    Centos7出现异常:Failed to start LSB: Bring up/down networking. 按照<Kafka:ZK+Kafka+Spark Streaming集群环境搭 ...

  7. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十一)定制一个arvo格式文件发送到kafka的topic,通过Structured Streaming读取kafka的数据

    将arvo格式数据发送到kafka的topic 第一步:定制avro schema: { "type": "record", "name": ...

  8. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(三)安装spark2.2.1

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  9. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二)安装hadoop2.9.0

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  10. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十)ES6.2.2 Client API

    scala版本2.11 java版本1.8 spark版本2.2.1 es版本6.2.2 hadoop版本2.9.0 elasticsearch节点列表: 192.168.0.120 192.168. ...

随机推荐

  1. 深入理解DOM节点类型第一篇——12种DOM节点类型概述

    × 目录 [1]元素 [2]特性 [3]文本[4]CDATA[5]实体引用[6]实体名称[7]处理指令[8]注释[9]文档[10]文档类型[11]文档片段[12]DTD 前面的话 DOM是javasc ...

  2. dr.wondr博士随笔之三星某智能机的SGHXXXX 的取证恢复一例

    大家好!欢迎今天再次来到我dr.wonde的博客, 今天我给大家带来一款三星镜面古董机SGH-E848的取证展示! 三星SGH-E848是一款非常漂亮的镜面手机,2008年出厂.. 上面黄色是97号数 ...

  3. [BCB] C++ Builder 绘图 绘制直线 —— 基于像素

    大三的这前半个学期了,又遇上了这个精通(滑稽) Delphi的老师,人还挺好的其实. 关于他对分辨率的吐槽呀,对Delphi的赞美啊,都是非常幽默的.另外我倒是很欣赏他对他的笔记本的保养[dell i ...

  4. Java 判断图片资源的存在否

    question: 如题,举个例子吧 String image ="http://info-database.csdn.net/Upload/2010-10-30/735-60sap1030 ...

  5. java自动转型

    /*2015-10-30*/ public class TypeAutoConvert { public static void main(String[] args) { int a = 5; Sy ...

  6. or1200于IMMU分析

    以下摘录<步骤吓得核心--软-core处理器的室内设计与分析>一本书 1 IMMU结构 OR1200中实现IMMU的文件有or1200_immu_top.v.or1200_immu_tlb ...

  7. Struts2学习笔记(三)——Action详解

    Action是用于处理请求操作的,它是由StrutsPrepareAndExceuteFilter分发过来的. 1.Action的创建方式 1) POJO类(PlainOldJavaObjects简单 ...

  8. 初识 systemd

    从 init 系统说起 linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化.内核初始化的最后一步就是启动 PID ...

  9. 阿里java编码规范考试总结

    前几天,考了阿里编码规范刚好80险过,总结出以下例题,答案有错,欢迎评论 1.关于方法的返回值是否可以为null,下列说法正确的是(BCD) A.方法的返回值可以为null,如果是集合,必须返回空集合 ...

  10. Python IDLE配置清屏快捷键(Ctrl+L)

    1.在Python\Lib\idlelib下,新建一个ClearWindow.py文件(没有时就新建),内容如下: """ Clear Window Extension ...