ElasticSearch7.2之布尔查询

  • 布尔查询

type
description
must
必须出现在匹配⽂档中
filter
必须出现在⽂档中,但是不打分
must_not
不能出现在⽂档中
should
应该出现在⽂档中
  • must (查找名字叫做James的球员)

POST /nba/_search
{
    "query": {
        "bool": {
            "must": [
                 {
                    "match": {
                        "displayNameEn": "james"
                     }
                 }
             ]
         }
     }
}
  • 效果同must,但是不打分(查找名字叫做James的球员)

POST /nba/_search
{
    "query": {
        "bool": {
            "filter": [
                {
                    "match": {
                        "displayNameEn": "james"
                     }
                 }
             ]
         }
     }
}
  • must_not (查找名字叫做James的⻄部球员)

POST /nba/_search
{
    "query": {
        "bool": {
            "must": [
                 {
                    "match": {
                        "displayNameEn": "james"
                     }
                 }
             ],
            "must_not": [
                 {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                         }
                     }
                 }
             ]
         }
    }
}
  • should(查找名字叫做James的打球时间应该在1120年⻄部球员)

   1、即使匹配不到也返回,只是评分不同
POST /nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                        }
                    }
                }
            ],
            "should": [
                {
                    "range": {
                        "playYear": {
                            "gte": 11,
                            "lte": 20
                        }
                    }
                }
            ]
        }
    }
}
   2、 如果 minimum_should_match=1 ,则变成要查出名字叫做 James 的打球时间在 11 20 年⻄部 球员
POST /nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                        }
                    }
                }
            ],
            "should": [
                {
                    "range": {
                        "playYear": {
                            "gte": 11,
                            "lte": 20
                        }
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}
发布了92 篇原创文章 · 获赞 3 · 访问量 5158

猜你喜欢

转载自blog.csdn.net/qq_22049773/article/details/103162174