在Elasticsearch中,搜索是通過使用基于JSON的查詢來進行的。查詢由兩個子句組成-
葉子查詢子句——這些子句是匹配的、術(shù)語或范圍,它們在特定字段中查找特定的值。
復合查詢子句—這些查詢是葉查詢子句和其他復合查詢的組合,用于提取所需的信息。
Elasticsearch支持大量查詢。查詢以查詢關(guān)鍵字開頭,然后以JSON對象的形式包含條件和過濾器。下面描述了不同類型的查詢。
這是最基本的查詢;它返回所有內(nèi)容,每個對象的得分為1.0。
POST /schools/_search
{
"query":{
"match_all":{}
}
}在運行上面的代碼時,我們得到以下結(jié)果-
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "5",
"_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",
"_type" : "school",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "City Best School",
"description" : "ICSE",
"street" : "West End",
"city" : "Meerut",
"state" : "UP",
"zip" : "250002",
"location" : [
28.9926174,
77.692485
],
"fees" : 3500,
"tags" : [
"fully computerized"
],
"rating" : "4.5"
}
}
]
}
}這些查詢用于搜索全文,例如章節(jié)或新聞文章。該查詢根據(jù)與該特定索引或文檔相關(guān)聯(lián)的分析器工作。在本節(jié)中,我們將討論全文查詢的不同類型。
此查詢將文本或短語與一個或多個字段的值匹配。
POST /schools*/_search
{
"query":{
"match" : {
"rating":"4.5"
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 44,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47000363,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "4",
"_score" : 0.47000363,
"_source" : {
"name" : "City Best School",
"description" : "ICSE",
"street" : "West End",
"city" : "Meerut",
"state" : "UP",
"zip" : "250002",
"location" : [
28.9926174,
77.692485
],
"fees" : 3500,
"tags" : [
"fully computerized"
],
"rating" : "4.5"
}
}
]
}
}此查詢將一個或多個字段匹配的文本或短語匹配。
POST /schools*/_search
{
"query":{
"multi_match" : {
"query": "paprola",
"fields": [ "city", "state" ]
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "5",
"_score" : 0.9808292,
"_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"
}
}
]
}
}該查詢使用查詢解析器和query_string關(guān)鍵字。
POST /schools*/_search
{
"query":{
"query_string":{
"query":"beautiful"
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 60,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
………………………………….這些查詢主要處理結(jié)構(gòu)化數(shù)據(jù),例如數(shù)字,日期和枚舉。
POST /schools*/_search
{
"query":{
"term":{"zip":"176115"}
}
}運行上面的代碼后,我們得到如下所示的響應:
……………………………..
hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "5",
"_score" : 0.9808292,
"_source" : {
"name" : "Central School",
"description" : "CBSE Affiliation",
"street" : "Nagan",
"city" : "paprola",
"state" : "HP",
"zip" : "176115",
"location" : [
31.8955385,
76.8380405
],
}
}
]
…………………………………………..該查詢用于查找具有給定值范圍之間的值的對象。為此,我們需要使用運算符,例如-
gte ?大于等于
gt ?大于
lte ?小于等于
lt ?小于
例如,觀察下面給出的代碼-
POST /schools*/_search
{
"query":{
"range":{
"rating":{
"gte":3.5
}
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 24,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "schools",
"_type" : "school",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "City Best School",
"description" : "ICSE",
"street" : "West End",
"city" : "Meerut",
"state" : "UP",
"zip" : "250002",
"location" : [
28.9926174,
77.692485
],
"fees" : 3500,
"tags" : [
"fully computerized"
],
"rating" : "4.5"
}
}
]
}
}還存在其他類型的術(shù)語級別查詢,例如-
存在查詢 ?如果某個字段的值為非空值。
缺少查詢 ?這與存在查詢完全相反,該查詢搜索沒有特定字段或值為空的對象。
通配符或regexp查詢 ?此查詢使用正則表達式在對象中查找模式。
這些查詢是不同查詢的集合,這些查詢通過使用布爾運算符(例如和/或,或不)或針對不同的索引或具有函數(shù)調(diào)用等彼此合并。
POST /schools/_search
{
"query": {
"bool" : {
"must" : {
"term" : { "state" : "UP" }
},
"filter": {
"term" : { "fees" : "2200" }
},
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}這些查詢處理地理位置和地理位置。這些查詢有助于找出學?;蛉魏纹渌乩砦恢酶浇牡乩韺ο?。您需要使用地理位置數(shù)據(jù)類型。
PUT /geo_example
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}運行上面的代碼后,我們得到如下所示的響應:
{ "acknowledged" : true,
"shards_acknowledged" : true,
"index" : "geo_example"
}現(xiàn)在,我們將數(shù)據(jù)發(fā)布到上面創(chuàng)建的索引中。
POST /geo_example/_doc?refresh
{
"name": "Chapter One, London, UK",
"location": {
"type": "point",
"coordinates": [11.660544, 57.800286]
}
}運行上面的代碼后,我們得到如下所示的響應:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
"_index" : "geo_example",
"_type" : "_doc",
"_id" : "hASWZ2oBbkdGzVfiXHKD",
"_score" : 1.0,
"_source" : {
"name" : "Chapter One, London, UK",
"location" : {
"type" : "point",
"coordinates" : [
11.660544,
57.800286
]
}
}
}
}