elasticsearch Query DSL(三)

本文參考官方提供api提煉出來的html

https://www.elastic.co/guide/en/elasticsearch/reference/current/search.htmlapi

Compound Queries緩存

複合查詢包裝其餘複合或葉子查詢,以組合其結果和分數,更改其行爲,或從查詢切換到篩選器上下文。app

Constant Score Querydom

一個包裝另外一個查詢的查詢,只返回一個等於篩選器中每一個文檔的查詢提高的常量分數。elasticsearch

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}

Bool Queryide

用於組合多個葉或化合物查詢子句,做爲默認查詢 mustshouldmust_not,或filter條款。在mustshould 條款有他們的分數相結合-更匹配的條款,更好的-而must_notfilter條款在過濾器上下文中執行。函數

發生 描述

mustui

子句(查詢)必須出如今匹配的文檔中,並有助於得分。this

filter

子句(查詢)必須出如今匹配的文檔中。可是不一樣於 must查詢的分數將被忽略。過濾器子句在過濾器上下文中執行,這意味着忽略評分並考慮使用子句進行高速緩存。

should

子句(查詢)應出如今匹配的文檔中。若是 bool查詢位於查詢上下文中而且具備mustorfilter子句,則bool即便沒有 should查詢匹配,文檔也將與查詢匹配。在這種狀況下,這些條款僅用於影響分數。若是bool查詢是過濾器上下文, 或者既沒有must或者filter至少有一個should查詢必須與文檔匹配,以便與bool查詢匹配。能夠經過設置minimum_should_match參數來顯式控制此行爲 。

must_not

子句(查詢)不得出如今匹配的文檔中。子句在過濾器上下文中執行,這意味着忽略評分並考慮使用子句進行高速緩存。因爲忽略了評分,所以0會返回全部文檔的分數。

GET _search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "status": "active"
        }
      }
    }
  }
}

等價

GET _search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "status": "active"
        }
      }
    }
  }
}

Dis Max Query

一個接受多個查詢的查詢,並返回與任何查詢子句匹配的任何文檔。當bool查詢組合來自全部匹配查詢的分數時,dis_max查詢使用單個最佳匹配查詢子句的分數。

GET /_search
{
    "query": {
        "dis_max" : {
            "tie_breaker" : 0.7,
            "boost" : 1.2,
            "queries" : [
                {
                    "term" : { "age" : 34 }
                },
                {
                    "term" : { "age" : 35 }
                }
            ]
        }
    }
}

Function Score Query

function_score容許你修改的由查詢檢索文檔的分數。

要使用function_score,用戶必須定義​​查詢和一個或多個函數,這些函數計算查詢返回的每一個文檔的新分數。

GET /_search
{
    "query": {
        "function_score": {
            "query": { "match_all": {} },
            "boost": "5",
            "random_score": {},
            "boost_mode":"multiply"
        }
    }
}
GET /_search
{
    "query": {
        "function_score": {
          "query": { "match_all": {} },
          "boost": "5",
          "functions": [
              {
                  "filter": { "match": { "test": "bar" } },
                  "random_score": {},
                  "weight": 23
              },
              {
                  "filter": { "match": { "test": "cat" } },
                  "weight": 42
              }
          ],
          "max_boost": 42,
          "score_mode": "max",
          "boost_mode": "multiply",
          "min_score" : 42
        }
    }
}

每一個文檔都由定義的函數評分。該參數 score_mode指定計算得分的組合方式:

multiply

分數乘以(默認)

sum

得分總和

avg

分數是平均的

first

應用具備匹配過濾器的第一個函數

max

使用最高分

min

使用最低分數

該參數boost_mode定義:

multiply

查詢得分和功能得分相乘(默認)

replace

僅使用功能得分,忽略查詢得分

sum

查詢得分和功能得分被添加

avg

平均

max

最大查詢分數和功能分數

min

最小查詢得分和功能得分

