es的六种搜索方式

  1. query string search
GET /ecommerce/product/_search //查询所有数据

{
"took": 4,//耗费几毫秒
"timed_out": false,//是否超时
"_shards": {//数据拆分成5个分片,对所有请求都会打到所有primary shared(或者是它的某个replica shared也可以)
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,//条数
"max_score": 1,//查询匹配度
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "jiajieshi meibai",
"price": 30,
"producer": "jiajieshi producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
]
}
}
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
  1. query DSL

    • DSL:Domain Specified Language:特定领域的语言
    • http request body:请求体,用json格式构建查询语法
    GET /ecommerce/product/_search
    {
    "query": {
    "match": {
    "name": "yagao" //查询包含单词
    }
    },
    "_source": ["name","price"],//不写查询所有字段
    "sort": [
    {
    "price": {
    "order": "desc"//倒序排序
    }
    }
    ],
    "from": 0,//分页
    "size": 1
    }
  2. query filter

GET /ecommerce/product/_search
{
"query": {
"bool": {//多个条件
"must": [
{
"match": {
"name": "yagao"
}
}
],
"filter": {//过滤条件
"range": {
"price": {
"gte": 10,
"lte": 40
}
}
}
}
}
}
  1. full-text search
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "lishi producer"//会查包含这两个单词的所有数据
}
}
} {
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.51623213,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.51623213,//匹配度最高
"_source": {
"name": "lishi yagao",
"desc": "lishi meibai",
"price": 50,
"producer": "lishi producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "jiaqiangban gaolujie yagao2",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.1805489,
"_source": {
"name": "jiajieshi yagao",
"desc": "jiajieshi meibai",
"price": 40,
"producer": "jiajieshi producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.14638957,
"_source": {
"name": "special yaogao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
}
]
}
}
  1. phrase search

    必须包含一模一样的串,才会返回(包含短语的意思)
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.70293105,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.70293105,
"_source": {
"name": "special yaogao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
}
]
}
}
  1. highlight search

    • 查询到的结果高亮
    GET /ecommerce/product/_search
    {
    "query": {
    "match_phrase": {
    "producer": "gaolujie producer"
    }
    },
    "highlight": {
    "fields": {
    "producer": {}
    }
    }
    }
    {
    "took": 18,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 1,
    "max_score": 0.51623213,
    "hits": [
    {
    "_index": "ecommerce",
    "_type": "product",
    "_id": "1",
    "_score": 0.51623213,
    "_source": {
    "name": "jiaqiangban gaolujie yagao2",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
    "meibai",
    "fangzhu"
    ]
    },
    "highlight": {
    "producer": [
    "<em>gaolujie</em> <em>producer</em>"
    ]
    }
    }
    ]
    }
    }

计算每个tag下的商品数量

  • 将文本field的fielddata属性设置为true
PUT /ecommerce/_mapping/product
{
"properties": {
"tags":{
"type": "text",
"fielddata": true
}
}
}
  • 再运行下面才能查出来
GET /ecommerce/product/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
} "aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 3
},
{
"key": "meibai",
"doc_count": 3
},
{
"key": "suibian",
"doc_count": 1
}
]
}
}

对名称中包含yagao的商品,计算每个tag下的商品数量

GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}

先分组,再计算每组的平均值,计算每个tag下的商品的平均价格

GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

对上述结果按平均价格排序

GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格

GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{
"from": 20,
"to": 40
},
{
"from": 40,
"to": 60
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}

