文章目录
0、准备工作
0.1、数据准备
给出user索引下people文档的数据,方便比对查询结果
0.2、profile搜索细节查看
当对搜索结果存在疑虑时,可以使用"profile"参数去查看搜索细节
举例使用:
## 查看字段数据等于20的数据 并查看搜索细节
GET /house/_search?q=20
{
"profile": "true"
}
1、URI查询语法
- q 指定查询语句,使用Query String Syntax
- df(default file)默认字段,不指定时会基于所有字段进行查询
- sort 排序
- from和size 用于分页
- profile 可以查看查询是如何被执行的
1.1、指定字段查询
如q=age:20 表示查询age=20的数据
## 查询people文档下 age=20的数据
GET /user/people/_search?q=age:20
## 响应
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "people",
"_id" : "hGk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "张三",
"age" : 20,
"mail" : "111@qq.com",
"hobby" : "羽毛球、乒乓球、足球"
}
}
]
}
}
1.2、泛查询
如q=20 表示查询字段值包含21的数据
## 查询people文档下 字段值包含21的数据
GET /user/people/_search?q=21
##响应
{
"took" : 146,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "people",
"_id" : "hWk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"age" : 21,
"mail" : "222@qq.com",
"hobby" : "羽毛球、乒乓球、足球、篮球"
}
}
]
}
}
1.3、Phase(短语)查询
先举例说明非短语查询是怎么查询的?如查询q=name:ke ting
实际上是查询name中包含ke 和 文档中所有字段中包含ting的数据(可通过profile查看细节)
如果只是想查询包含"ke ting"这个短语,可以尝试短语查询
短语"ke ting"等价于ke AND ting,并且短语查询要求短语的前后顺序保持一致;
也可以使用()
分组,如"ke ting" 也等价于(ke AND ting)
举例短语查询
## 下面两句等价
GET /house/room/_search?q=name:"ke ting" ①
GET /house/room/_search?q=name:(ke AND ting) ②
## 响应
{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "house",
"_type" : "room",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"name" : "ke ting",
"area" : "20平米"
}
}
]
}
}
👆上面语句①的查询是查找name="ke ting"的数据,语句②的查询是查找name包含ke 和 name包含ting的数据
1.4、布尔操作
AND、OR、NOT(字符必须大写)或者&&、||、!
举例 title:(Beautiful AND Mind) 表示title中包含Beautiful 也包含Mind的数据
举例 title:(Beautiful NOT Mind) 表示title中包含Beautiful 不包含Mind的数据
## 可以查看查询如何执行
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile": "true"
}
1.5、分组
- +表示must
- - 表示must not
举例 title:(Beautiful -Mind)表示title中包含Beautiful 不包含Mind的数据,相当于NOT
举例title:(Beautiful +Mind)表示title中包含了Beautiful 或者Mind的数据,相当于OR
1.6、范围查询
- 区间表示 []表示闭区间 {}表示开区间
举例查询age在[20,24]之间的数据
GET /user/_search/?q=age:[20 TO 24]
## 响应
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "people",
"_id" : "hGk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "张三",
"age" : 20,
"mail" : "111@qq.com",
"hobby" : "羽毛球、乒乓球、足球"
}
},
{
"_index" : "user",
"_type" : "people",
"_id" : "hWk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"age" : 21,
"mail" : "222@qq.com",
"hobby" : "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index" : "user",
"_type" : "people",
"_id" : "iGk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "孙七",
"age" : 24,
"mail" : "555@qq.com",
"hobby" : "听音乐、看电影、羽毛球"
}
},
{
"_index" : "user",
"_type" : "people",
"_id" : "hmk5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "王五",
"age" : 22,
"mail" : "333@qq.com",
"hobby" : "羽毛球、篮球、游泳、听音乐"
}
},
{
"_index" : "user",
"_type" : "people",
"_id" : "h2k5jHQBzgrkYgRNOrUU",
"_score" : 1.0,
"_source" : {
"name" : "赵六",
"age" : 23,
"mail" : "444@qq.com",
"hobby" : "跑步、游泳、篮球"
}
}
]
}
}
举例查询age在{20,24}之间的数据
GET /user/_search?q=age:{20 TO 24}
## 响应
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "people",
"_id": "hWk5jHQBzgrkYgRNOrUU",
"_score": 1,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index": "user",
"_type": "people",
"_id": "hmk5jHQBzgrkYgRNOrUU",
"_score": 1,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
},
{
"_index": "user",
"_type": "people",
"_id": "h2k5jHQBzgrkYgRNOrUU",
"_score": 1,
"_source": {
"name": "赵六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳、篮球"
}
}
]
}
}
1.7、算数符号
- age:(>21 AND <=24) 相当于 age:{21 TO 24]
- age:(>=21 AND <=24) 相当于 age:[21 TO 24]
- age:>=21 相当于 age:[21 TO *]
1.8、通配符查询
通配符查询效率低,占用内存大 不建议使用
? 代表1个字符 *代表0或者多个字符
- 举例 title:mi?d
- 举例 title:be*
- 正则表达式 title:[bt]oy 表示匹配boy或toy
- 模糊查询 title:ke~1
- 近似查询 title:“ke”~2
举例:
GET /house/_search?q=name:"ke"~1
## 响应
{
"took" : 32,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "house",
"_type" : "room",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "ke ting",
"area" : "20平米"
}
}
]
}
}