目录
一、低级查询
2.条件查询
3.简单查询
4.聚合查询
一、高级查询
2.复合条件查询
前言
查询的链接操作是
127.0.0.1:9200/book/_search
动作指令:POST
一、简单查询
所有的查询都是以query为关键词。
1.查询全部文档
{
"query":{
"match_all":{}//查询所有内容
},
"from":1,//表示从哪里返回
"size":1//表示返回几条数据
}
查询结果
{
"took": 5,//表示接口响应花费了四毫秒
"timed_out": false,
"_shards": {
"total": 5,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {//代表响应的全部结果,hits默认返回的是10条数据
"total": 8,//总共有十一条
"max_score": 1.0,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1.0,
"_source": {
"author": "王五",
"title": "菜谱",
"word_count": "5000",
"publish_date": "2012-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.0,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1.0,
"_source": {
"author": "很胖的瓦力",
"title": "Elasticsearch精通",
"word_count": "3000",
"publish_date": "2017-08-15"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "10",
"_score": 1.0,
"_source": {
"author": "牛魔王",
"title": "芭蕉扇",
"word_count": "1000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1.0,
"_source": {
"author": "张三",
"title": "移魂大法",
"word_count": "10000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 1.0,
"_source": {
"author": "张三丰",
"title": "太极拳",
"word_count": "1000",
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "3",
"_score": 1.0,
"_source": {
"author": "张四",
"title": "python入门",
"word_count": "2000",
"publish_date": "2005-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 1.0,
"_source": {
"author": "孙悟空",
"title": "七十二变",
"word_count": "1000",
"publish_date": "2000-10-01"
}
}
]
}
}
2.关键词查询
{
"query":{
"match":{
"title":"ElasticSearch"//指定查询title里面含有后面内容的文档
}
},
"sort":[//sort关键字用于重定义排序规则
{"publish_date":{"order":"desc"}}//例子是以日子作为标准默认是升序,DESC变成降序
]
}
返回结果
返回的结果默认是以score倒排
想要自定义排序条件:加sort关键字
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6682933,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.6682933,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 0.6682933,
"_source": {
"author": "很胖的瓦力",
"title": "Elasticsearch精通",
"word_count": "3000",
"publish_date": "2017-08-15"
}
}
]
}
}
二、聚合查询
aggs是聚合查询的关键字。
1.单聚合以单词数量统计查询
{
"aggs":{//聚合查询的关键字
"group_by_word_count":{//自定义查询的名字例子是以单词的数量作为聚合查询
"terms":{//关键字
"field":"word_count"//字段
}
}
}
}
返回的结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 1.0,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1.0,
"_source": {
"author": "王五",
"title": "菜谱",
"word_count": "5000",
"publish_date": "2012-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.0,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1.0,
"_source": {
"author": "很胖的瓦力",
"title": "Elasticsearch精通",
"word_count": "3000",
"publish_date": "2017-08-15"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "10",
"_score": 1.0,
"_source": {
"author": "牛魔王",
"title": "芭蕉扇",
"word_count": "1000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1.0,
"_source": {
"author": "张三",
"title": "移魂大法",
"word_count": "10000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 1.0,
"_source": {
"author": "张三丰",
"title": "太极拳",
"word_count": "1000",
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "3",
"_score": 1.0,
"_source": {
"author": "张四",
"title": "python入门",
"word_count": "2000",
"publish_date": "2005-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 1.0,
"_source": {
"author": "孙悟空",
"title": "七十二变",
"word_count": "1000",
"publish_date": "2000-10-01"
}
}
]
},
"aggregations": {
"group_by_word_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1000,//聚合信息
"doc_count": 3//此条件下有多少个
},
{
"key": 3000,
"doc_count": 2
},
{
"key": 2000,
"doc_count": 1
},
{
"key": 5000,
"doc_count": 1
},
{
"key": 10000,
"doc_count": 1
}
]
}
}
}
2.多聚合以日期,单词数量进行查询
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
},
"group+by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
返回结果
"aggregations": {
"group+by_publish_date": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 970358400000,
"key_as_string": "2000-10-01 00:00:00",
"doc_count": 3
},
{
"key": 852076800000,
"key_as_string": "1997-01-01 00:00:00",
"doc_count": 1
},
{
"key": 1128124800000,
"key_as_string": "2005-10-01 00:00:00",
"doc_count": 1
},
{
"key": 1349049600000,
"key_as_string": "2012-10-01 00:00:00",
"doc_count": 1
},
{
"key": 1502755200000,
"key_as_string": "2017-08-15 00:00:00",
"doc_count": 1
},
{
"key": 1503187200000,
"key_as_string": "2017-08-20 00:00:00",
"doc_count": 1
}
]
},
"group_by_word_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1000,
"doc_count": 3
},
{
"key": 3000,
"doc_count": 2
},
{
"key": 2000,
"doc_count": 1
},
{
"key": 5000,
"doc_count": 1
},
{
"key": 10000,
"doc_count": 1
}
]
}
}
3.聚合查询函数
{
"aggs":{
"grades_word_count":{
"stats":{//函数进行计算
"field":"word_count"
}
}
}
}
返回结果
"aggregations": {
"grades_word_count": {
"count": 8,
"min": 1000.0,
"max": 10000.0,
"avg": 3250.0,
"sum": 26000.0
}
}
4.计算统计某一个字段的各类值
{
"aggs":{
"grades_word_count":{
"stats":{
"field":"word_count"
}
}
}
}
返回结果
"aggregations": {
"grades_word_count": {
"count": 8,
"min": 1000.0,
"max": 10000.0,
"avg": 3250.0,
"sum": 26000.0
}
}
二、高级查询
第一类:特定字段查询指定的特定值
QUERY_context: 在查询过程中,除了判断文档是否满足查询条件以外,ES还会计算一个_score来标识匹配的程度,意在判断目标文档和查询条件匹配的有多好。
- 全文本查询 针对文本类型的数据
- 字段级别的查询 针对结构化数据、如数字、日期等
1.模糊匹配作者的书籍
{
"query":{
"match":{
"author":"瓦力"
}
}
}
返回结果
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.2039728,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
}
]
2.习语匹配
{
"query":{
"match_phrase":{
"author":"瓦力"
}
}
}
3.多个字段模糊匹配查询
{
"query":{
"multi_match":{
"query":"Elasticsearch",
"fields":["author","title"]
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.6682933,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 0.6682933,
"_source": {
"author": "很胖的瓦力",
"title": "Elasticsearch精通",
"word_count": "3000",
"publish_date": "2017-08-15"
}
}
三、语法查询
1.AND OR查询
{
"query":{
"query_string":{
"query":"Elasticsearch AND 瓦力"
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.872266,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
}
可以在上述查询里用括号加上or来并列查询
2.指定在某些字段内包含关键词语的查询
{
"query":{
"query_string":{
"query":"Elasticsearch AND 瓦力",
"fields":["title","author"]
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.872266,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
}
3.结构化的查询
具体项目关键词:term
举例 :
{
"query":{
"term":{
"author":"瓦力"
}
}
}
返回值
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1.2039728,
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
}
4.范围查询
{
"query":{
"range":{//也可以支持日期查询
"word_count":{
"gte":1000,//e表示的是euqals
"lte":2000
}
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "10",
"_score": 1.0,
"_source": {
"author": "牛魔王",
"title": "芭蕉扇",
"word_count": "1000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 1.0,
"_source": {
"author": "张三丰",
"title": "太极拳",
"word_count": "1000",
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "3",
"_score": 1.0,
"_source": {
"author": "张四",
"title": "python入门",
"word_count": "2000",
"publish_date": "2005-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 1.0,
"_source": {
"author": "孙悟空",
"title": "七十二变",
"word_count": "1000",
"publish_date": "2000-10-01"
}
}
四、Filter Context子条件查询
再查询过程中,只判断文档是否满足条件。与bool联合使用
举例:
只查询word_count为1000 的信息
{
"query":{
"bool":{
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "10",
"_score": 0.0,
"_source": {
"author": "牛魔王",
"title": "芭蕉扇",
"word_count": "1000",
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 0.0,
"_source": {
"author": "张三丰",
"title": "太极拳",
"word_count": "1000",
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 0.0,
"_source": {
"author": "孙悟空",
"title": "七十二变",
"word_count": "1000",
"publish_date": "2000-10-01"
}
filter相对query速度快一些,因为ES会对filter的结果进行缓存
五、复合条件查询
1.固定分数查询
{
"query":{
"constant_score":{
"filter":{
"match":{
"title":"菜谱"
}
},
"boost":2
}
}
}
返回结果
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1.0,
"_source": {
"author": "王五",
"title": "菜谱",
"word_count": "5000",
"publish_date": "2012-10-01"
}
}
2.bool匹配字段
{
"query":{
"bool":{
"should":[
{
"match":{
"author":"瓦力"
}
},
{
"match":{
"title":"Elasticsearch"
}
}
]
}
}
}
返回结果
"_source": {
"author": "瓦力",
"title": "Elasticsearch入门",
"word_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 0.6682933,
"_source": {
"author": "很胖的瓦力",
"title": "Elasticsearch精通",
"word_count": "3000",
"publish_date": "2017-08-15"
}
3.should或的关系和must与的关系
{
"query":{
"bool":{
"must":[
{
"match":{
"author":"瓦力"
}
},
{
"match":{
"title":"太极拳"
}
}
]
}
}
}
//must_not不能满足的条件