#文档元数据
一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息。 三个必须的元数据元素如下:
## _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删除索引操作:
创建索引
获取索引定义结构:
查询2:
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": 32}} }, "category": { "type": "text", "fields": {"keyword": {"type": "keyword","ignore_above": 32}} }, "content": { "properties": { "content": { "type": "text", "fields": {"keyword": {"type": "keyword"}} }, "sub_content": { "type": "text", "fields": {"keyword": {"type": "keyword","ignore_above": 512}} } } }, "imgs": { "type": "text", "fields": {"keyword": {"type": "keyword","ignore_above": 512}} }, "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": 64}} }, "tag": { "type": "text", "fields": {"keyword": {"type": "keyword","ignore_above": 64}} }, "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": 256}} } } } } }'
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": 20, "up_num": 8, "down_num": 3, "recoment_num":9 }, "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":"1","_version":4,"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": 20, "up_num": 8, "down_num": 3, "recoment_num":9 }, "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": 10, "up_num": 4, "down_num": 3, "recoment_num":5 }, "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": "1", "_version": 4, "found": true, "_source": { "title": "虫儿飞" } }
curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source=title,time.create_time'
返回结果
{ "_index": "blog_article", "_type": "article", "_id": "1", "_version": 4, "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": "1", "_version": 4, "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": 20, "up_num": 8, "down_num": 3, "recoment_num": 9 }, "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": 10, "up_num": 4, "down_num": 3, "recoment_num":5 }, "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": "2", "_version": 4, "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": 10, "up_num": 4, "down_num": 3, "recoment_num": 5 }, "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": "2", "_version": 10, "found": true, "_source": { "num": { "view_num": 11 }, "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": "2", "_version": 10, "found": true, "_source": { "author": "马六", "title": "拔萝卜" } }
9.删除一个文档
curl -XDELETE 'http://192.168.0.120:9200/blog_article/article/1'
返回结果
{ "_index": "blog_article", "_type": "article", "_id": "1", "_version": 3, "result": "deleted", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
再查看id为1的返回数据为:
curl -XGET 'http://192.168.0.120:9200/blog_article/article/1?_source=title,author' 返回 { "_index": "blog_article", "_type": "article", "_id": "1", "found": false }
10.查询所有
curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{ "query": { "match_all": {} } }'
返回:
{ "took": 14, //执行搜索的时间(以毫秒为单位) "timed_out": false, //是否超时 "_shards": { //搜索分片,成功和失败的分片 "total": 5, //总搜索分片 "successful": 5, //成功搜索分片 "skipped": 0, //失败搜索分片 "failed": 0 }, "hits": { "total": 1, //符合我们的搜索条件的文档总数 "max_score": 1, //最高分数 "hits": [ //搜索结果的实际数组(默认为前10个文档) { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 1, //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。 "_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": 11, "up_num": 4, "down_num": 3, "recoment_num": 5 }, "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": 20, "up_num": 8, "down_num": 3, "recoment_num":9 }, "category":"儿歌", "tag":"儿歌,流行歌曲", "author":"张三" }'
查询第一条
curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{ "query": { "match_all": { } }, "size": 1 }'
返回结果
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 1, "_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": 10, "up_num": 4, "down_num": 3, "recoment_num": 5 }, "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": 0, "size": 10 }'
返回结果
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 1, "_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": 10, "up_num": 4, "down_num": 3, "recoment_num": 5 }, "category": "儿歌", "tag": "儿歌,流行歌曲", "author": "李四" } }, { "_index": "blog_article", "_type": "article", "_id": "1", "_score": 1, "_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": 20, "up_num": 8, "down_num": 3, "recoment_num": 9 }, "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": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": null, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "1", "_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": 20, "up_num": 8, "down_num": 3, "recoment_num": 9 }, "category": "儿歌", "tag": "儿歌,流行歌曲", "author": "张三" }, "sort": [ 20 ] }, { "_index": "blog_article", "_type": "article", "_id": "2", "_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": 10, "up_num": 4, "down_num": 3, "recoment_num": 5 }, "category": "儿歌", "tag": "儿歌,流行歌曲", "author": "李四" }, "sort": [ 10 ] } ] } }
14. 返回部分制字段
curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d'{ "query": { "match_all": { } }, "_source": ["title","property","category"] }'
返回结果
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 1, "_source": { "property": "置顶,热点", "title": "拔萝卜", "category": "儿歌" } }, { "_index": "blog_article", "_type": "article", "_id": "1", "_score": 1, "_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": 16, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.8630463, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_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": 32, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.5753642, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 0.5753642, "_source": { "property": "置顶,热点", "title": "拔萝卜", "category": "儿歌" } }, { "_index": "blog_article", "_type": "article", "_id": "1", "_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": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.4384105, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "1", "_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": 25, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.1507283, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "1", "_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": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_score": 1, "_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": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "2", "_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": "19", "lt": "99999" } } } } }, "_source": ["title","property","category","num.view_num"] }'
返回结果
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "blog_article", "_type": "article", "_id": "1", "_score": 1, "_source": { "num": { "view_num": 20 }, "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": 8, "up_num": 8, "down_num": 3, "recoment_num":9 }, "category":"古典歌曲", "tag":"古典歌曲,流行歌曲", "author":"王五" }'
执行统计
curl -XGET 'http://192.168.0.120:9200/blog_article/article/_search' -d '{ "size": 0, "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": 0, "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": 400 }
需要先执行:
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": 0, "aggs": { "group_by_state": { "terms": { "field": "category" } } } }'
返回结果
{ "took": 46, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 0, "hits": [] }, "aggregations": { "group_by_state": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "歌", "doc_count": 3 }, { "key": "儿", "doc_count": 2 }, { "key": "典", "doc_count": 1 }, { "key": "古", "doc_count": 1 }, { "key": "曲", "doc_count": 1 } ] } } }
之所以统计分类出现次数返回上边结果,因为s默认的分词器是英文分词器。我们需要安装中文分词器插件才可以满足我们的需求,下一章我们将会讲解关于中文分词器的安装与用法。
参考:《https://blog.csdn.net/qq_20641565/article/details/77070934》