数据准备:

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. idea 多项目部署JRebel

    碰到个问题,Jrebel热部署Idea,两个项目一个可以热部署,一个不行.刚开始发现是其中一个项目没有添加JRebel,于是加上 然而发现还是不行,继续google, 在一篇文章里面发现如下的内容: ...

  2. HDFS元数据管理机制

    元数据管理概述 HDFS元数据,按类型分,主要包括以下几个部分: 1.文件.目录自身的属性信息,例如文件名,目录名,修改信息等. 2.文件记录的信息的存储相关的信息,例如存储块信息,分块情况,副本个数 ...

  3. 四、CentOS 安装mariadb——Linux学习笔记

    A)安装及配置 下载mariadb: yum -y install mariadb-server mariadb 开启mariadb服务: systemctl start mariadb.servic ...

  4. Python学习---Python下[字符串]的学习

    字符串[不可变] 重点方法: in count() center(50,'*') startswith('h') endwith('h') find('r') : 找不到返回 -1 index('r' ...

  5. 设计模式:仲裁者(Mediator)模式

    设计模式:仲裁者(Mediator)模式 一.前言     Mediator模式又称为仲裁者模式或者中介者模式,所起的作用是仲裁和中介,帮助其它类之间进行交流.在仲裁者模式之中,我们要明确两个概念,那 ...

  6. Java集合工具类

    import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map ...

  7. selenium借助send_keys实现上传(以网易邮箱为例)

    #code:utf-8from selenium import webdriverimport time#网易163邮箱dr = webdriver.Firefox()file_path = 'htt ...

  8. July 18th 2017 Week 29th Tuesday

    My heart is stronger now that you are in it. 我的心里有了你,从此变得更强大. You will no longer feel lonely if ther ...

  9. July 16th 2017 Week 29th Sunday

    Opportunities are like sunrises, if you wait too long, you miss them. 机会如同日出,等得太久就会错过. Indecision is ...

  10. CopyOnWriteArrayList对比ArrayList

    ArrayList非线程安全,CopyOnWriteArrayList线程安全 ArrayList添加元素的时候内部会预先分配存储空间,CopyOnWriteArrayList每次添加元素都会重新co ...