本文只是写一些常用es命令,这里不用任何客户端,只用 9200/_plugin/head/ 那个端口网页就可以,然后是复合查询。

注意es其实一个idnex只能有一个type,如果一个index做了多个type,坑。。。太多了,首先 where不能用,然后 聚合也不能用。至少在es2.*是这样的,这个坑你可以搜索下“_type Field Not Available for Aggregation #5634”

1. 查询所有
关键字:match_all
等同SQL

select * from item.ic_spu;
{
"query": {
"match_all": {}
}
}

2. 根据某个字段查询 – 默认匹配
关键字:match
等同SQL

select * from item.ic_spu where id = 20001137 ;
{
"query": {
"match": {
"id": "20001137"
}
}
}
{
"query": {
"match": {
"tags.tagId": "30000"
}
}
}

根据文本查询(有分词)

{
"query": {
"match": {
"title": "黄连丸"
}
}
}

这三种查询都会在item_repository/item库中匹配出的结果示例

{
"_index": "item_repository",
"_type": "item",
"_id": "20001137",
"_version": 101,
"found": true,
"_source": {
"seller": {
"name": "好药师官方旗舰店",
"alias": "好药师大药房",
"id": 989810607
},
"modifiedTime": "2015-12-31 11:31:04.000",
"img": "T1E3YlByJT1RCvBVdK|T14ddTBsZT1RCvBVdK|T1jdCTBjxT1RCvBVdK|T1fJETBjWT1RCvBVdK|T1IJdTBjAT1RCvBVdK",
"skus": [{
"hcodePrice": 0,
"origPrice": 1500,
"outerId": "2115",
"id": 20001513,
"price": 1528,
"status": 2
}],
"title": "清胃黄连丸",
"type": 20,
"tags": [{
"code": "webFronts2",
"tagName": "五官科疾病",
"tagId": 30017,
"domain": "mall_sales"
},
{
"code": "o2oFronts",
"tagName": "感冒发烧",
"tagId": 40002,
"domain": "mall_sales"
},
{
"code": "webFronts1",
"tagName": "中西药品",
"tagId": 30000,
"domain": "mall_sales"
},
{
"code": "webFronts3",
"tagName": "口腔/咽喉",
"tagId": 30132,
"domain": "mall_sales"
},
{
"code": "webFronts",
"tagName": "前台根节点",
"tagId": 2,
"domain": "mall_sales"
},
{
"code": "normal",
"tagName": "normal",
"tagId": 20839,
"domain": "mall_sales"
}],
"timeStamp": "2016-12-27 16:00:30.758",
"subTitle": "",
"createTime": "2015-12-31 11:31:04.000",
"indexId": "20001137",
"subType": 15,
"id": 20001137,
"status": 2
}
}

3.根据某个字段查询 – 绝对匹配
关键字:match + phrase
JQuery示例
根据id查询

{
"query": {
"match": {
"id": {
"query": "20001137",
"type": "phrase"
}
}
}
}

注意:类似 id 等数值类型的属性,默认匹配和绝对匹配结果是相同的
对于文本类的属性,没有做分词(not_analyzed)的属性,绝对匹配时用属性完整的值可以匹配出预期结果,例如item库中的tags.domain

如果设置了analyzer,用属性完整的值去做绝对匹配可能会无结果,如item库中的title属性

原因:这种情况下的匹配,是用属性完整的值去匹配索引中被分词后的属性值,自然就可能匹配不上。
例如某spu名称为“感冒颗粒”,那么索引中实际上存储的是分词后的“感冒”和“颗粒”,此时用“感冒颗粒”去匹配,当然无法绝对匹配到“感冒”或者“颗粒”
根据文本查询(针对无analyzer的属性)
等同SQL

select * from item.ic_tag where domain = "mall_sales" ;
{
"query": {
"match": {
"tags.domain": {
"query": "mall_sales",
"type": "phrase"
}
}
}
}

这些查询都会在item_repository/item库中匹配出的结果示例

