简介:选型原理
- 除了搜索文本之外还需要他来处理分析查询
- 需要分布式索引, 对于需要良好可伸缩性和以及性能分布式环境, Elasticsearch是更好的选择
- 监控和指标, Elasticsearch暴露了更多的关键指标
win启动方法
首先去官网下载压缩包
解压压缩包这里可以看到Jdk文件夹这是因为es是基于Java开发的的, 当然一般来说是使用我们自己的Java环境而不使用内置的
这里注意如果使用的是本地环境的话8.0以后是需要JDK17版本的, 另外需要添加环境变量.
官方文档:
Java (JVM) Versionedit Elasticsearch is built using Java, and includes a bundled version of OpenJDK from the JDK maintainers (GPLv2+CE) within each distribution. The bundled JVM is the recommended JVM and is located within the jdk directory of the Elasticsearch home directory. To use your own version of Java, set the ES_JAVA_HOME environment variable. If you must use a version of Java that is different from the bundled JVM, we recommend using a supported LTS version of Java. Elasticsearch will refuse to start if a known-bad version of Java is used. The bundled JVM directory may be removed when using your own JVM.
翻译(机器翻译):
Java (JVM) Elasticsearch是使用Java构建的,并且在每个发行版中都包含了来自JDK维护者的OpenJDK捆绑版本(GPLv2+CE)。捆绑的JVM是推荐的JVM,位于Elasticsearch主目录的jdk目录中。要使用您自己的Java版本,请设置ES_JAVA_HOME环境变量。如果必须使用与捆绑的JVM不同的Java版本,我们建议使用受支持的LTS版本的Java。如果使用已知错误的Java版本,Elasticsearch将拒绝启动。当使用您自己的JVM时,捆绑的JVM目录可能会被删除。
进入bin目录双击elasticsearch.bat文件
es启动
其中注意9300端口为Elasticsearch集群间组件的通信端口, 9200端口为浏览器访问http协议Resful端口
打开浏览器输入地址http://localhost:9200 ,测试结果
启动问题解决方案
bin 启动文件
config 配置文件
log4j2 日志配置文件
Jvm.options java默认虚拟机相关设置, 如果修改需要创建jvm.options.d文件夹然后在内部创建option后缀的文件进行修改jvm配置
elasticsearch.yml elasticsearch的配置文件, 跨域配置
lib 相关jar包
moudles 功能模块
plugins 插件
Elasticsearch是面向文档型数据库, 一条数据在这里就是一个文档. 为了方便理解和MySQL进行一下对比
- 一个索引就是拥有几分相似特征的文档的集合. 比如说, 你可以有一个客户数据的索引, 另一个产品目录的索引, 还有一个订单数据的索引. 一个索引由一个名字来表示(必须全部小写字母), 并且当我们要对这个索引中的文档进行索引, 搜索, 更新和删除的时候, 都要使用到这个名字, 在一个集群中, 可以定义任意多的索引
- 能搜索的数据必须索引, 这样的好处是可以提高查询速度, 比如, 新华字典前面的目录就是索引的意思, 目录可以提高查询速度
- Elasticsearch索引的精髓: 一切设计都是为了提高搜索的性能
在一个索引中, 你可以定义一种或多种类型
一个类型是你的索引的一个逻辑上的分类/ 分区, 其语义完全由你来定. 通常, 会为具有一组共同字段的文档定义一个类型. 不同的版本, 类型发生了不同的变化
- 一个文档是一个可被索引的基础信息单元, 也就是一条数据
- eg: 可以拥有某一个产品的文档, 某一个人的文档, 当然, 也可以拥有某个订单的一个文档. 文档以JSON(Javascript Object Notation)格式来表示, 而JSON是一个到处存在的互联网数据交互格式
- 在一个index/type里面, 你可以存储任意多的文档
相当于数据库的字段, 对文档数据根据不同属性进行的分类标识
mapping是处理数据的方式和规则方面做一些限制, 如: 某个字段的数据类型, 默认值, 分析器, 是否被索引等等. 这些都是映射里面可以设置的, 其他就是处理ES里面数据的一些使用规则设置也叫映射, 按着最优规则处理数据对性能提高很大, 因此才需要建立映射, 并且需要思考如何建立映射才能对性能更好
- 一个索引可以存储超过单个节点硬件限制的大量数据. 比如, 一个具有10亿文档数据的索引占据1TB磁盘空间, 而任一节点都可能没有这样大的磁盘空间. 或者单个节点处理搜做请求, 响应太慢. 为了解决这个问题Elasticsearch提供了将索引分成多份的能力, 每一份就称之为分片. 当你创建一个索引的时候, 你可以指定你想要的分片的数量. 每个分片本身也是一个功能完善并且独立的"索引", 这个"索引"可以被放置到集群中的任何节点上
- 分片很重要, 主要有两方面的内容:
- 允许你水平分割 / 扩展你的内容容量
- 允许你在分片之上进行分布式的, 并行的操作, 进而提高性能 / 吞吐量
- 至于一个分片怎么分布, 他的文档怎样聚合搜索请求, 是完全由Elasticsearch管理的, 对于作为用户的你来说, 这些都是透明的, 无需过分关心
- 在一个网络 / 云的环境里面, 失败随时都可能发生, 在某个分片 / 节点不知怎么的就处于离线状态, 或者由于任何原因消失了, 这种情况下, 有一个故障转移机制是非常有用的并且是强烈推荐的. 为此目的, Elasticsearch允许你创建分片的一份或者多份拷贝, 这些拷贝叫做复制分片(副本)
- 复制分片之所以重要, 有两个重要原因
- 在分片 / 节点失败的情况下, 提高了高可用性. 因为这个原因, 注意到复制分片从不与源 / 主要(original / primary)分片置于同一节点上是非常重要的
- 扩展你的搜索量 / 吞吐量, 因为搜索可以在所有的副本上并行运行
- 总之, 每个索引可以被分成多个分片. 一个索引也可以被复制0次(意思是没有复制)或多次. 一旦复制了, 每个索引就有了主分片(作为复制源头的原来的分片)和复制分片(主分片的拷贝)之别. 分片和复制的数量可以在索引创建的时候指定. 在索引创建之后, 你可以在任何时候动态的修改复制的数量, 但是你事后不能改变分片的数量. 默认情况下Elasticsearch中的每一个索引被分片一个主分片和一个复制, 这意味着, 如果你的集群中至少有两个节点, 你的索引将会有一个主分片和另外一个复制分片(一个完全拷贝), 这样的话每个索引总共就有两个分片, 我们需要根据索引需要确定分片个数
将分片分配给某个节点的过程, 包括分配主分片或者副本. 如果是副本, 还包含从主分片复制数据的过程. 这个过程是有master节点完成的
PUT{地址}/{索引}
默认一个索引分片是1副本是1, 如果需要设置分片数和副本数全局搜索单节点集群
向ES服务器发送PUT请求(不能使用Post请求的原因是因为Post是非幂等性的)
PUThttp://localhost:9200/azang
如果使用的了post请求来创建文档的话会返回如下问题
{
"error": "Incorrect HTTP method for uri [/azang] and method [POST], allowed: [PUT, DELETE, GET, HEAD]",
"status": 405
}
如果开启了密码验证请求返回状态码401解决方案
GET{地址}/{索引}
DELETE{地址}/{索引}
查询索引相关信息使用Get请求GET{地址}/{索引}
GEThttp://localhost:9200/azang
结果集如下
{
"azang": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "azang",
"creation_date": "1661495314719",
"number_of_replicas": "1",
"uuid": "uKbq8YPySPiY9slo_ZPhEA",
"version": {
"created": "8040099"
}
}
}
}
}
删除索引相关信息使用Delete请求DELETE{地址}/{索引}
DELETEhttp://localhost:9200/azang
结果如下
{
"acknowledged": true
}
PSOT{地址}/{索引}/_doc``PSOT{地址}/{索引}/_doc/{id}
(具体参数要放到body里面json格式)-----非自定义_id-------Post请求
不能使用Put请求因为会返回一个_id, 每一个_id都是不一样的, 且会作为唯一标识使用, 所以这里面要求非幂等性的
POSThttp://localhost:9200/azang/_doc
{
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
结果
{
"_index": "azang",
"_id": "r5i-6IIBsEf_A_EJ_SHH",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
(具体参数要放到body里面json格式)-----自定义_id-------Post请求
不能使用Put请求因为会返回一个_id, 每一个_id都是不一样的, 且会作为唯一标识使用, 所以这里面要求非幂等性的
POSThttp://localhost:9200/azang/_doc/10086
{
"title": "olala",
"category": "palala",
"image": "xilala",
"price": 66666
}
结果
{
"_index": "azang",
"_id": "10086",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
只搜索一条对应的-----Get请求PSOT{地址}/{索引}/_doc/{id}
GEThttp://localhost:9200/azang/_doc/10086
结果
{
"_index": "azang",
"_id": "10086",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "olala",
"category": "palala",
"image": "xilala",
"price": 66666
}
}
搜索全部-----Get请求GET{地址}/{索引}/_search
GEThttp://localhost:9200/azang/_search
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "r5i-6IIBsEf_A_EJ_SHH",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "10086",
"_score": 1,
"_source": {
"title": "olala",
"category": "palala",
"image": "xilala",
"price": 66666
}
}
]
}
}
PUT{地址}/{索引}/_doc/{id}
----Put请求
PUThttp://localhost:9200/azang/_doc/10086
{
"title": "olala",
"category": "palala",
"image": "xilala",
"price": 66666
}
结果
{
"_index": "azang",
"_id": "10086",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
PSOT{地址}/{索引}/_update/{id}
----POST请求{地址}/{索引}/_update/{id}
POSThttp://localhost:9200/azang/_update/10086
{
"doc": {
"title": "aaaaalala",
"price": 1111
}
}
结果
{
"_index": "azang",
"_id": "10086",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
GET{地址}/{索引}/_search?q={字段}:值
POST{地址}/{索引}/_search
GET请求http://localhost:9200/azang/_search?q=title:aaaaalala
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "azang",
"_id": "10086",
"_score": 0.2876821,
"_source": {
"title": "aaaaalala",
"category": "aalala",
"image": "xilala",
"price": 1111
}
}
]
}
}
POST请求http://localhost:9200/azang/_search
{
"query": {
"match": {
"price": 1111
}
}
}
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "10086",
"_score": 1,
"_source": {
"title": "aaaaalala",
"category": "aalala",
"image": "xilala",
"price": 1111
}
}
]
}
}
查询全部 -> POST请求http://localhost:9200/azang/_search
{
"query": {
"match_all": {}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "10086",
"_score": 1,
"_source": {
"title": "aaaaalala",
"category": "aalala",
"image": "xilala",
"price": 1111
}
}
]
}
}
分页查询 -> POST请求http://localhost:9200/azang/_search
from表示起始位置, size表示查几个
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "bIyCDYMB-29t2ccZqbwa",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "bYyCDYMB-29t2ccZr7xl",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
}
]
}
}
限制字段操作查询 -> POST请求http://localhost:9200/azang/_search
_source只查询对应的字段
{
"query": {
"match_all": {}
},
"from": 1,
"size": 2,
"_source": [
"title"
]
}
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "bIyCDYMB-29t2ccZqbwa",
"_score": 1,
"_source": {
"title": "wohenda"
}
},
{
"_index": "azang",
"_id": "bYyCDYMB-29t2ccZr7xl",
"_score": 1,
"_source": {
"title": "wohenda"
}
}
]
}
}
排序操作查询 -> POST请求http://localhost:9200/azang/_search
sort排序
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2,
"_source": [
"title"
],
"sort": {
"price": {
"order": "desc"
}
}
}
结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "azang",
"_id": "bIyCDYMB-29t2ccZqbwa",
"_score": null,
"_source": {
"title": "wohenda"
},
"sort": [
66666.66
]
},
{
"_index": "azang",
"_id": "bYyCDYMB-29t2ccZr7xl",
"_score": null,
"_source": {
"title": "wohenda"
},
"sort": [
66666.66
]
}
]
}
}
must 约等于and 表示都满足
GET请求http://localhost:9200/azang/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g"
}
},
{
"match": {
"price": 6666670000077.66
}
}
]
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.5404449,
"hits": [
{
"_index": "azang",
"_id": "cYyTDYMB-29t2ccZ1LzF",
"_score": 2.5404449,
"_source": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666670000077.66
}
}
]
}
}
should 约等于or 表示或者
GET请求http://localhost:9200/azang/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g"
}
},
{
"match": {
"price": 6666677.66
}
}
]
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.6739764,
"hits": [
{
"_index": "azang",
"_id": "cYyTDYMB-29t2ccZ1LzF",
"_score": 1.6739764,
"_source": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666670000077.66
}
},
{
"_index": "azang",
"_id": "coyiDYMB-29t2ccZ1bxj",
"_score": 1,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666677.66
}
}
]
}
}
范围查询 filter -> range -> gt(大于) -> lt(小于)
GET请求http://localhost:9200/azang/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g"
}
},
{
"match": {
"category": "你忍一下"
}
}
],
"filter": {
"range": {
"price": {
"gt": 6666677.66
}
}
}
}
}
}
结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.9321306,
"hits": [
{
"_index": "azang",
"_id": "cYyTDYMB-29t2ccZ1LzF",
"_score": 1.9321306,
"_source": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666670000077.66
}
}
]
}
}
模糊查询 match模糊查询 -> (具体是否可以进行需要根据映射关系)
GET请求http://localhost:9200/azang/_search
{
"query": {
"match": {
"category": "上中"
}
}
}
结果
{
"took": 679,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.837029,
"hits": [
{
"_index": "azang",
"_id": "dIyfEIMB-29t2ccZ8bzQ",
"_score": 2.837029,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上中",
"image": "pron",
"price": 6666677.66
}
},
{
"_index": "azang",
"_id": "c4yfEIMB-29t2ccZzrwf",
"_score": 1.3042111,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上",
"image": "pron",
"price": 6666677.66
}
}
]
}
}
完全匹配 match_phrase
GET请求http://localhost:9200/azang/_search
{
"query": {
"match_phrase": {
"category": "上中"
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.837029,
"hits": [
{
"_index": "azang",
"_id": "dIyfEIMB-29t2ccZ8bzQ",
"_score": 2.837029,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上中",
"image": "pron",
"price": 6666677.66
}
}
]
}
}
查询内容高亮显示
GET请求http://localhost:9200/azang/_search
{
"query": {
"match_phrase": {
"category": "上中"
}
},
"highlight": {
"fields": {
"categroy": {}
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.837029,
"hits": [
{
"_index": "azang",
"_id": "dIyfEIMB-29t2ccZ8bzQ",
"_score": 2.837029,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上中",
"image": "pron",
"price": 6666677.66
}
}
]
}
}
分组
GET请求http://localhost:9200/azang/_search
{
"aggs": { // 聚合操作
"price_group": { // 名称
"terms": { // 分组
"field": "price" // 分组字段
}
}
}
}
结果
{
"took": 39,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azang",
"_id": "bIyCDYMB-29t2ccZqbwa",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "bYyCDYMB-29t2ccZr7xl",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "boyCDYMB-29t2ccZsrzl",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "b4yCDYMB-29t2ccZvryX",
"_score": 1,
"_source": {
"title": "wohenda",
"category": "你忍一下",
"image": "pron",
"price": 66666.66
}
},
{
"_index": "azang",
"_id": "cIyTDYMB-29t2ccZqry7",
"_score": 1,
"_source": {
"title": "wohendaaaaaa",
"category": "你忍一下",
"image": "pron",
"price": 66666777.66
}
},
{
"_index": "azang",
"_id": "cYyTDYMB-29t2ccZ1LzF",
"_score": 1,
"_source": {
"title": "wohendaaaaaaa'd'ga'd'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666670000077.66
}
},
{
"_index": "azang",
"_id": "coyiDYMB-29t2ccZ1bxj",
"_score": 1,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下",
"image": "pron",
"price": 6666677.66
}
},
{
"_index": "azang",
"_id": "c4yfEIMB-29t2ccZzrwf",
"_score": 1,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上",
"image": "pron",
"price": 6666677.66
}
},
{
"_index": "azang",
"_id": "dIyfEIMB-29t2ccZ8bzQ",
"_score": 1,
"_source": {
"title": "wohendaaaaaaa'ga'f'ga'g",
"category": "你忍一下上中",
"image": "pron",
"price": 6666677.66
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 66666.65625,
"doc_count": 4
},
{
"key": 6666677.5,
"doc_count": 3
},
{
"key": 66666776,
"doc_count": 1
},
{
"key": 6666670047232,
"doc_count": 1
}
]
}
}
}
分组只显示聚合操作结果不显示详细信息
GET请求http://localhost:9200/azang/_search
{
"aggs": { // 聚合操作
"price_group": { // 名称
"terms": { // 分组
"field": "price" // 分组字段
}
}
},
"size": 0 // 不显示具体详细信息
}
结果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 66666.65625,
"doc_count": 4
},
{
"key": 6666677.5,
"doc_count": 3
},
{
"key": 66666776,
"doc_count": 1
},
{
"key": 6666670047232,
"doc_count": 1
}
]
}
}
}
平均值
GET请求http://localhost:9200/azang/_search
{
"aggs": { // 聚合操作
"price_avg": { // 名称
"avg": { // 平均值
"field": "price" // 取平均字段
}
}
},
"size": 0
}
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 740750775634.125
}
}
}
索引创建映射关系前提需要先创建索引
PUT请求 http://localhost:9200/azangmapping/_mapping
{
"properties": {
"name": {
"type": "text", // 可分词
"index": true // 可以被索引
},
"sex": {
"type": "keyword", // 不可以被分词必须精准匹配
"index": true // 可以被索引
},
"tel": {
"type": "keyword", // 不可以被分词必须精准匹配
"index": false // 不可以被索引
}
}
}
结果
{
"acknowledged": true
}
可以被分词就说明match是模糊查询的进行测试分别两个请求
首先根据上面RESTFUL的请求确定了单个字段对于type是keyword的进行校验
首先有两组可用数据分别是
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "azangmapping",
"_id": "1001",
"_score": 1,
"_source": {
"name": "张三",
"sex": "男",
"tel": 123123123
}
},
{
"_index": "azangmapping",
"_id": "1002",
"_score": 1,
"_source": {
"name": "张三",
"sex": "男的",
"tel": 123123123
}
}
]
}
}
POST请求 http://localhost:9200/azangmapping/_search
{
"query": {
"match": {
"sex": "的" // sex是不可分词的所以结果应该是查不出来的
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
POST请求 http://localhost:9200/azangmapping/_search
{
"query": {
"match": {
"name": "张" // name是分词的所以结果应该差的出来所有包含'张'的数据
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.18232156,
"hits": [
{
"_index": "azangmapping",
"_id": "1001",
"_score": 0.18232156,
"_source": {
"name": "张三",
"sex": "男",
"tel": 123123123
}
},
{
"_index": "azangmapping",
"_id": "1002",
"_score": 0.18232156,
"_source": {
"name": "张三",
"sex": "男的",
"tel": 123123123
}
}
]
}
}