前言
以movies结果集作为查询数据。
可以参考我的文章 《使用 movielens + logstash 给 elasticsearch导入测试数据》。
语句是在kibana中运行的
URI
即通过url的方式进行数据查询
基本参数
movies 代表索引
_search 代表对索引进行查询
q 查询条件 title:Beautiful title中包含 Beautiful
from 数据偏移量 等价mysql offset
size 每页显示条数 等价mysql limit
sort 排序 year:desc 安装year desc 排
profile true 显示查询过程
GET /movies/_search?q=title:Beautiful&from=0&size=10&sort=year:desc
{
"profile": "true"
}
如果要求的是任意一个字段包含Beautiful就可以,那么去掉title就像了,默认泛查询
GET /movies/_search?q=Beautiful&from=0&size=10&sort=year:desc
同一个查询条件在不同情况下的查询范围
查询条件为 Beautiful Mind
a.没有引号和()的情况下,查询的结果集由title包含Beautiful的结果集 + 某个字段包含Mind的结果集构成。也就是说对于Mind是泛查
GET /movies/_search?q=title:Beautiful Mind
{
"profile": "true"
}
b.在有引号的情况下,返回的结果集title必须包含 Beautiful Mind
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile": "true"
}
c.在有()的情况下,返回的结果集由 title包含Beautiful的结果集 + title包含Mind的结果集构成
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile": "true"
}
提供了一些操作符
AND
要求必须包含Beautiful Mind
GET /movies/_search?q=title:(Beautiful AND Mind)
{
"profile": "true"
}
OR
包含Beautiful 或者 包含 Mind
GET /movies/_search?q=title:(Beautiful OR Mind)
{
"profile": "true"
}
+
必须有的意思,在url中 %2B 代表+号
结果集由a,b两部分组成
a:title包含Mind的部分
b:title包含:Beautiful Mind 的部分(要求存在Beautiful 则必须有Mind)
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile": "true"
}
-
必须没有的意思
包含Beautiful 不包含 Mind
GET /movies/_search?q=title:(Beautiful -Mind)
{
"profile": "true"
}
找电影,除了2018年的电影
GET /movies/_search?q=year:(-2018)
{
"profile": "true"
}
范围查询
这里以>为例,还有(>=,<,<=),不等于用-
查找2018年以后的电影
GET /movies/_search?q=year:>2018
正则查找
查找包含b开头词的电影
GET /movies/_search?q=title:b*
{
"profile": "true"
}
~
类似于mysql的模糊查询,但又不同
Bautiful~1有两种意思:
a.Bautiful 中可能在某个位置缺少一个字母,需补充(可以补充成为 Beautiful)
b.Bautiful 中可能在某个位置字母错了,需替换(可以替换成为 Biutiful)
返回的结果集包含补充过,替换过的内容
GET /movies/_search?q=title:Bautiful~1
{
"profile": "true"
}
加上双引号修饰后变成Lord Rings 两个单词之间可以缺少最多两个词,可以匹配 Lord of the Rings
GET /movies/_search?q=title:"Lord Rings"~2
{
"profile": "true"
}