Elasticsearch查詢DSL
在Elasticsearch中,通過使用基於JSON的查詢進行搜索。 查詢由兩個子句組成 -
- 葉查詢子句 - 這些子句是匹配,項或範圍的,它們在特定字段中查找特定值。
- 複合查詢子句 - 這些查詢是葉查詢子句和其他複合查詢的組合,用於提取所需的信息。
Elasticsearch支持大量查詢。 查詢從查詢關鍵字開始,然後以JSON
對象的形式在其中包含條件和過濾器。以下描述了不同類型的查詢 -
匹配所有查詢
這是最基本的查詢; 它返回所有內容,併爲每個對象的分數爲1.0
。 例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"match_all":{}
}
}
響應
{
"took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":5, "max_score":1.0, "hits":[
{
"_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
"_source":{
"name":"Saint Paul School", "description":"ICSE Affiliation",
"street":"Dawarka", "city":"Delhi", "state":"Delhi",
"zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,
"tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
},
{
"_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,
"_source":{
"name":"Government School", "description":"State Board Affiliation",
"street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
"location":[18.599752, 73.6821995], "fees":500, "tags":["Great Sports"],
"rating":"4"
}
},
{
"_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
"_source":{
"name":"Central School", "description":"CBSE Affiliation",
"street":"Nagan", "city":"paprola", "state":"HP",
"zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2200, "tags":["Senior Secondary", "beautiful campus"],
"rating":"3.3"
}
},
{
"_index":"schools_gov", "_type":"school", "_id":"1", "_score":1.0,
"_source":{
"name":"Model School", "description":"CBSE Affiliation",
"street":"silk city", "city":"Hyderabad", "state":"AP",
"zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3"
}
},
{
"_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
"_source":{
"name":"Crescent School", "description":"State Board Affiliation",
"street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",
"location":[26.8535922, 75.7923988], "fees":2500,
"tags":["Well equipped labs"], "rating":"4.5"
}
}
]
}
}
全文查詢
這些查詢用於搜索整個文本,如章節或新聞文章。 此查詢根據與特定索引或文檔相關聯的分析器一起工作。 在本節中,我們將討論不同類型的全文查詢。
匹配查詢
此查詢將文本或短語與一個或多個字段的值匹配。 例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"match" : {
"city":"pune"
}
}
}
響應
{
"took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":1, "max_score":0.30685282, "hits":[{
"_index":"schools_gov", "_type":"school", "_id":"2", "_score":0.30685282,
"_source":{
"name":"Government School", "description":"State Board Afiliation",
"street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
"location":[18.599752, 73.6821995], "fees":500,
"tags":["Great Sports"], "rating":"4"
}
}]
}
}
multi_match查詢
此查詢將文本或短語與多個字段匹配。 例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"multi_match" : {
"query": "hyderabad",
"fields": [ "city", "state" ]
}
}
}
響應
{
"took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":1, "max_score":0.09415865, "hits":[{
"_index":"schools_gov", "_type":"school", "_id":"1", "_score":0.09415865,
"_source":{
"name":"Model School", " description":"CBSE Affiliation",
"street":"silk city", "city":"Hyderabad", "state":"AP",
"zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3"
}
}]
}
}
查詢字符串查詢
此查詢使用查詢解析器和query_string
關鍵字。 例如,
POST http://localhost:9200/schools/_search
請求正文
{
"query":{
"query_string":{
"query":"good faculty"
}
}
}
響應
{
"took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":1, "max_score":0.09492774, "hits":[{
"_index":"schools", "_type":"school", "_id":"2", "_score":0.09492774,
"_source":{
"name":"Saint Paul School", "description":"ICSE Affiliation",
"street":"Dawarka", "city":"Delhi", "state":"Delhi",
"zip":"110075", "location":[28.5733056, 77.0122136],
"fees":5000, "tags":["Good Faculty", "Great Sports"],
"rating":"4.5"
}
}]
}
}
期限等級查詢
這些查詢主要處理結構化數據,如數字,日期和枚舉。 例如,
POST http://localhost:9200/schools/_search
請求正文
{
"query":{
"term":{"zip":"176115"}
}
}
響應
{
"took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":1, "max_score":0.30685282, "hits":[{
"_index":"schools", "_type":"school", "_id":"1", "_score":0.30685282,
"_source":{
"name":"Central School", "description":"CBSE Affiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location":[31.8955385, 76.8380405], "fees":2200,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
}
}]
}
}
範圍查詢
此查詢用於查找值的範圍之間的值的對象。 爲此,需要使用類似 -
- gte − 大於和等於
- gt − 大於
- lte − 小於和等於
- lt − 小於
例如 -
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"range":{
"rating":{
"gte":3.5
}
}
}
}
響應
{
"took":31, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":3, "max_score":1.0, "hits":[
{
"_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
"_source":{
"name":"Saint Paul School", "description":"ICSE Affiliation",
"street":"Dawarka", "city":"Delhi", "state":"Delhi",
"zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,
"tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
},
{
"_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,
"_source":{
"name":"Government School", "description":"State Board Affiliation",
"street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
"location":[18.599752, 73.6821995] "fees":500,
"tags":["Great Sports"], "rating":"4"
}
},
{
"_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
"_source":{
"name":"Crescent School", "description":"State Board Affiliation",
"street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",
"location":[26.8535922, 75.7923988], "fees":2500,
"tags":["Well equipped labs"], "rating":"4.5"
}
}
]
}
}
其他類型的期限級查詢是 -
- 存在的查詢 - 如果某一個字段有非空值。
- 缺失的查詢 - 這與存在查詢完全相反,此查詢搜索沒有特定字段的對象或有空值的字段。
- 通配符或正則表達式查詢 - 此查詢使用正則表達式來查找對象中的模式。
類型查詢 - 具有特定類型的文檔。 例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"type" : {
"value" : "school"
}
}
}
響應
存在於指定的索引中的所有學校的JSON
對象。
複合查詢
這些查詢是通過使用如和,或,非和或等,用於不同索引或具有函數調用等的布爾運算符彼此合併的不同查詢的集合。例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"filtered":{
"query":{
"match":{
"state":"UP"
}
},
"filter":{
"range":{
"rating":{
"gte":4.0
}
}
}
}
}
}
響應
{
"took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{"total":0, "max_score":null, "hits":[]}
}
連接查詢
這些查詢用於包含多個映射或文檔的位置。 連接查詢有兩種類型 -
嵌套查詢
這些查詢處理嵌套映射(將在下一章閱讀了解更多內容)。
has_child和has_parent查詢
這些查詢用於檢索在查詢中匹配的文檔的子文檔或父文檔。 例如,
POST http://localhost:9200/tutorials/_search
請求正文
{
"query":
{
"has_child" : {
"type" : "article", "query" : {
"match" : {
"Text" : "This is article 1 of chapter 1"
}
}
}
}
}
響應
{
"took":21, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},
"hits":{
"total":1, "max_score":1.0, "hits":[{
"_index":"tutorials", "_type":"chapter", "_id":"1", "_score":1.0,
"_source":{
"Text":"this is chapter one"
}
}]
}
}
地理查詢
這些查詢處理地理位置和地理點。這些查詢有助於查找任何位置附近的學校或任何其他地理對象。需要使用地理位置數據類型。 例如,
POST http://localhost:9200/schools*/_search
請求正文
{
"query":{
"filtered":{
"filter":{
"geo_distance":{
"distance":"100km",
"location":[32.052098, 76.649294]
}
}
}
}
}
響應
{
"took":6, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
"hits":{
"total":2, "max_score":1.0, "hits":[
{
"_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
"_source":{
"name":"Saint Paul School", "description":"ICSE Affiliation",
"street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
"location":[28.5733056, 77.0122136], "fees":5000,
"tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
},
{
"_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
"_source":{
"name":"Central School", "description":"CBSE Affiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location":[31.8955385, 76.8380405], "fees":2000,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
}
}
]
}
}
注意 - 如果在執行上述示例時遇到異常,請將以下映射添加到索引。
{
"mappings":{
"school":{
"_all":{
"enabled":true
},
"properties":{
"location":{
"type":"geo_point"
}
}
}
}
}