script_score函數容許您使用腳本表達式包裝另外一個查詢並使用從doc中其餘數字字段值派生的計算來自定義它的評分。

GET /_search
{
    "query": {
        "function_score": {
            "query": {
                "match": { "message": "elasticsearch" }
            },
            "script_score" : {
                "script" : {
                  "source": "Math.log(2 + doc['likes'].value)"
                }
            }
        }
    }
}

 

field_value_factor功能容許您使用文檔中的字段來影響分數。它與使用該script_score函數相似,但它避免了腳本的開銷。若是在多值字段上使用,則僅在計算中使用該字段的第一個值。

GET /_search
{
    "query": {
        "function_score": {
            "field_value_factor": {
                "field": "likes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
            }
        }
    }
}

這將轉化爲如下評分公式:

sqrt(1.2 * doc['likes'].value)

該功能有多種選擇field_value_factor

field

要從文檔中提取的字段。

factor

將字段值乘以的可選因子,默認爲1

modifier

修改適用於該字段的值,能夠是一個:nonelog, log1plog2plnln1pln2psquaresqrt,或reciprocal。默認爲none

 

修改 含義

none

不要對字段值應用任何乘數

log

取字段值的經常使用對數

log1p

將1添加到字段值並採用經常使用對數

log2p

將2添加到字段值並採用經常使用對數

ln

取字段值的天然對數

ln1p

將1添加到字段值並採用天然對數

ln2p

將2添加到字段值並採用天然對數

square

平方字段值(乘以它本身)

sqrt

取字段值的平方根

reciprocal

報答字段值,同1/x那裏x是該字段的值

missing

若是文檔沒有該字段,則使用的值。修飾符和因子仍然應用於它,就像從文檔中讀取同樣。

Boosting Query

返回與positive查詢匹配的文檔,但減小與negative查詢匹配的文檔的分數。

GET /_search
{
    "query": {
        "boosting" : {
            "positive" : {
                "term" : {
                    "field1" : "value1"
                }
            },
            "negative" : {
                 "term" : {
                     "field2" : "value2"
                }
            },
            "negative_boost" : 0.2
        }
    }
}

Joining Queries

Nested Query

嵌套查詢容許查詢嵌套對象/文檔

GET /_search
{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}

Has Child Query

has_child過濾器接受查詢和子類型針對其運行,並致使具備子文檔與查詢匹配的父文件。

GET /_search
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
            "query" : {
                "term" : {
                    "tag" : "something"
                }
            }
        }
    }
}

has_child也有得分支持。支持的得分模式minmaxsumavgnone。默認值爲 noneand,產生與先前版本相同的行爲。若是將得分模式設置爲除了以外的其餘值none,則將全部匹配的子文檔的得分聚合到關聯的父文檔中。

GET /_search
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
            "score_mode" : "min",
            "query" : {
                "term" : {
                    "tag" : "something"
                }
            }
        }
    }
}

Has Parent Query

has_parent查詢接受查詢和父類型。查詢在父文檔空間中執行,該父文檔空間由父類型指定。此查詢返回關聯父項已匹配的子文檔。對於其他has_parent查詢具備相同的選項,其工做方式與has_child查詢相同。

GET /_search
{
    "query": {
        "has_parent" : {
            "parent_type" : "blog",
            "score" : true,
            "query" : {
                "term" : {
                    "tag" : "something"
                }
            }
        }
    }
}

Parent Id Query

parent_id查詢可用於查找屬於特定父級的子文檔。

GET /my_index/_search
{
  "query": {
    "parent_id": {
      "type": "my_child",
      "id": "1"
    }
  }
}

此查詢有兩個必需參數:

type

類型名稱,在指定的join領域

id

父文檔的ID。

ignore_unmapped

設置爲truethis時將忽略未映射,type而且不匹配此查詢的任何文檔。在查詢可能具備不一樣映射的多個索引時,這很是有用。設置爲false(默認值)時,若是type未映射,查詢將引起異常。

相關文章
相關標籤/搜索