上一篇文章介绍了ES中的Rest API,本章将重点介绍ES中的查询API的使用。由于笔者在实际项目仅仅将ES用作索引数据库,并没有深入研究过ES的搜索功能。而且鉴于笔者的搜索引擎知识有限,本文将仅仅介绍ES简单(非全文)的查询API。
笔者原本打算在本文中介绍聚合API的内容,但是写着写着发现文章有点过长,不便于阅读,故将聚合API的内容移至下一篇博客中。
引言
单单介绍理论和API是乏味和低效率的,本文将结合一个实际的例子来介绍这些API。下表是本文数据表的表结构,表名(type)为“student”。注意,studentNo是本表的id,也就是_id字段的值与studentNo的值保持一致。
字段名 | 字段含义 | 类型 | 是否能被索引 | 备注 |
---|---|---|---|---|
studentNo | 学号 | string | 是 | id |
name | 姓名 | string | 是 | |
sex | 性别 | string | 是 | |
age | 年龄 | integer | 是 | |
birthday | 出生年月 | date | 是 | |
address | 家庭住址 | string | 是 | |
classNo | 班级 | string | 是 | |
isLeader | 是否为班干部 | boolean | 是 |
上面的表结构所对应的mapping如下,将数据保存在索引名为“student”的索引中。
{
"student": {
"properties": {
"studentNo": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"male": {
"type": "string",
"index": "not_analyzed"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyyy-MM-dd"
},
"address": {
"type": "string",
"index": "not_analyzed"
},
"classNo": {
"type": "string",
"index": "not_analyzed "
},
"isLeader": {
"type": "boolean"
}
}
}
}
索引中保存的数据如下,下面介绍的所有API都将基于这个数据表。
studentNo | name | male | age | birthday | classNo | address | isLeader |
---|---|---|---|---|---|---|---|
1 | 刘备 | 男 | 24 | 1985-02-03 | 1 | 湖南省长沙市 | true |
2 | 关羽 | 男 | 22 | 1987-08-23 | 2 | 四川省成都市 | false |
3 | 糜夫人 | 女 | 19 | 1990-06-12 | 1 | 上海市 | false |
4 |