#文档元数据

一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息。 三个必须的元数据元素如下:
## _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. 在windows下安装配置python开发环境及Ulipad开发工具(转)

    最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...

  2. 使用 IntraWeb (8) - 系统模板

    我们可以自定义系统错误模板, 编辑 IWError.html 放到模板文件夹后, 它将替换默认的模板. {在主页面, 这是要模拟一个系统错误} procedure TIWForm1.IWButton1 ...

  3. USBDM Debugger interface for Freescale RS08,HCS08,HCS12,Coldfire and ARM-Kinetis Devices.

    Introduction USBDM is a debugger hardware interface for a range of Freescale microcontrollers. It is ...

  4. delphi Image 处理

    procedure ResizeBmp(Src,Dst:String);var SrcBM,DstBM:TBitMap; Rect:TRect; NewW,NewH,PicW,PicH:Integer ...

  5. AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器

    本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...

  6. Subversion detected an unsupported working copy version

    关于这个错误:Subversion detected an unsupported working copy version while checking the status of 'XXXX'. ...

  7. mariadb设置初始密码

    mariadb设置初始密码 CENTOS7 自带MARIADB数据库.安装的时候可以勾选安装. 当然也可以以后在CENTOS7里面添加安装. MARIADB安装后,默认是没有密码的. 我们需要给ROO ...

  8. iOS中使用RegexKitLite来试用正则表达式

    转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中 ...

  9. java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

    java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.w ...

  10. Windows:chm 文件打开出现“已取消到该网页的导航”的解决方案

    症状 解决方案