ElasticSearch核心知识总结(一)es的六种搜索方式和数据分析的更多相关文章

  1. ElasticSearch核心知识总结(二)

    如何超出扩容极限,以及如何提升容错性 primary&replica自动负载均衡,6个shared,3个primary,3个replica,随着机器扩容,会被均衡分配到多台机器上 6个shar ...

  2. ElasticSearch核心知识 -- 索引过程

    1.索引过程图解: api向集群发送索引请求,集群会使用负载均衡节点来处理该请求,如果没有单独的负载均衡点,master节点会充当负载均衡点的角色. 负载均衡节点根据routing参数来计算要将该索引 ...

  3. elasticsearch核心知识梳理

    https://blog.csdn.net/laoyang360/article/details/52244917

  4. Elasticsearch核心知识大纲脑图

  5. 十九种Elasticsearch字符串搜索方式终极介绍

    前言 刚开始接触Elasticsearch的时候被Elasticsearch的搜索功能搞得晕头转向,每次想在Kibana里面查询某个字段的时候,查出来的结果经常不是自己想要的,然而又不知道问题出在了哪 ...

  6. ElasticSearch入门篇Ⅰ --- ES核心知识概括

    C01.什么是Elasticsearch 1.什么是搜索 垂直搜索(站内搜索) 互联网的搜索:电商网站,招聘网站,各种app IT系统的搜索:OA软件,办公自动化软件,会议管理,员工管理,后台管理系 ...

  7. Elasticsearch必知必会的干货知识一:ES索引文档的CRUD

    ​ 若在传统DBMS 关系型数据库中查询海量数据,特别是模糊查询,一般我们都是使用like %查询的值%,但这样会导致无法应用索引,从而形成全表扫描效率低下,即使是在有索引的字段精确值查找,面对海量数 ...

  8. Elasticsearch必知必会的干货知识二:ES索引操作技巧

    该系列上一篇文章<Elasticsearch必知必会的干货知识一:ES索引文档的CRUD> 讲了如何进行index的增删改查,本篇则侧重讲解说明如何对index进行创建.更改.迁移.查询配 ...

  9. Elasticsearch 基础知识要点与性能监控

    本文的来源是我翻译国外的一篇技术博客,感谢原作者Emily Chang,原文地址通过如下的知识,我们能大致学到关于ES的一些基本知识,进而对elasticsearch的性能进行监控和调优 注意elas ...

随机推荐

  1. [Ting's笔记Day2]在Github用Jekyll创建自己的blog

    昨天工程师在我们共同的群组分享他的blog,他提到是使用Jekyll(一个简单静态blog网站生成器)架在github上的. 于是好奇的我决定照着关键字来搜寻一下,如法炮制做一个出来. 也可以放一份到 ...

  2. CSS表单3 光标样式 (每个位置鼠标放上去的样式不同)

    <!DOCTYPE html> <html>     <head>         <title>单选按钮对齐</title>        ...

  3. JQEUERY案例

    案例效果: 点击显示全部奶粉品牌前: 点击后: 源码: <!DOCTYPE html><html><head> <meta charset="utf ...

  4. Django_Form验证(二),ajax验证

    还是一个简单的html提交页面,ajax提交就不需要form表单了: <p><input id="a" type="text" name=&q ...

  5. python学习笔记(四)- 常用的字符串的方法

    一.常用的字符串方法(一):(字符串是不能被修改的) 1)a.strip()   #默认去掉字符串两边的空格和换行符 a = ' 字符串 \n\n ' c = a.strip() a.lstrip() ...

  6. wiredtiger--初学数据恢复

    启动mongodb是failed,日志如下 1.解压wirdtiger包 tar -vxf wiredtiger-3.1.0.tar.bz2 -C /home/wiredtiger/ 2.安装snap ...

  7. 大数据spark学习第一周Scala语言基础

    Scala简单介绍 Scala(Scala Language的简称)语言是一种能够执行于JVM和.Net平台之上的通用编程语言.既可用于大规模应用程序开发,也可用于脚本编程,它由由Martin Ode ...

  8. 做u盘启动重装系统 进winPE 出现 cdboot:couldn't find ntldr 解决办法

    公司的QA本来用的ubuntu系统 觉得不是很好使 就找我重装win10系统  之前有重装过系统 就信心满满的答应了 我拿出U盘 把U盘格式化了下 去下载了个雨林木风的win10 系统(ISO文件) ...

  9. error link 2019 waveout

    winmm.lib的影响 在做音频播放的时候使用到了win系统音频函数waveout; 但是报错: error link 2019 无法解析外部符号:waveoutGetnumDevice /clos ...

  10. zabbix自定义监控

    有的时候zabbix提供的监控项目,不能满足我们生产环境下的监控需求,此时我们就要按照zabbix的规范自定义监控项目,达到监控的目的 zabbix_get:模拟zabbix_server和agent ...