Elasticsearch提供了許多的搜尋語法,讓我們能透過這些語法的組合,可以查詢出各式各樣的結果,接下來就開始介紹一些常用的查詢語法,來讓大家了解一下。
match_all 表示查詢所有數據。在沒有指定其它查詢方式時,是預設查詢方式。
{ "match_all": {}}
match 全文檢索的標準查詢,會對查詢的關鍵字進行分詞,並對每個分詞的字串進行查詢(預設為or)。
{ "match": { "name": "Jackson Selena" }}
查詢欄位
name,資料中包含Jackson或Selena。
multi_match 可以在在多個欄位來查詢資料。
{
"multi_match": {
"query": "Marrakesh Low",
"fields": [ "manufacturer", "city_name" ]
}
}
查詢欄位
manufacturer或city_name,資料中有包含Marrakesh或Low這二個關鍵字。
range 可查詢出落在指定區間的資料
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
查詢資料中
age>=20並且age<30的資料。
term可以用來精準搜尋,可以是數字、時間、布林值或者是設定了not_analyzed不分詞的字串。
{ "term": { "id": 27 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
查詢
id值為27的資料。
查詢date值為2014-09-01的資料。
查詢public值為true的資料。
查詢tag值為full_text的資料。
terms和 term 是相同的用法,只是terms可以搜尋多個字串。
{ "terms": { "id": [ 27, 43 ] }}
查詢
id值為27或43的資料。
查詢欄位不為空的數據(column is not null)。
{
"exists": {
"field": "title"
}
}
查詢數據中欄位
title不為空的資料。