ElasticSearch_(12)布爾查詢

布爾查詢

type description
must 必須出現在匹配文檔中
filter 必須出現在文檔中,但是不打分
must_not 不能出現在文檔中
should 應該出現在文檔中

must

查找名字叫做 james 的球員

POST nba/_search
{
  "query": {
    "bool":{
      "must":[
        {
          "match":{
            "displayNameEn":"james"
          }
        }
      ]
    }
  }
}

filter

POST nba/_search
{
  "query": {
    "bool":{
      "filter":[
        {
          "match":{
            "displayNameEn":"james"
          }
        }
      ]
    }
  }
}

must not

查找 名字叫 james 的球員,但是不在 東部Eastern的 球員

POST nba/_search
{
  "query": {
    "bool":{
      "must":[
        {
          "match":{
            "displayNameEn":"james"
          }
        }
      ],
      "must_not": [
        {
          "term":{
            "teamConferenceEn": {
              "value": "Eastern"
            }
          }
        }
      ]
    }
  }
}

should

查找名字叫做 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

代表了最小匹配精度,如果設置了 minimum_should_match=1, 那麼should 語句中至少需要有一個條件滿足

查出名字叫做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
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章