数据准备:

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,
"successful": 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": {
"title": {
"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,
"hits": [
{
"_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",
"_id": "P4EGtmgBBdkQnU_d8b7I",
"_score": 1,
"_source": {
"price": 8599,
"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)"
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": 1,
"_source": {
"price": 109,
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器"
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PYEGtmgBBdkQnU_d4b4m",
"_score": 1,
"_source": {
"price": 2299,
"title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待"
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PoEGtmgBBdkQnU_d6b4Q",
"_score": 1,
"_source": {
"price": 4999,
"title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机"
}
}
]
}
}

2.2 排除字段查询(excludes)

查询指令

GET /shop/_search
{
"query": {
"match_all": {}
},
"_source": {
"excludes": "price"
}
}

查询结果:

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 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黑)",
"stock": 1900
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": 1,
"_source": {
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
"stock": 9970
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PYEGtmgBBdkQnU_d4b4m",
"_score": 1,
"_source": {
"title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
"stock": 8800
}
},
{
"_index": "shop",
"_type": "goods",
"_id": "PoEGtmgBBdkQnU_d6b4Q",
"_score": 1,
"_source": {
"title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
"stock": 5600
}
}
]
}
}

三、高级查询

3.1 范围查询(range)

查询找出那些落在指定区间内的数字或者时间

查询指令:

GET /shop/_search
{
"query": {
"range": {
"price": {
"gte": 2000,
"lte": 5000
}
}
}
}

查询结果:

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_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
}
}
]
}
}

range查询允许以下操作符

3.2 布尔查询(bool)

bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合

查询指令:

GET /shop/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "笔记本"
}
},
{
"term": {
"price": 109.00
}
}
]
}
}
}

查询结果:

{
"took": 15,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.9646256,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "P4EGtmgBBdkQnU_d8b7I",
"_score": 1.9646256,
"_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
}
}
]
}
}

四、过滤查询

所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,

那么就不要把过滤条件作为查询条件来用,而是使用filter方式。

查询指令:

GET /shop/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手机"
}
}
],
"filter": {
"range": {
"price": {
"gt": 3000
}
}
}
}
}
}

查询结果:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.17329858,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "PoEGtmgBBdkQnU_d6b4Q",
"_score": 0.17329858,
"_source": {
"title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
"price": 4999,
"stock": 5600
}
}
]
}
}

注意:filter中还可以再次进行bool组合条件过滤。 

五、排序查询

sort 可以让我们按照不同的字段进行排序,并且通过order指定排序的方式。

查询指令:

GET /shop/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}

查询结果:

{
"took": 55,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "shop",
"_type": "goods",
"_id": "P4EGtmgBBdkQnU_d8b7I",
"_score": null,
"_source": {
"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
"price": 8599,
"stock": 1900
},
"sort": [
8599
]
},
{
"_index": "shop",
"_type": "goods",
"_id": "PoEGtmgBBdkQnU_d6b4Q",
"_score": null,
"_source": {
"title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
"price": 4999,
"stock": 5600
},
"sort": [
4999
]
},
{
"_index": "shop",
"_type": "goods",
"_id": "PYEGtmgBBdkQnU_d4b4m",
"_score": null,
"_source": {
"title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
"price": 2299,
"stock": 8800
},
"sort": [
2299
]
},
{
"_index": "shop",
"_type": "goods",
"_id": "QIEGtmgBBdkQnU_d-r6T",
"_score": null,
"_source": {
"title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
"price": 109,
"stock": 9970
},
"sort": [
109
]
}
]
}
}

