多搜索条件组合查询

多搜索条件组合查询

GET /website/article/_search
{
“query”: {
​ “bool”: {
​ “must”: [
​ {
​ “match”: {
​ “title”: “elasticsearch”
​ }
​ }
​ ],
​ “should”: [
​ {
​ “match”: {
​ “content”: “elasticsearch”
​ }
​ }
​ ],
​ “must_not”: [
​ {
​ “match”: {
​ “author_id”: 111
​ }
​ }
​ ]
​ }
}
}

{
​ “bool”: {
​ “must”: { “match”: { “title”: “how to make millions” }},
​ “must_not”: { “match”: { “tag”: “spam” }},
​ “should”: [
​ { “match”: { “tag”: “starred” }}
​ ],
​ “filter”: {
​ “range”: { “date”: { “gte”: “2014-01-01” }}
​ }
​ }
}

bool 关键字底下可以方 以下字段:
must,must_not,should,filter

每个子查询都会计算一个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进行查询 ,需要加一个 constant_score 进行单纯的过滤

GET /company/employee/_search
{
“query”: {
​ “constant_score”: {
​ “filter”: {
​ “range”: {
​ “age”: {
​ “gte”: 30
​ }
​ }
​ }
​ }
}
}

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • © 2020 John Doe
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信