elasticsearch常用基础命令及查询语句
elasticsearch常用基础命令及查询语句
·
提示:以下是本篇文章正文内容,下面案例可供参考
一、elasticsearch基础命令
1.查看节点信息
http://127.0.0.1:9200/_cat/nodes
2.查看健康状况
http://127.0.0.1:9200/_cat/health
3.查看主节点信息
http://127.0.0.1:9200/_cat/master
4.查看所有索引
http://127.0.0.1:9200/_cat/indices
5.保存
(1)put请求必须带id保存,多次操作是更新操作
http://127.0.0.1:9200/_cat/indices
{
"name":"john "
}
(2)POST请求可以不带id保存,是新增,带id是新增或更新
http://192.168.98.6:9200/customer/external/
{
"name":"YUHAN "
}
6.查询id为1的文档
http://192.168.98.6:9200/customer/external/1
7.乐观锁修改(put请求)
http://192.168.98.6:9200/customer/external/1?if_seq_no=3&if_primary_term=2
{
"name":"1"
}
8.更新文档(post请求)
http://192.168.98.6:9200/customer/external/1/_update
{
"doc":{
"name":"3"
}
}
9.删除文档(delete请求)
http://192.168.98.6:9200/customer/external/1
10.删除索引(delete请求)
http://192.168.98.6:9200/customer/
11.删除索引数据,不删除索引结构(post请求)
http://127.0.0.1:9200/knowledge/_delete_by_query
{
"query": {
"match_all": {}
}
}
12.批量保存(post请求)
http://192.168.98.6:9200/customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
二、elasticsearch查询语句
1.查询bank索引下所有数据
192.168.98.6:9200/bank/_search
{
"query": { //查询条件
"match_all": {}
},
"sort": [ //排序
{
"balance": {
"order": "desc"
}
}
],
"from": 0, //从第几条开始
"size": 5, //到第几条结束
"_source": ["balance","firstname","address"] //设置返回的字段,没写的字段则不返回
}
2.match检索
192.168.98.6:9200/bank/_search
{
"query": {
"match": {
//存到索引中的值是数字时时精确搜索,是字符串是可以是模糊搜索,会分词,只包含mill或者road也能搜出来
"address": "mill road"
}
}
}
3.match_phrase短语匹配和与match中.keyword用法区别
192.168.98.6:9200/bank/_search
{
"query": {
"match_phrase": {
//短语匹配,检索时不会分词,只要包含mill road这个短语就能搜出来,例如mill road rain也能搜出来
"address": "mill road"
}
}
}
// { 只有address值等于789 madison才会被搜出来,例如789 madison rain搜不出来
// "query": {
// "match": {
// "address.keyword": "789 madison"
// }
// }
// }
4.multi_match多字段匹配
192.168.98.6:9200/bank/_search
//检索时会分词,address或者city字段值中包含mill或者movico都会被搜出来
{
"query": {
"multi_match": {
"query": "mill movico",
"fields": ["address","city"]
}
}
}
5.bool中must、must_not、should用法
192.168.98.6:9200/bank/_search
//必须同时满足gender包含M,address包含mill、age不等于28这三个条件
//should中的条件满不满足不影响查询结果,但是满足条件后得分会更高
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": "28"
}
}
],
"should": [
{
"match": {
"lastname": "Wallace"
}
},
{
"match": {
"firstname": "Parker"
}
}
]
}
}
}
6.filter过滤
192.168.98.6:9200/bank/_search
//filter只过滤数据,不会计算得分
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
7.term精确字段查询
192.168.98.6:9200/bank/_search
//text文本类型字段建议使用match
//age等字段精确检索的,建议使用term
{
"query": {
"term": {
"age": {
"value": "28"
}
}
}
}
8.aggregations聚合分析
192.168.98.6:9200/bank/_search
//搜索address中包含mill的所有人的年龄分布以及平均年龄
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"ageAgg": { //ageAgg,ageAvg,balance 聚合名称,可以自定义
"terms": { //terms,avg 聚合类型
"field": "age",
"size": 10 //只展示10种年龄情况,例如10岁、18岁等情况
}
},
"ageAvg":{
"avg": {
"field": "age"
}
},
"balanceAvg":{
"avg": {
"field": "balance"
}
}
},
"size": 0 //"size": 0 表示只看聚合分析结果,不看query中的结果
}
9.aggregations子聚合
192.168.98.6:9200/bank/_search
//按照年龄聚合,并且查看这些年龄段的人的平均薪资
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"ageAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
10.aggregations子聚合和多个聚合
192.168.98.6:9200/bank/_search
//查出所有年龄分布,并且这些年龄段中性别为M的平均薪资和性别为F的平均薪资以及这个年龄段的总体总体平均薪资
//terms中字段为文本类型需要加上.keyword
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword" //terms中字段为文本类型需要加上.keyword
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"ageBalanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
11.创建索引并置顶映射(put请求)
192.168.98.6:9200/my_index
//类似于mysql数据类型意思
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
12.在已存在索引中新增映射字段,不能修改已存在的映射字段
192.168.98.6:9200/my_index/_mapping
//因为修改映射后,对应字段保存的数据检索方式也会发生变化,会出问题,建议创建新索引使用数据迁移
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false //false指不需要被索引,检索条件不能用employee-id来查询,默认为true
}
}
}
13.数据迁移
192.168.98.6:9200/_reindex
//es6以后版本已不用type,_mapping,,默认为_doc
//将twitter老索引下tweet类型的数据迁移到新索引tweets下
{
"source": {
"index": "twitter", //老索引名
"type": "tweet"
},
"dest": {
"index": "tweets" //新索引名,不用type,默认为_doc
}
}
14.minimum_should_match
(1)minimum_should_match在match中应用
http://192.168.98.6:9200/bank/_search
//复杂查询
{
"query": {
"match": {
"address": {
"query": "282 Kings Place",
//match检索address时会将282 Kings Place分词,例如分成282,Kings,Place,minimum_should_match:2表示最少包含分词后三个词中的2个
"minimum_should_match": 2
}
}
}
}
(2)minimum_should_match在should中应用
//如果配置成数字2,表示查询关键词被分词器分成2个及其以下的term 时,条件都需要满足才能符合查询要求
//minimum_should_match值为百分比时默认向下取整
{
"query": {
"bool": {
"should": [
{
"match": {
"address": "Holmes Lane"
}
},
{
"match": {
"city": "Brogan"
}
}
],
"minimum_should_match": 2
}
}
}
15.es创建实体(put请求)
http://192.168.98.6:9200/product
{
"mappings": {
"properties": {
"skuId": {
"type": "long"
},
"spuId": {
"type": "keyword"
},
"skuTitle": {
"type": "text",
"analyzer": "ik_smart"
},
"skuPrice": {
"type": "keyword"
},
"skuImg": {
"type": "keyword",
"index": false,
"doc_values": false
},
"saleCount": {
"type": "long"
},
"hasStock": {
"type": "boolean"
},
"hotScore": {
"type": "long"
},
"brandId": {
"type": "long"
},
"catalogId": {
"type": "long"
},
"brandName": {
"type": "keyword",
"index": false,
"doc_values": false
},
"brandImg": {
"type": "keyword",
"index": false,
"doc_values": false
},
"catalogName": {
"type": "keyword",
"index": false,
"doc_values": false
},
"attrs": {
"type": "nested",
"properties": {
"attrId": {
"type": "long"
},
"attrName": {
"type": "keyword",
"index": false,
"doc_values": false
},
"attrValue": {
"type": "keyword"
}
}
}
}
}
}
16.多字段检索高亮
http://192.168.98.6:9200/gulimall_product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "小米"
}
},
{
"match": {
"brandName": "小米"
}
}
],
"filter": [
{
"term": {
"catalogId": 225
}
},
{
"terms": {
"brandId": [
"1",
"2",
"10"
]
}
},
{
"term": {
"hasStock": true
}
},
{
"range": {
"skuPrice": {
"gte": "0",
"lte": "2200"
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": "15"
}
},
{
"terms": {
"attrs.attrValue": [
"高通(Qualcomm)",
"骁龙855"
]
}
}
]
}
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "asc"
}
}
],
"from": 0,
"size": 5,
"highlight": {
"fields": {
"skuTitle": {
"number_of_fragments": 0
},
"brandName": {
"number_of_fragments": 0
}
},
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>"
},
"aggs": {
"brandAgg": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"brandNameAgg": {
"terms": {
"field": "brandName",
"size": 10
}
},
"brandImgAgg": {
"terms": {
"field": "brandImg",
"size": 10
}
}
}
},
"catalogAgg": {
"terms": {
"field": "catalogId",
"size": 10
},
"aggs": {
"catalogNameAgg": {
"terms": {
"field": "catalogName",
"size": 10
}
}
}
},
"attrAgg": {
"nested": { //嵌入式属性聚合
"path": "attrs" //嵌入式属性对象名
},
"aggs": {
"attrIdAgg": {
"terms": {
"field": "attrs.attrId",
"size": 10
},
"aggs": {
"attrNameAgg": {
"terms": {
"field": "attrs.attrName",
"size": 10
}
},
"attrValueAgg": {
"terms": {
"field": "attrs.attrValue",
"size": 10
}
}
}
}
}
}
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)