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. c++冒号作用

    转自http://www.360doc.com/content/13/0605/11/3373961_290615318.shtml 1.冒号(:)用法 (1)表示机构内位域的定义(即该变量占几个bi ...

  2. javaScript+html5实现图片拖拽

    源码: <!DOCTYPE html><html><head> <meta charset="utf-8"/> <title& ...

  3. mysql查询语句中自定义变量(转)

    转:http://blog.sina.com.cn/s/blog_1512521570102wrfl.htmlselect cost,@a:=@a+1 from testone,(select @a: ...

  4. AltiumDesigner 查找相似对象

    同类器件的集体选中.集体选中的方法是:先选中一个标识符,右击在选项表中选择Find Similar Objects,然后就会出现一个对话框,在这个对话框中,有一些any项,根据自己的需要把一些any改 ...

  5. c#: 任务栏进度显示(TaskbarManager)

    Win7及以上系统支持任务栏进度条,为有进度类应用显示进度于任务栏,甚为方便. 以c#之WinForm实现其,大多采用Windows API Code Pack这个方案,加多引用,比较繁琐,而我总也打 ...

  6. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  7. [原]CentOS7.2部署KVM虚拟机

    前段时间学习了关于PostGis.OSM数据以及Mapnik相关内容,接下来将利用假期重点学习PostgreSQL-XL和瓦片服务器集群技术,因此先把环境搭好.计划采用KVM来充分利用家里不太宽裕的“ ...

  8. c#npoi 报错Cannot get a numeric value from a text cell 的解决

    一般是因为cell里边的值为数字导致,有时变成文本格式还是解决不了这个问题. 下边的代码是c# 改变设置cell类型的方法 是用这个参数 CellType.String Row.GetCell((in ...

  9. RNA-seq数据综合分析教程 AKAP95

    https://blog.csdn.net/l_yivs?t=1 RNA-seq数据综合分析教程 2 4,055 A+ 所属分类:Transcriptomics   收  藏 2     RNA-se ...

  10. ArcGIS for JS 离线部署

    本文以arcgis_js_v36_api为例,且安装的是IIS Web服务器 1.下载最新的ArcGIS for JS api 包,可在Esri中国社区或者Esri官网下载 2.下载后解压 3.将解压 ...