在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" } }] } }范围查询
此查询用于查找值的范围之间的值的对象。 为此,需要使用类似 -
例如
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":[]} }