- List item
- match all
- match
- multi match
- range query
- term query
- terms query
- exist query
- bool 中可以放那些语法
- 只想用 filter
- _validate & explain
- 默认排序规则
- 定制排序规则
match all
GET /_search
{
"query": {
"match_all": {}
}
}
match
搜索所有 index 中 title 包含 my elasticsearch article 内容。
GET _search
{
"query": {
"match": {
"age":33
}
}
}
记住:搜索的内容会默认会安装该字段的 mapping 进行分词匹配
multi match
一个查询文本在多个字段中匹配,其中一个字段中有则返回
GET company/employee/_search
{
"query": {
"multi_match": {
"query": "beijing",
"fields": ["address.country","address.city"]
}
}
}
range query
范围
GET company/employee/_search
{
"query": {
"range": {
"age": {
"gte": 30
}
}
}
}
term query
将搜索文本不分词查询。
GET company/employee/_search
{
"query": {
"term": {
"name": "tom"
}
}
}
terms query
多个词查询
GET company/employee/_search
{
"query": {
"terms": {
"name": [
"jack",
"tom"
]
}
}
}
bool 中可以放那些语法
bool 中可以放:must,must_not,should,filter
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }}
],
"filter": {
"range": { "date": { "gte": "2014-01-01" }}
}
}
}
每个子查询都会计算一个 document 针对它的相关度分数,然后 bool 综合所有分数,合并为一个分数,当然 filter 是不会计算分数的
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }}
],
"filter": {
"bool": {
"must": [
{ "range": { "date": { "gte": "2014-01-01" }}},
{ "range": { "price": { "lte": 29.99 }}}
],
"must_not": [
{ "term": { "category": "ebooks" }}
]
}
}
}
}
只想用 filter
上面说到 bool 中可以放:must,must_not,should,filter,但是不支持直接放 filter, 但是可以通过 constant_score(恒定分数,所有分数都是 1)来实现
GET company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
_validate & explain
验证语法是否正确,和查看计划/错误信息
比如以下查询:match 写成了 math
GET company/employee/_validate/query?explain
{
"query": {
"math": {
"name": "tom"
}
}
}
-- -----------------------------------响应
{
"valid": false,
"error": "org.elasticsearch.common.ParsingException: no [query] registered for [math]"
}
-- ----------------- 改正之后
{
"valid": true,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"explanations": [
{
"index": "company",
"valid": true,
"explanation": "+name:tom #*:*"
}
]
}
explain 参数在验证失败的情况下,会返回错误消息;验证通过的情况下,会返回计划,如在哪个 index 上查询等信息
一般用在那种特别复杂庞大的搜索下,比如你一下子写了上百行的搜索,这个时候可以先用 validate api 去验证一下,搜索是否合法
默认排序规则
默认情况下,是按照_score 降序排序的
然而,某些情况下,可能没有有用的_score,比如说 filter
GET /_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"address.city": "shanghai"
}
}
}
}
}
当然,也可以是 bool
GET /_search
{
"query": {
"bool": {
"filter": {
"term": {
"address.city": "shanghai"
}
}
}
}
}
定制排序规则
按员工入职时间升序排列:
GET company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
},
"sort": [
{
"join_date": {
"order": "desc"
}
}
]
}
使用自定义排序后,_score会变成 null