Elasticsearch Search APIs

 

 

 

 

 

 

 

Elasticsearch Search APIshtml

By:授客 QQ:1033553122 mysql

 

1. 搜索 1sql

在單個索引的全部類型中搜索 1數組

在單個索引的指定類型中搜索 1app

在多個指定的索引中搜索 1elasticsearch

在全部索引中搜索 1ide

2. URI搜索 2post

3. 請求體搜索 4測試

1. query 5ui

2. from/size 5

3. sort 6

4. source filter 11

5. script field 13

6. doc field 17

7. post filter 18

8. search_after 22

 

測試環境:

Win elasticsearch-5.4.1

 

1. 搜索

在單個索引的全部類型中搜索

.customer索引中查找包含firstname字段,且值字段值包含單詞brad的文檔

GET /customer/_search?q=firstname:Brad

 

在單個索引的指定類型中搜索

.customer索引的external,sometype類型中查找包含firstname字段,且值字段值包含單詞brad的文檔

GET /customer/external,sometype/_search?q=firstname:Brad

 

在多個指定的索引中搜索

.customeraccount索引中查找包含firstname字段,且值字段值包含單詞brad的文檔

GET /account,customer/sometype/_search?q=firstname:Brad

GET /account,customer/_search?q=firstname:Brad

 

注意:索引之間只能以逗號隔開,不能有空格,好比account, customer

 

 

 

在全部索引中搜索

.在全部索引的sometype類型中查找包含firstname字段,且值字段值包含單詞brad的文檔

GET /_all/sometype/_search?q=firstname:Brad

 

.在全部索引中查找包含firstname字段,且值字段值包含單詞brad的文檔

GET /_all/_search?q=firstname:Brad

 

或者

GET /_search?q=firstname:Brad

 

參考連接:

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

 

 

2. URI搜索

經常使用參數說明:

q: 要查詢的字段值

 

. customer索引external類型中查找字段值16623的文檔

GET /customer/external/_search?q=16623

 

_source:指定文檔中hits包含的字段值

 

. customer索引external類型中查找字段值16623的文檔,僅返回firstname,lastname,balance字段

 

GET /customer/external/_search?q=16623&_source=firstname,lastname,balance

 

注意:字段值之間只能以逗號分隔,且不能包含空格,好比firstname, lastname,

 

sort:用於排序文檔,格式 fieldNamefieldName:asc fieldName:desc

其中,asc表示按fieldName字段值升序排序,同不帶fieldName,相反desc表示降序排序,能夠按多個字段排序,格式形如 fieldName1:asc,fieldName2:desc,的先按字段fieldName1的值升序排序,fieldName1值相同的話,再按fieldName2的值降序排序

 

.查詢customer索引external類型中的全部文檔,按balance字段值升序排序。

GET /customer/external/_search?sort=balance:asc

 

.查詢customer索引external類型中的全部文檔,按balance字段值升序排序,balance字段值相同則按account_number降序排序。

GET /customer/external/_search?sort=balance:asc,account_number:desc

 

from:指定須要返回記錄的起始索引,默認爲0,能夠理解爲mysql查詢 limit子句的 offset

 

size:須要返回的記錄數,默認爲10

 

 

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html

 

3. 請求體搜索

. 查詢/customer索引,external類型中包含firstname字段,且值爲Braw的記錄

POST /customer/external/_search?pretty

{

   "query": {

      "term": {

         "firstname": "braw"

      }

   }

}

 

注意:PUT也能夠替換爲GET

注意:例中,若是把"firstname": "braw" 改爲 "firstname": "Braw",查詢查不到結果,估計默認設置的狀況下,先把文檔字段值轉小寫後進行的比較

 

返回結果部分截圖

 

說明:默認狀況下,查詢結果不區分大小,可是字段名是區分大小寫的。

 

常見參數:參考 URI搜索

 

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html

 

1. query

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-query.html

 

2. from/size

.返回查詢結果中,索引大於等於1的記錄,總的返回一條記錄

POST customer/external/_search?pretty

{

   "query": {

      "term": {

         "firstname": "braw"

      }

   },

   "from": 1,

   "size": 1

}

 

