大數據利器Elasticsearch之bool查詢和boosting查詢

這是我參與8月更文挑戰的第6天,活動詳情查看:8月更文挑戰java

本Elasticsearch相關文章的版本爲:7.4.2python

bool查詢

Elasticsearch的bool查詢是使用一個或多個bool查詢子句構建的,每一個子句都是如下特定bool類型:c++

bool類型 描述 是否參與評分
must must裏面的全部子句都知足的文檔才返回
should 應該知足should裏面的0個或多個子句的文檔才返回
must_not must_not裏面的子句不能出如今文檔裏
filter 必須知足filter子句的文檔才返回

bool查詢遵循more-matches-is-better的原則。意味着知足must和should子句查詢的分數將會彙總到一塊兒做爲最後的_scorre評分。filter和must_not的子句不歸入到評分範圍,僅進行過濾文檔 下面的查詢語句就是查找獲取知足如下條件的候選人:golang

  1. tag裏面必須是有algorithm的;
  2. tag裏面必須是有it行業經驗的;
  3. 年齡不能大於36或者小於18歲;
  4. 至少熟悉如下編程語言中一種, 熟悉的語言越多越好:Python、Golang、C++或Java
POST /candidate/_search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "tag" : "algorithm" }
      },
      "filter": {
        "term" : { "tag" : "it" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 36, "lte" : 18 }
        }
      },
      "should" : [
        { "term" : { "tag" : "python" } },
        { "term" : { "tag" : "golang" } },
        { "term" : { "tag" : "c++" } },
        { "term" : { "tag" : "java" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}
複製代碼

boosting查詢

boosting查詢返回知足positive子句的文檔,而且同時下降知足negative子句的文檔的相關性得分。這樣,咱們能夠使用boosting查詢將某些文檔相關性下降而不將它們從搜索結果中排除。
下面的查詢語句將會獲得如下文檔:編程

  1. 返回全部愛好裏面喜歡蘋果的文檔;
  2. 下降喜歡蘋果的同時喜歡薯片的文檔的相關性得分。
GET /bank/_search
{
    "query": {
        "boosting" : {
            "positive" : {
                "term" : {
                    "hobby" : "apple"
                }
            },
            "negative" : {
                 "term" : {
                     "hobby" : "chips"
                }
            },
            "negative_boost" : 0.5
        }
    }
}
複製代碼

boosting查詢適合挑選最重要的但容許存在某些不過重要的條件的文檔。markdown

相關文章
相關標籤/搜索