Elasticsearch基本使用
程序员文章站
2022-07-05 12:31:44
...
文章目录
一、定义一个索引
定义一个索引这个操作,在官方中叫做mappings,如果非要给mappings一个翻译,那么可以不恰当的说,mappings就是类似于关系型数据库的表结构,下面来看一下官方给mappings下的定义:
Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define:
- which string fields should be treated as full text fields.
- which fields contain numbers, dates, or geolocations.
- the format of date values.
- custom rules to control the mapping for dynamically added fields.
意思就是mappings就是定义一个文档、一个字段是如何被存储,被建立索引,例如,哪一个字段应该被建立全文索引,哪一个字段包含数值、日期、地理位置信息,如何格式化时间,当动态添加field的时候,如何自动定义这个field的类型。来一个例子,定义一个user_detail索引:
PUT /user_detail
{
"mappings": {
"properties": {
"id": { "type": "long" },
"name": { "type": "keyword" },
"height": { "type": "float" },
"weight": { "type": "float" },
"birthTime": { "type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"age": { "type": "integer"},
"birthPlace": { "type": "text" },
"phone": { "type": "keyword" }
}
}
}
见名知意,各个字段意思为,id,姓名,身高,体重,出生时间,年龄,出生地,电话号码。
二、添加数据
添加10条数据:
PUT /user_detail/_doc/1
{
"name": "杨希菲",
"height": 178.15,
"weight": 70.08,
"birthTime": "1990-01-15 18:40:13",
"age": 29,
"birthPlace": "河南省南阳市新野县中集镇石沟村18号",
"phone": "13126672335"
}
PUT /user_detail/_doc/2
{
"name": "宋振虎",
"height": 175.15,
"weight": 60.08,
"birthTime": "1995-06-25 08:40:13",
"age": 24,
"birthPlace": "河南省南阳市淅川县上集镇郭沟村23号",
"phone": "14126675335"
}
PUT /user_detail/_doc/3
{
"name": "杨宗梅",
"height": 174.15,
"weight": 75.08,
"birthTime": "1993-07-23 09:40:13",
"age": 29,
"birthPlace": "河南省南阳市新野县舞美镇李家村18号",
"phone": "13127672337"
}
PUT /user_detail/_doc/4
{
"name": "朱先进",
"height": 177.15,
"weight": 77.21,
"birthTime": "1992-01-15 18:40:13",
"age": 27,
"birthPlace": "河南省南阳市淅川县上集镇朱家村49号",
"phone": "13126672335"
}
PUT /user_detail/_doc/5
{
"name": "张康",
"height": 165.15,
"weight": 55.08,
"birthTime": "1991-09-15 18:40:13",
"age": 28,
"birthPlace": "河南省南阳市新野县祥林镇东街村18号",
"phone": "13826672339"
}
PUT /user_detail/_doc/6
{
"name": "贞洁林",
"height": 170.15,
"weight": 60.08,
"birthTime": "1993-03-15 18:40:13",
"age": 26,
"birthPlace": "河南省南阳市新野县黄庙镇贞美村18号",
"phone": "13127672435"
}
PUT /user_detail/_doc/7
{
"name": "赵美琪",
"height": 160.15,
"weight": 55.08,
"birthTime": "1992-08-15 17:40:13",
"age": 27,
"birthPlace": "河南省南阳市淅川县九重镇赵家村18号",
"phone": "13127682335"
}
PUT /user_detail/_doc/8
{
"name": "李洁",
"height": 172.15,
"weight": 66.08,
"birthTime": "1993-03-15 18:40:13",
"age": 26,
"birthPlace": "河南省南阳市镇平县黄流镇李家村18号",
"phone": "13226972335"
}
PUT /user_detail/_doc/9
{
"name": "甄东环",
"height": 170.15,
"weight": 60.08,
"birthTime": "1991-03-15 18:40:13",
"age": 28,
"birthPlace": "河南省郑州市中牟县桃花镇先平村18号",
"phone": "18129672335"
}
PUT /user_detail/_doc/10
{
"name": "靳美丽",
"height": 165.15,
"weight": 49.08,
"birthTime": "1993-08-15 18:40:13",
"age": 26,
"birthPlace": "河南省驻马店市长虹县下焦镇黄花村18号",
"phone": "13126572135"
}
注意:以上数据纯属虚构,如有侵权请及时联系我。
三、查询数据
1、查询全部数据
POST /user_detail/_search
{
"query": {
"match_all": {}
}
}
2、查询1993年出生的人
POST /user_detail/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"birthTime": {
"gte": "1993-01-01 00:00:00",
"lte": "1993-12-31 00:00:00"
}
}
}
]
}
}
}
3、查询年龄为26并且身高超过173的人
POST /user_detail/_search
{
"query": {
"bool": {
"must": [
{"term": {
"age": {
"value": 26
}
}}
],
"filter": {
"range": {
"height": {
"gte": 170
}
}
}
}
}
}
4、查询年龄等于24或者29的人
POST /user_detail/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"age": {
"value": 24
}
}
},
{
"term": {
"age": {
"value": 29
}
}
}
]
}
}
}
三、聚合数据
1、查询所有人的年龄之和
POST /user_detail/_search?size=0
{
"aggs": {
"aggs_sum_age": {
"sum": {
"field": "age"
}
}
}
}
2、查询所有人年龄的平均值
POST /user_detail/_search?size=0
{
"aggs": {
"aggs_avg_age": {
"avg": {
"field": "age"
}
}
}
}
3、查询26岁的人的平均身高
POST /user_detail/_search?size=0
{
"query": {
"term": {
"age": 26
}
},
"aggs": {
"age_26_avg_height": {
"avg": {
"field": "height"
}
}
}
}