URL Search
Url Query 常用参数:
1.简单查询
- q 指定查询语句,语法为 Query String Syntax
- df q 中不指定字段时默认查询的字段,如果不指定,ES 会查询所有字段
- sort 指定排序字段
- timeout 指定超时时间,默认不超时
- from, size 用于分页
example:
PUT test_search_index
{
"settings": {
"index": {
"number_of_shards": "1"
}
}
}
PUT test_search_index/doc/_bulk
{"index":{"_id":"1"}}
{"username":"alfred way","job":"java engineer","age":18,"birth":"1990-01-02","isMarried":false}
{"index":{"_id":"2"}}
{"username":"alfred","job":"java senior engineer and java specialist", "age":28,"birth":"1980-05-07","isMarried":true}
{"index":{"_id":"3"}}
{"username":"lee","job":"java and ruby engineer","age":22,"birth":"1985-08-07","isMarried":false}
{"index":{"_id":"4"}}
{"username":"alfred junior way","job":"ruby engineer","age":23,"birth":"1989-08-07","isMarred":false}
GET /my_index/_search?q=alfred&df=user&sort=age:asc&from=4&size=10&timeout=1s
2.过滤查询
-term 与 phrase
alfred way 等价于 alfred OR way, OR 要大写
"alfred way" 短语查询
-泛查询
alfred 等价于在所有字段中匹配 term
-指定字段
name:alfred
# 检索所有字段包含alfred
GET test_search_index/_search?q=alfred
{
"profile":true
}
# 检索username包含alfred
GET test_search_index/_search?q=username:alfred
# 检索username包含alfred或者所有字段中包含way
GET test_search_index/_search?q=username:alfred way
# 检索username包含alfred或者way
GET test_search_index/_search?q=username:(alfred way)
# 检索username包含alfred way词汇
GET test_search_index/_search?q=username:"alfred way"
# 检索username包含alfred,并且age大于26
GET test_search_index/_search?q=username:alfred age:>26
# 检索birth大于1980且小于1990
GET test_search_index/_search?q=birth:(>1980 AND <1990)
3.分组查询
Group 分组设定,使用括号指定匹配规则
(quick OR brown) AND for
status:(active OR pending)title:(full text search)
####### 4.逻辑运算符
-布尔操作符
-
AND (&&), OR(||), NOT(!)
name:(tom NOT lee)
注意大写,不能小写 -
+ - 分别对应 must 和 must_not
name:(tom +lee -alfred),+ -符号要紧跟字符串
name:((lee && !alfred) || (tom && lee && !alfred))
+ 在 url 中会被解析空格,要使用 encode 后的结果,即 %2B
# 检索username包含alfred或者所有字段中包含way
GET test_search_index/_search?q=username:alfred AND way
# 检索username包含alfred和username包含way
GET test_search_index/_search?q=username:(alfred AND way)
# 检索username包含alfred或者username包含way
GET test_search_index/_search?q=username:(alfred way)
GET test_search_index/_search?q=username:(alfred OR way)
# 检索username包含alfred且不包含way
GET test_search_index/_search?q=username:(alfred NOT way)
# username包含alfred且必须包含way,+号必须写经过encode后的值
GET test_search_index/_search?q=username:(alfred AND %2Bway)
5.数值和日期范围查询
-区间写法,闭区间用[],开区间用{}
age:[1 TO 10] 意为 1<=age<=10
age:[1 TO 10} 意为 1<=age<10
age:[1 TO] 意为 age >=1
age:[* TO 10] 意为 <=10
-算数写法
age:>=1
age:(>=1 AND <= 10)
age:(+>=10 +<=10)
# 检索1<=age<=23
GET test_search_index/_search?q=age:[1 TO 23]
# 检索age<=20
GET test_search_index/_search?q=age:[* TO 20]
# 检索1<=age<28
GET test_search_index/_search?q=age:([1 TO 28})
# 检索 20<=age<=28
GET test_search_index/_search?q=age:(>=20 AND <=28)
GET test_search_index/_search?q=age:(%2b>=20 %2B<=28)
6.通配符查询
- ?代表 1 个字符, *代表 0 个或多个字符
name:t?m
name:tom*
name:t*m通配符匹配执行效率低,且占用较多内存,不建议使用
如无特殊需求,不要将?/*放在最前面
GET test_search_index/_search?q=username:/[a]?.l*/
{
"profile":true
}
7.正则和占位匹配
-正则匹配
name:/[mb]oat/
-模糊匹配
name:roam~1
匹配与 roam 差 1 个 character 的词,如 foam roams 等
-近似度匹配(proximity search)
"fox quick"~5
以 term 为单位进行差异比较,比如"quick for" "quick brown fox"都会匹配
GET test_search_index/_search?q=username:alfed~
GET test_search_index/_search?q=job:"java engineer"~2
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于