跳到主要内容

十八、Elasticsearch 教程: 查询描述语言

Elasticsearch 使用基于 JSON 数据格式的查询来执行搜索

查询由两个子句组成:

1、 叶子查询短语

这种短语包括 **匹配** ( matching ) 、词 ( term ) 或范围 ( range ),用于在特定的字段中查找特定的值

2、 复合查询短语

这种查询短语由 **叶子查询短语** 和其它复合查询短语组成,用于提取所需要的数据

Elasticsearch 支持数量庞大的各种不同类型的查询

这些查询以 query 开始,然后使用 JSON 对象包含条件和过滤器

本章接下来的内容将学习和介绍各种不同类型的查询

匹配所有的查询

这是最基本的查询,它会返回所有的内容,其中的每个对象的得分都是 1.0

这种查询的请求正文一般为

{
"query":{
"match_all":{}
}

例如下面的请求用于查询 user* 索引中的所有数据

POST http://localhost:9200/user*/_search?pretty

请求正文

{
"query":{
"match_all":{}
}

响应内容

{
"took": 21,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"nickname": "枫晚",
"description": "停车坐爰枫林晚",
"street": "苏州大学",
"city": "Suzhou",
"state": "Jiangsu",
"zip": "215006",
"location": [
120.65426,
31.30797
],
"money": 10235,
"tags": [
"Java",
"Android"
],
"vitality": "3.5"
}
},
{
"_index": "user_admin",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"nickname": "雅少",
"description": "虚怀若谷",
"street": "四川大学",
"city": "Chengdu",
"state": "Sichuan",
"zip": "610044",
"location": [
104.094537,
30.640174
],
"money": 68023,
"tags": [
"Python",
"HTML"
],
"vitality": "7.8"
}
},
{
"_index": "user",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"nickname": "question",
"description": "问题少年也是少年",
"street": "张江高科技园区",
"city": "Shanghai",
"state": "Shanghai",
"zip": "201204",
"location": [
121.60632,
31.199305
],
"money": 13648,
"tags": [
"VUE",
"HTML"
],
"vitality": "8.8"
}
},
{
"_index": "user_admin",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"nickname": "站长",
"description": "pottercoding.cn 程序员波特,程序员编程资料站 ,教程 ",
"street": "东四十条",
"city": "Beijing",
"state": "Beijing",
"zip": "100007",
"location": [
116.432727,
39.937732
],
"money": 5201814,
"tags": [
"PHP",
"Python"
],
"vitality": "9.0"
}
},
{
"_index": "user_admin",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"nickname": "歌者",
"description": "程序设计也是设计,研发新菜也是研发",
"street": "五道口",
"city": "Beijing",
"state": "Beijing",
"zip": "100083",
"location": [
116.346346,
39.999333
],
"money": 71128,
"tags": [
"Java",
"Scala"
],
"vitality": "6.9"
}
}
]
}