title: Elasticsearch-探索数据
date: 2019-01-18 21:14:40
tags: Elasticsearch
categories: Elasticsearch
Search API
查询的API分为两类,一类是基于URL查询的,一类是带有请求体的查询方式
工具
- ElasticSearch
- kibana(可选)/谷歌插件ElasticSearch Head/PostMan
基于URL查询
Rest API 通过访问_search
节点访问得到
GET /item/_search?q=*&sort=cId:asc
这个查询中item
指定index,q=*
代表匹配所有的index,sort=cId:asc
表示根据cId
按照asc
由小到大的顺序呢进行排序
查询结果:
{
"took": 75,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 35,
"max_score": null,
"hits": [
{
"_index": "item",
"_type": "item",
"_id": "MWTTTTGFR",
"_score": null,
"_source": {
"itemId": "MWTTTTGFR",
"name": "小米笔记本Air 13.3\"",
"cId": 6,
"itemCatName": null,
"sellPoint": "全新升级:第八代四核处理器\n5399元起",
"price": 5399,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": null
},
"sort": [
6
]
},
{
"_index": "item",
"_type": "item",
"_id": "MARADSAi",
"_score": null,
"_source": {
"itemId": "MARADSAi",
"name": "小米笔记本Air 12.5\" ",
"cId": 8,
"itemCatName": null,
"sellPoint": "在设计小米笔记本时,设计师做了一个大胆的决定:去掉笔记本正面的Logo。这个设计虽然冒险,却换来了一个优雅的、干净的表面,高度简洁的设计美学使笔记本从一个产品,成为了一件艺术品。什么都没有的正面,会不会没个性?美学家认为这恰恰提供了个性的无限可能:没有 Logo,正意味着它的每个角落都可以被装饰。还有更多艺术家机身贴纸,陆续推出。",
"price": 3599,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 4
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "AGVERFERVE",
"_score": null,
"_source": {
"itemId": "AGVERFERVE",
"name": "小米游戏本",
"cId": 8,
"itemCatName": null,
"sellPoint": "让你在游戏的世界里恣意闯荡,亦可在游戏之外更加专注极致地创作,这是我们的初衷。为此,我们邀请多\n领域的业内专家,从配置、散热,到键盘、屏幕、音效等进行了系统级的重塑,历经两年,终于将这款小米\n游戏本呈现给你。",
"price": 6399,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms_1533266333.04566853!200x200.jpg",
"remark": null,
"description": null,
"sortOrder": 5
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "PDASJSDASCS",
"_score": null,
"_source": {
"itemId": "PDASJSDASCS",
"name": "小米笔记本Air 13.3\"",
"cId": 8,
"itemCatName": null,
"sellPoint": "只需一个 GeForce®️ MX150 独立显卡和 2GB GDDR5 高速显存,让你对\n轻薄本的游戏性能完全改观。43%的性能提升,流畅运行大场景游戏。",
"price": 5399,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 5
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "QEWQEDFDF",
"_score": null,
"_source": {
"itemId": "QEWQEDFDF",
"name": "小米净水器",
"cId": 8,
"itemCatName": null,
"sellPoint": "大流量直出纯净水,健康家庭必备",
"price": 1999,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 8
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "QFCAPAK",
"_score": null,
"_source": {
"itemId": "QFCAPAK",
"name": "小米笔记本 15.6\"",
"cId": 8,
"itemCatName": null,
"sellPoint": "全面均衡的国民轻薄本\n第八代酷睿™ 四核处理器",
"price": 4199,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 1
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "BNRWRVBSFV",
"_score": null,
"_source": {
"itemId": "BNRWRVBSFV",
"name": "小米笔记本Pro 15.6″",
"cId": 8,
"itemCatName": null,
"sellPoint": "最高配备第八代英特尔® 酷睿™ i7处理器 ,无锁4核8线程,最大频率达4.0GHz,性能较上一代提升40%。\n强大而高效的多任务处理能力,让你近乎实时地创建、编辑和共享图形渲染及视频内容。",
"price": 6299,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms_1505897592.73836006!200x200.jpg",
"remark": null,
"description": null,
"sortOrder": 2
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "BRWTGVRF",
"_score": null,
"_source": {
"itemId": "BRWTGVRF",
"name": "小米电视4C 50英寸",
"cId": 8,
"itemCatName": null,
"sellPoint": "4K HDR 人工智能语音电视",
"price": 1899,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": "",
"description": null,
"sortOrder": 2
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "VAAAEFEFERF",
"_score": null,
"_source": {
"itemId": "VAAAEFEFERF",
"name": "米家空气净化器 Pro",
"cId": 8,
"itemCatName": null,
"sellPoint": "澎湃动力,净化能力更快更强",
"price": 1499,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 5
},
"sort": [
8
]
},
{
"_index": "item",
"_type": "item",
"_id": "GRJTOGVJRFGOV",
"_score": null,
"_source": {
"itemId": "GRJTOGVJRFGOV",
"name": "小米笔记本Air 13.3\"",
"cId": 8,
"itemCatName": null,
"sellPoint": "全新升级:第八代四核处理器\n",
"price": 5399,
"limitNum": 80,
"image": "http://39.105.59.55:9999/img/pms.jpg",
"remark": null,
"description": null,
"sortOrder": 4
},
"sort": [
8
]
}
]
}
}
响应中会出现的一些参数
-
took
: 查询发费的时间(ms) -
timeout
: 是否超时 -
_shards: 碎片数
-
hits
: 查询的结果
json格式的查询语言发出请求
-
匹配所有信息
match_all
URL:
localhost:9200/item/_search
请求体:
{ "query":{ "match_all":{ } } }
-
限定查询出数据的数量默认查询出来的结果数量是10
GET /item/_search { "query": { "match_all": {} }, "size": 1 }
-
分页查询
GET /item/_search { "query": { "match_all": {} }, "from": 10, "size": 10 }
-
按照某一个属性排序
GET /item/_search { "query": { "match_all": {} }, "sort": { "price": { "order": "desc" } } }
-
-
如果只写
match_all
默认会把信息的所有属性全都查出来,我们可以通过以下方式限定查询的属性GET /item/_search { "query": { "match_all": {} }, "_source": ["id", "price"] }
这样子,查询结果的字段信息就只会包含
id
和price
,类似于SQL语句里的select id,price from item
-
限定查询条件
-
匹配某个一字段
下面这个就是匹配id为20的商品
GET /item/_search { "query": { "match": { "id": 20 } } }
-
匹配多个字段,有一个就行
下面这个匹配商品厂家是mill或者lane
GET /item/_search { "query": { "match": { "address": "mill lane" } } }
-
包含
mill lane
整个短语GET /item/_search { "query": { "match_phrase": { "address": "mill lane" } } }
-
bool 查询
-
下面这个例子返回的数据必须包含
mill
和lane
GET /item/_search { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
-
下面这个例子返回的数据包含
mill
或者lane
GET /item/_search { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
-
下面这个例子返回的数据不能包含id为20的数据
GET /item/_search { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "id": 20 } } ] } } }
-
-
执行过滤操作
下面这个例子返回的数据包含id在10到20之间的数据
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"id": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
执行聚类
聚类可以理解为SQL中的GROUP BY
,对信息进行分类
下边这个例子根据商品名称进行分类
GET /item/_search
{
"size": 0,
"aggs": {
"group_by_name": {
"terms": {
"field": "name.keyword"
}
}
}
}