{
"_index": "item_repository",
"_type": "item",
"_id": "20001137",
"_version": 101,
"found": true,
"_source": {
"seller": {
"name": "好药师官方旗舰店",
"alias": "好药师大药房",
"id": 989810607
},
"modifiedTime": "2015-12-31 11:31:04.000",
"img": "T1E3YlByJT1RCvBVdK|T14ddTBsZT1RCvBVdK|T1jdCTBjxT1RCvBVdK|T1fJETBjWT1RCvBVdK|T1IJdTBjAT1RCvBVdK",
"skus": [{
"hcodePrice": 0,
"origPrice": 1500,
"outerId": "2115",
"id": 20001513,
"price": 1528,
"status": 2
}],
"title": "清胃黄连丸",
"type": 20,
"tags": [{
"code": "webFronts2",
"tagName": "五官科疾病",
"tagId": 30017,
"domain": "mall_sales"
},
{
"code": "o2oFronts",
"tagName": "感冒发烧",
"tagId": 40002,
"domain": "mall_sales"
},
{
"code": "webFronts1",
"tagName": "中西药品",
"tagId": 30000,
"domain": "mall_sales"
},
{
"code": "webFronts3",
"tagName": "口腔/咽喉",
"tagId": 30132,
"domain": "mall_sales"
},
{
"code": "webFronts",
"tagName": "前台根节点",
"tagId": 2,
"domain": "mall_sales"
},
{
"code": "normal",
"tagName": "normal",
"tagId": 20839,
"domain": "mall_sales"
}],
"timeStamp": "2016-12-27 16:00:30.758",
"subTitle": "",
"createTime": "2015-12-31 11:31:04.000",
"indexId": "20001137",
"subType": 15,
"id": 20001137,
"status": 2
}
}

4. 根据某个字段查询 – 通配符匹配
关键字:wildcard
?代表任意1个字符,*代表任意多个字符
JQuery示例
注意:类似 id 等数值类型的属性,也可使用通配符匹配,如同sql中的like查询
对于文本类的属性,纯粹的通配符匹配(即:预期返回以查询keyword做like查询的结果)只适用于没有做分词(not_analyzed)的属性,例如item库中的tags.domain

如果设置了analyzer,通配符匹配也要结合分词去考虑,实际上只能用分词加通配符匹配出预期结果,例如“ *白药* ”可以匹配出“云南白药膏”,但是“ *白药牙* ”就匹配不出。

查看索引分词结果可用 GET /item_repository/_analyze?analyzer=shennong-max&pretty=true&text=(云南白药膏)
根据文本查询(针对无analyzer的属性)
等同SQL

select * from item.ic_tag where domain like "?al%" ;
{
"query": {
"wildcard": {
"tags.domain": "?al*"
}
}
}

这些查询都会在item_repository/item库中匹配出的结果示例
{
"_index": "item_repository",
"_type": "item",
"_id": "20001137",
"_version": 101,
"found": true,
"_source": {
"seller": {
"name": "好药师官方旗舰店",
"alias": "好药师大药房",
"id": 989810607
},
"modifiedTime": "2015-12-31 11:31:04.000",
"img": "T1E3YlByJT1RCvBVdK|T14ddTBsZT1RCvBVdK|T1jdCTBjxT1RCvBVdK|T1fJETBjWT1RCvBVdK|T1IJdTBjAT1RCvBVdK",
"skus": [{
"hcodePrice": 0,
"origPrice": 1500,
"outerId": "2115",
"id": 20001513,
"price": 1528,
"status": 2
}],
"title": "清胃黄连丸",
"type": 20,
"tags": [{
"code": "webFronts2",
"tagName": "五官科疾病",
"tagId": 30017,
"domain": "mall_sales"
},
{
"code": "o2oFronts",
"tagName": "感冒发烧",
"tagId": 40002,
"domain": "mall_sales"
},
{
"code": "webFronts1",
"tagName": "中西药品",
"tagId": 30000,
"domain": "mall_sales"
},
{
"code": "webFronts3",
"tagName": "口腔/咽喉",
"tagId": 30132,
"domain": "mall_sales"
},
{
"code": "webFronts",
"tagName": "前台根节点",
"tagId": 2,
"domain": "mall_sales"
},
{
"code": "normal",
"tagName": "normal",
"tagId": 20839,
"domain": "mall_sales"
}],
"timeStamp": "2016-12-27 16:00:30.758",
"subTitle": "",
"createTime": "2015-12-31 11:31:04.000",
"indexId": "20001137",
"subType": 15,
"id": 20001137,
"status": 2
}
}
5.根据某个字段查询 – 前缀匹配
关键字:prefix
JQuery示例
注意:可以把前缀匹配理解为一种特殊的通配符匹配
对于文本类的属性,前缀匹配和通配符匹配一样需要考虑analyzed的影响
根据文本查询(针对无analyzer的属性)
等同SQL