注意:from + size不能大於index.max_result_window設置的值(默認1000)

 

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

 

3. sort

sort 對查詢結果排序

 

. 對查詢結果排序,按account_number降序排序,account_number相同的狀況下,按balance升序排序

POST customer/external/_search?pretty

{

   "query": {

      "match_all": {}

   },

   "sort": [

      {

         "account_number":{ "order": "desc"},

         "balance":"asc"

      }

   ]

}

 

說明:

一、desc:降序,asc:升序

如上,"account_number":{ "order": "desc"},也能夠簡單寫成"account_number":"desc",

 

sort mode選項

mode選項用於字段值爲數組列表、多個值組成的字段排序,可選值以下:

min

 

 

選擇數組中的最小值,用於字段排序

 

max

選擇數組中的最大值,用於字段排序

 

sum

使用數組中全部值總和,用於字段排序,僅限於字段值由數字組成的數組

 

avg

使用數組中全部值的均值,用於字段排序,僅限於字段值由數字組成的數組

 

median

使用數組中全部值的中位數,用於字段排序,僅限於字段值由數字組成的數組

 

按以下方式建立一些文檔記錄

PUT /product/fruit/4?pretty

{

    "product":"orange",

    "price":[12, 17, 22]

}

 

例子.price字段的數組均值降序排序查詢結果

POST /product/fruit/_search

{

   "query": {

      "match_all": {}

   },

   "sort": [

      {

         "price": {

            "order": "desc",

            "mode": "avg"

         }

      }

   ]

}

 

 

嵌套對象裏的排序

嵌套對象映射

.設置offer字段爲嵌套對象(同時也會執行類型的建立操做)

PUT /product

{

   "mappings": {

      "myfruit": {

         "properties": {

            "offer": {

               "type": "nested",

               "properties": {

                  "price": {"type":"short"}

               }

            }

         }

      }

   }

}

 

PUT /product/myfruit/1?pretty

{

   "product": "orange",

   "offer": [{

      "price": [

         12,

         17,

         22

      ]

   }]

}

 

PUT /product/myfruit/2?pretty

{

   "product": "apple",

   "offer": [{

      "price": [

         14,

         10,

         9

      ]

   }]

}

 

PUT /product/myfruit/3?pretty

{

   "product": "apple",

   "offer": [

      {}

   ]

}

 

POST /product/myfruit/_search

{

   "query": {

      "match_all": {}

   },

   "sort": [

      {

         "offer.price": {

            "order": "asc",

            "mode": "avg",

            "nested_path":"offer"

         }

      }

   ]

}

 

說明:

nested_path:指明在哪一個嵌套對象上進行排序

 

missing參數

missing參數用於指定,文檔缺少指定字段時的處理方式,missing參數值能夠設置爲_last(默認值,即位於最下方) _first(位於最上方)、或者其它自定義值,該參數值將用於排序。

 

修改上述例中,文檔3以下

PUT /product/myfruit/3?pretty

{

   "product": "apple",

   "offer": [

      {}

   ]

}

 

POST /product/myfruit/_search

{

   "query": {

      "match_all": {}

   },

   "sort": [

      {

         "offer.price": {

            "order": "asc",

            "mode": "avg",

            "missing":"_first",

            "nested_path": "offer"

         }

      }

   ]

}

 

返回結果部分截圖:

 

 

更多參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html

4. source filter

.不返回查詢結果中的字段

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "_source":false

}

 

返回結果:

 

 

 

.僅返回查詢結果中指定的字段,firstnameaccount_number

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "_source": [

      "firstname",

      "account_number"

   ]

}

返回結果部分截圖

 

使用通配符

.僅返回查詢結果中以em,或者字母a開頭字段

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "_source": [

      "a*",

      "em*"

   ]

}

 

返回結果部分截圖

 

 

includesexcludes

.僅返回查詢結果中字段名以字符a開頭,但不以em開頭的字段

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "_source": {

      "includes": [

         "a*"

      ],

      "excludes": [

         "em*"

      ]

   }

}

 

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

 

 

5. script field

返回腳本計算值(基於不一樣字段)

