Elasticsearch数据查询
数据准备:
PUT /shop
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
PUT /shop/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "float"
},
"stock": {
"type": "integer"
}
}
高速查询}
POST /shop/goods/_bulk
{"index":{}}
{"title": "⼩⽶8 全⾯屏游戏智能⼿机 6GB+64GB ⿊⾊全⽹通4G 双卡双待","price": 2299.00,"stock": 8800}
{"index":{}}
{"title": "OPPO Find X曲⾯全景屏波尔多红 8GB+128GB 全⽹通移动联通电信全⽹通4G 双卡双待⼿机","price": 4999.00,"stock": 5600} {"index":{}}
{"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英⼨游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz⿊)","price": 8599.00,"stock": 1900} {"index":{}}
{"title": "TP-LINK TL-WDR5620 1200M 5G双频智能⽆线路由器四天线智能wifi 稳定穿墙⾼速家⽤路由器","price": 109.00,"stock": 9970}
⼀、基本查询
语法:
GET /索引库名/_search
{
"query": {
"查询类型": {
"查询条件": "查询条件值"
}
}
}
查询类型:match_all,match,term,range,fuzzy,bool 等等
查询条件:查询条件会根据类型的不同,写法也有差异
1.1 查询所有(match_all)
查询指令:
GET /shop/_search
{
"query": {
"match_all": {}
}
}
查询结果:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "P4EGtmgBBdkQnU_d8b7I",
"_score": 1,
"_source": {
"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英⼨游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz⿊)", "price": 8599,
"stock": 1900
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": 1,
"_source": {
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能⽆线路由器四天线智能wifi 稳定穿墙⾼速家⽤路由器",
"price": 109,
"stock": 9970
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PYEGtmgBBdkQnU_d4b4m",
"_score": 1,
"_source": {
"title": "⼩⽶8 全⾯屏游戏智能⼿机 6GB+64GB ⿊⾊全⽹通4G 双卡双待",
"price": 2299,
"stock": 8800
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PoEGtmgBBdkQnU_d6b4Q",
"_score": 1,
"_source": {
"title": "OPPO Find X曲⾯全景屏波尔多红 8GB+128GB 全⽹通移动联通电信全⽹通4G 双卡双待⼿机",
"price": 4999,
"stock": 5600
}
}
]
}
}
对查询结果进⾏分页
GET /shop/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 2
}
1.2 匹配查询(match)
or 关系:会把查询条件进⾏分词,然后进⾏查询,多个词条之间是or的关系
查询指令:
GET /shop/_search
{
"query": {
"match": {
"query": "⼩⽶⼿机",
"operator": "or"
}
}
}
}
查询结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.6051829,
"hits": [
{
"_index": "demo",
"_type": "goods",
"_id": "MIHNs2gBBdkQnU_d_r6o",
"_score": 1.6051829,
"_source": {
"title": "华为⼿机",
"price": "2199.00"
}
},
{
"_index": "demo",
"_type": "goods",
"_id": "LYHJs2gBBdkQnU_dt75n",
"_score": 0.2876821,
"_source": {
"title": "⼩⽶⼿机",
"price": "1699.00"
}
}
]
}
}
and关系:会把查询条件进⾏分词,然后进⾏查询,多个词条之间是and的关系查询指令:
GET /shop/_search
{
"query": {
"match": {
"title": {
"query": "⼩⽶⼿机",
"operator": "and"
}
}
}
}
查询结果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.92355466,
"_index": "shop",
"_type": "goods",
"_id": "PYEGtmgBBdkQnU_d4b4m",
"_score": 0.92355466,
"_source": {
"title": "⼩⽶8 全⾯屏游戏智能⼿机 6GB+64GB ⿊⾊全⽹通4G 双卡双待",
"price": 2299,
"stock": 8800
}
}
]
}
}
1.3 词条查询
单值查询(term)
查询指令:
GET /shop/_search
{
"query": {
"term": {
"price": 109.00
}
}
}
查询结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": 1,
"_source": {
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能⽆线路由器四天线智能wifi 稳定穿墙⾼速家⽤路由器", "price": 109,
"stock": 9970
}
}
]
}
}
多值查询(terms)
查询指令:
GET /shop/_search
{
"query": {
"terms": {
"price": [8599.00,109.00]
}
}
}
"took": 15,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "P4EGtmgBBdkQnU_d8b7I",
"_score": 1,
"_source": {
"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英⼨游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz⿊)", "price": 8599,
"stock": 1900
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": 1,
"_source": {
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能⽆线路由器四天线智能wifi 稳定穿墙⾼速家⽤路由器",
"price": 109,
"stock": 9970
}
}
]
}
}
⼆、结果过滤
默认情况下,elasticsearch在搜索的结果中,会把⽂档中保存在 _source 的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加 _source 字段进⾏过滤
2.1 包含字段查询(includes)
查询指令:
GET /shop/_search
{
"query": {
"match_all": {}
},
"_source": {
"includes": ["title","price"]
}
}
查询结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "shop",
"_type": "goods",
发布评论