select * from item.ic_tag where domain like "mal%" ;
{
"query": {
"prefix": {
"tags.domain": "mal"
}
}
}

6. 根据某个字段查询 – 范围匹配
关键字:range
JQuery示例
等同SQL
select * from item.ic_spu where id >= 20001137 and id <= 20001140;
注意:范围匹配对数值类型和时间类型的属性都适用

{
"query": {
"range": {
"id": {
"include_lower": true,
"from": "20001137",
"include_upper": true,
"to": "20001140"
}
}
}
}
{
"query": {
"range": {
"modifiedTime": {
"include_lower": false,
"from": "2015-12-31 11:31:04.000",
"include_upper": true,
"to": "2015-12-31 11:31:15.000"
}
}
}
}
7. AND 查询
关键字:bool + must
JQuery示例

{
"query": {
"bool": {
"must": [
{
"match": {
"type": 20
}
},
{
"match": {
"subType": 15
}
},
{
"query": {
"wildcard": {
"title": "*感冒*"
}
}
}
]
}
}
}

这些查询会在item_repository/item库中匹配出的结果示例
{
"_index": "item_repository",
"_type": "item",
"_id": "20001122",
"_version": 102,
"found": true,
"_source": {
"seller": {
"name": "好药师官方旗舰店",
"alias": "好药师大药房",
"id": 989810607
},
"modifiedTime": "2015-12-31 11:31:03.000",
"img": "T1.BYlB4LT1RCvBVdK|T1rdDTBjWT1RCvBVdK|T1FdKTBsxT1RCvBVdK|T17dKTBjDT1RCvBVdK|T1vOATBjLT1RCvBVdK",
"skus": [{
"hcodePrice": 0,
"origPrice": 1100,
"outerId": "2099",
"id": 20008366,
"price": 1078,
"status": 2
}],
"title": "感冒软胶囊",
"type": 20,
"tags": [{
"code": "o2oFronts",
"tagName": "感冒发烧",
"tagId": 40002,
"domain": "mall_sales"
},
{
"code": "webFronts",
"tagName": "前台根节点",
"tagId": 2,
"domain": "mall_sales"
},
{
"code": "webFronts1",
"tagName": "中西药品",
"tagId": 30000,
"domain": "mall_sales"
},
{
"code": "normal",
"tagName": "normal",
"tagId": 20839,
"domain": "mall_sales"
},
{
"code": "webFronts3",
"tagName": "感冒发烧",
"tagId": 30190,
"domain": "mall_sales"
},
{
"code": "webFronts2",
"tagName": "感冒咳嗽",
"tagId": 30037,
"domain": "mall_sales"
}],
"timeStamp": "2016-12-27 18:00:31.438",
"subTitle": "",
"createTime": "2015-12-31 11:31:03.000",
"indexId": "20001122",
"subType": 15,
"id": 20001122,
"status": 2
}
}
8. OR 查询
关键字:bool + should + minimum_should_match
JQuery示例
注意:"minimum_should_match": 1 可以不写,表示should结构体中至少得满足一个条件,即通常意义上的or,这也是默认值。
当然,对于should结构体中包括>2个条件的情况,可以设m来构造必须满足其中m个条件的广义OR查询
例如,下面的JQuery,如果设成2 其实也等同于AND查询