Elasticsearch 数据查询的更多相关文章

  1. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(1)

    本文描述了一个系统,功能是评价和抽象地理围栏(Geo-fencing),以及监控和分析核心地理围栏中业务的表现. 技术栈:Spring-JQuery-百度地图WEB SDK 存储:Hive-Elast ...

  2. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(2)-查询实现

    在上一篇博客中,我们准备好了数据.现在数据已经以我们需要的格式,存放在Elasticsearch中了. 本文讲述如何在Elasticsearch中进行空间GEO查询和聚合查询,以及如何准备ajax接口 ...

  3. Elasticsearch Kibana查询语法

    Elasticsearch Kibana查询语法 2018年06月03日 23:52:30 wangpei1949 阅读数:3992   Elasticsearch Kibana Discover的搜 ...

  4. Oracle和Elasticsearch数据同步

    Python编写Oracle和Elasticsearch数据同步脚本 标签: elasticsearchoraclecx_Oraclepython数据同步    Python知识库 一.版本 Pyth ...

  5. [转] [Elasticsearch] 数据建模 - 处理关联关系(1)

    [Elasticsearch] 数据建模 - 处理关联关系(1) 标签: 建模elasticsearch搜索搜索引擎 2015-08-16 23:55 6958人阅读 评论(0) 收藏 举报 分类: ...

  6. ElasticSearch—分页查询

    ElasticSearch查询—分页查询详解 Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.那么,如何实现分页查询呢? 按照一般的查询流程来说,如 ...

  7. 服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana

    服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana https://www.cnblogs.com/xishuai/p/elk- ...

  8. ElasticSearch高级查询

    ElasticSearch高级查询 https://www.imooc.com/video/15759/0 ElasticSearch查询 1,子条件查询:特定字段查询所指特定值 1.1query c ...

  9. 【原创】MapReduce备份Elasticsearch数据到HDFS(JAVA)

    一.环境:JAVA8,Elasticsearch-5.6.2,Hadoop-2.8.1二.实现功能:mapreduce读elasticsearch数据.输出parquet文件.多输出路径三.主要依赖 ...

随机推荐

  1. QT5.3.1 Quick 开发(二) 项目类型的选择

    作为一个转行QT开发的新手,面对基于QML的开发时候 看到很多的项目类型感到很困惑,不知道应该怎么选择.如图: 经过研究发现QT widgets Application.QtQuick Applica ...

  2. android测试Code

    <!--android:layout_alignParentTop="true"--><com.koooke.platform.View.CenterImage ...

  3. gitattributes中的filter

    .gitattributes文件就是一个简单的text文本文件,它的作用是gives attributes to pathnames. 该文件中的一些配置可以为某些特定目录或者文件来设置,这样Git就 ...

  4. WAKE-WIN10-SOFT-环境

    操作系统名称 Microsoft Windows 10 专业版版本 10.0.14393 版本 14393其他操作系统描述 没有资料操作系统制造商 Microsoft Corporation系统名称 ...

  5. springMVC框架下返回json格式的对象,list,map

    原文地址:http://liuzidong.iteye.com/blog/1069343 注意这个例子要使用jQuery,但是jquery文件属于静态的资源文件,所以要在springMVC中设置静态资 ...

  6. 【BZOJ3757】苹果树(树上莫队)

    点此看题面 大致题意: 每次问你树上两点之间路径中有多少种颜色,每次询问可能会将一种颜色\(a\)看成\(b\). 树上莫队 这题是一道树上莫队板子题. 毕竟求区间中有多少种不同的数是莫队算法的经典应 ...

  7. PHP中__get()和__set()的用法实例详

    刚刚看到一个对我有用的文章,我就把它摘抄下来了.                                                                        php面 ...

  8. 第五章.MyBatis高级映射

    5.1   新建数据库准备 CREATE TABLE `finacial_products` ( `product_id` ) NOT NULL AUTO_INCREMENT, `name` ) NO ...

  9. 全新释放 | RealSight APM, 让客户的极致数字体验成为可能

    根据专业评测机构 downdetector.com 统计,2018年,Facebook 系统全年宕机 200 次,Youtube 宕机  140 次,Google 宕机 100 次.每次宕机损失至少 ...

  10. 调用save()方法,页面显示保存成功,但是数据库中没有值的原因

    在DAO层调用save()方法,页面上显示成功,但是在数据库中查找时发现数据没有保存到数据库中的原因可能是: 1.Service层中是否在调用DAO层中的save()方法之前添加注解@Transact ...