.

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "script_fields": {

      "test1": {

         "script": {

            "inline": "doc['account_number'].value * 2"

         }

      },

      "test2": {

         "script": {

            "inline": "doc['account_number'].value * params.factor",

            "params": {

               "factor": 3

            }

         }

      }

   }

}

 

注意:這裏,account_number爲文檔中已存在的字段名

 

返回結果

 

 

.

POST /customer/external/_search?

{

   "script_fields": {

      "test1": {

         "script": {

            "inline": "params.factor * params.factor",

            "params": {

               "factor": 3

            }

         }

      }

   }

}

 

返回結果

 

.訪問 _source,返回firstname的值

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

   "script_fields": {

      "test1": {

         "script": "params['_source']['firstname']"

      }

   }

}

 

返回結果部分截圖

 

注意:使用doc['my_field_name'].value比使用arams['_source']['my_field_name']更快更效率,推薦使用

 

參考連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html

 

 

6. doc field

.

POST /customer/external/_search?

{

   "query": {

      "match_all": {}

   },

    "docvalue_fields" : ["account_number", "test2"]

}

 

返回結果部分截圖

 

 

7. post filter

filteraggregation完成後才被執行。

 

PUT /shirts

{

    "mappings": {

        "item": {

            "properties": {

                "brand": { "type": "keyword"},

                "color": { "type": "keyword"},

                "model": { "type": "keyword"}

            }

        }

    }

}

 

PUT /shirts/item/1?refresh

{

    "brand": "gucci",

    "color": "red",

    "model": "slim"

}

 

.僅返回搜索結果中包含colorredbrandgucci的文檔記錄

POST /shirts/_search

{

   "query": {

      "bool": {

         "filter": [

            {

               "term": {

                  "color": "red"

               }

            },

            {

               "term": {

                  "brand": "gucci"

               }

            }

         ]

      }

   }

}

 

.僅返回搜索結果中包含colorredbrandguccishirt,按model分組,按分組統計數降序排序

POST /shirts/_search

{

   "query": {

      "bool": {

         "filter": [

            {

               "term": {

                  "color": "red"

               }

            },

            {

               "term": {

                  "brand": "gucci"

               }

            }

         ]

      }

   },

   "aggs": {

      "models": {

         "terms": {

            "field": "model"

         }

      }

   }

}

 

返回結果部分截圖

 

 

.

.僅搜索brand值爲guccishirt,按color分組,降序展現每種colorshirt數量,同時,針對colorredshirt商品,按model分組統計,降序展現每種model的數量

 

POST /shirts/_search

{

   "query": {

      "bool": {

         "filter": {

            "term": {

               "brand": "gucci"

            }

         }

      }

   },

   "aggs": {

      "group_by_colors": {

         "terms": {

            "field": "color"

         }

      },

      "color_red": {

         "filter": {

            "term": {

               "color": "red"

            }

         },

         "aggs": {

            "group_by_models": {

               "terms": {

                  "field": "model"

               }

            }

         }

      }

   },

   "post_filter": {

      "term": {

         "color": "red"

      }

   }

}

 

說明:   "post_filter",做用於最後,不展現color不爲redshirt記錄

 

返回結果部分截圖

 

8. search_after

.以下,每頁只顯示5條記錄,按leve_vale降序排序,若是leve_vale相同則按_uid降序排序

POST /fenxi/fenxishuj/_search?

{

   "query": {

      "match_all": {}

   },

   "sort": [

      {

         "leve_vale":"desc",

         "_uid": "desc"

      }

   ],

   "size":5

}

 

返回結果部分截圖

 

 

這時,在不改變頁size值的狀況下,咱們想查看下一頁的記錄,咋辦?

 

方案:把sort中的參數值,按出現順序,依次傳遞給search_after

POST /fenxi/fenxishuj/_search?

{

   "query": {

      "match_all": {}

   },

   "search_after":[31,"fenxishuj#9"],

   "sort": [

      {"leve_vale":"desc",

         "_uid": "desc"

      }

   ],

   "size":5

}

 

注意:

1sort中的參數值要和search_after一一對應(數量&順序的對應)

2、使用了search_after的狀況下,若是要使用from參數,參數值只能爲0 -1

 

參考資料:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html

 


 

更多資料參考:

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

相關文章
相關標籤/搜索