{
"query": {
"bool": {
"should": [
{
"match": {
"type": 20
}
},
{
"match": {
"tags.domain": {
"query": "mall_admin",
"type": "phrase"
}
}
}
],
"minimum_should_match": 1
}
}

这些查询都会在item_repository/item库中匹配出的结果示例
{
"_index": "item_repository",
"_type": "item",
"_id": "20011265",
"_version": 73,
"found": true,
"_source": {
"modifiedTime": "2016-01-20 17:22:53.000",
"img": "T1m5xXByVT1RXrhCrK",
"skus": [{
"hcodePrice": 0,
"origPrice": 3000,
"id": 911911888303103,
"price": 3000,
"status": 2
}],
"title": "名医商品2436560600",
"type": 35,
"tags": [{
"code": "treeBackground2",
"tagName": "名医咨询",
"tagId": 13,
"domain": "mall_admin"
},
{
"code": "treeBackground",
"tagName": "后台节点根",
"tagId": 880154,
"domain": "mall_admin"
},
{
"code": "treeBackground1",
"tagName": "旧商品类目",
"tagId": 1,
"domain": "mall_admin"
},
{
"code": "treeBackground3",
"tagName": "名医",
"tagId": 14,
"domain": "mall_admin"
}],
"timeStamp": "2016-12-27 16:00:49.954",
"subTitle": "名医咨询",
"createTime": "2016-01-20 17:22:53.000",
"indexId": "20011265",
"subType": 0,
"id": 20011265,
"status": 2
}
}
9. NOT 查询
关键字:bool + must_not
JQuery示例
根据id查询
等同SQL

select * from item.ic_spu where type != 20 ;
{
"query": {
"bool": {
"must_not": [
{
"match": {
"type": 20
}
}
]
}
}
}

根据文本查询
等同SQL

select * from item.ic_tag where domain <> 'mall_sales' ;
{
"query": {
"bool": {
"must_not": [
{
"match": {
"tags.domain": {
"query": "mall_sales",
"type": "phrase"
}
}
}
]
}
}
}

这些查询会在item_repository/item库中匹配出的结果示例
{
"_index": "item_repository",
"_type": "item",
"_id": "20011265",
"_version": 73,
"found": true,
"_source": {
"modifiedTime": "2016-01-20 17:22:53.000",
"img": "T1m5xXByVT1RXrhCrK",
"skus": [{
"hcodePrice": 0,
"origPrice": 3000,
"id": 911911888303103,
"price": 3000,
"status": 2
}],
"title": "名医商品2436560600",
"type": 35,
"tags": [{
"code": "treeBackground2",
"tagName": "名医咨询",
"tagId": 13,
"domain": "mall_admin"
},
{
"code": "treeBackground",
"tagName": "后台节点根",
"tagId": 880154,
"domain": "mall_admin"
},
{
"code": "treeBackground1",
"tagName": "旧商品类目",
"tagId": 1,
"domain": "mall_admin"
},
{
"code": "treeBackground3",
"tagName": "名医",
"tagId": 14,
"domain": "mall_admin"
}],
"timeStamp": "2016-12-27 16:00:49.954",
"subTitle": "名医咨询",
"createTime": "2016-01-20 17:22:53.000",
"indexId": "20011265",
"subType": 0,
"id": 20011265,
"status": 2
}
}
10. 分页与排序
关键字:from + size + sort
JQuery示例
根据id查询
等同SQL

select * from item.ic_spu order by gmt_modified limit 1,5;

{
"size": "5",
"from": "1",
"sort": [
{
"modifiedTime": "asc"
}
]
}

11.展示属性限制
关键字:fields
JQuery示例
等同SQL

select id,title from item.ic_spu ;
{
"fields": [
"id",
"title"]
}

最后,用一个案例做下总结:
如果想要查 type=20(药), subType !=10(非处方药), status=2(上架状态), title中包含"感冒"(商品名中包含“感冒”)或者 tags.tagName中包含"感冒"(标签名中包含“感冒”)的所有spu,以modifiedTime(spu修改时间)一维倒排+ id 二维顺排(modifiedTime相同的情况下用id从小到大排序),取前10个,返回结果中只需要spu的id,title,modifiedTime,seller.name(售卖商户名),那么对应的JQuery如下:

{
"size": "10",
"from": "0",
"sort": [
{
"modifiedTime": "desc"
},
{
"id": "asc"
}
],
"query": {
"bool": {
"must": [
{
"match": {
"type": 20
}
},
{
"bool": {
"must_not": [
{
"match": {
"subType": 10
}
}
]
}
},
{
"match": {
"status": 2
}
},
{
"bool": {
"should": [
{
"match": {
"tags.tagName": "感冒"
}
},
{
"match": {
"title": "感冒"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"fields": [
"id",
"title",
"seller.name",
"modifiedTime"
]
}

此案例也可以这么写
因为should查询中匹配的keyword是相同的,只是field不同,所以可以改成

{
"size": "10",
"from": "0",
"sort": [
{
"modifiedTime": "desc"
},
{
"id": "asc"
}
],
"query": {
"bool": {
"must": [
{
"match": {
"type": 20
}
},
{
"bool": {
"must_not": [
{
"match": {
"subType": 10
}
}
]
}
},
{
"match": {
"status": 2
}
},
{
"multi_match": {
"query": "感冒",
"fields": [
"title",
"tags.tagName"
]
}
}
]
}
},
"fields": [
"id",
"title",
"seller.name",
"modifiedTime"
]
}

es查询命令备份(只需要网页9200/_plugin/head/就可以访问)的更多相关文章

  1. es快照和备份

    注册前要注意配置文件加上 path.repo: ["/data/es_backup"] 然后重启es 不然会报错doesn't match any of the locations ...

  2. ELK查询命令详解

    目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...

  3. ELK查询命令详解总结

    目录 ELK查询命令详解 倒排索引 倒排索引原理 分词器介绍及内置分词器 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Map ...

  4. Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图   今天接着昨天的说   索引有4种:      普通 索引 :ind ...

  5. 记一次ES查询数据突然变为空的问题

    基本环境 elasticsearch版本:6.3.1 客户端环境:kibana 6.3.4.Java8应用程序模块. 其中kibana主要用于数据查询诊断和查阅日志,Java8为主要的客户端,数据插入 ...

  6. es 查询更新操作

    # es 查询更新操作# _*_ coding: utf-8 _*_ import time import datetime import pymysql from elasticsearch imp ...

  7. 【Linux高频命令专题(8)】五大查询命令

    find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...

  8. DBA查询命令积累——不断更新

    原文:DBA查询命令积累--不断更新 一.服务器配置: 1.兼容级别:兼容级别只影响指定数据库中的行为,而不会影响整个服务器上的行为. 1.1.查看数据库兼容级别及更改兼容级别: SELECT com ...

  9. MySQL常用的查询命令

    MySQL常用的查询命令 author: headsen chen   2017-10-19  10:15:25 个人原创.转载请注明作者,出处,否则依法追究法律责任 1,查询现在的时间:mysql& ...

随机推荐

  1. MFC 如何创建浏览文件夹的对话框

    如何创建浏览文件夹的对话框 如何创建浏览文件夹的对话框 CString CXXXXDlg::GetOpenfolderPath() { BROWSEINFO bi; ZeroMemory(&b ...

  2. android include进来的组件 调用其子元素

    include标签包裹着一个可复用的布局: <include layout="@layout/footer_detail" android:id="@+id/foo ...

  3. 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】

    昨天有同学问我问题,他告诉我他的Action中的一个属性明明提供了get/set方法,但是在方法中却获取不到表单中传递过来的值.代码如下(简化后的代码) public class UserAction ...

  4. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  5. CSS布局经典—圣杯布局与双飞翼布局

    在我之前的博客网页整体布局完全剖析-剖完你不进来看一下么?中总结单列.两列.三列固宽与变宽布局,我还以为已经囊括了所有经典的网页布局方法了呢,当然除了CSS3的弹性盒模型没有涉及到,现在看来确实是自己 ...

  6. 蓝桥杯算法提高 P1001(大数乘法)

      算法提高 P1001   时间限制:1.0s   内存限制:256.0MB   当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法. 具体 ...

  7. 【Mybatis架构】Mapper映射文件中的#{}与${}

    前言 还记得当初从北京回来的时候,跟着倪文杰师姐做JavaITOO的一卡通模块,我亲姐贾梦洁带着我一块做,期间,我遇到了一个特别奇葩的问题,就死我要实现Mybatis的模糊查询,根据当时亲姐教给我方法 ...

  8. Zookeeper操作

    Zookeeper操作 注意搭建: 1.集群规模不小于3个节点 2.服务器之间系统时间要保持一致 1.搭建步骤: 1.解压安装包 2.设置zookeeper环境变量 3.修改配置文件————zoo.c ...

  9. VS低版本打开高版本解决方案(如08打开10、12、13版本vs编译的项目)

    一.vs2005打开vs2008编译的项目:1.用记事本打开sln文件,将: Microsoft Visual Studio Solution File, Format Version 10.00 # ...

  10. 用vue2 +vue-router2 + es6 +webpack 高仿饿了么app(干货满满)

    #高仿饿了么app商家详情 (vue2 +vue-router2 + es6 +webpack )   ##demo [demo 地址](http://liangxiaojuan.github.io/ ...