欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ElasticSearch相关--进阶

时间:2023-06-23
1.QUERY DSL

GET bank/_search{ "query": { "match_all": {} }, "sort": { "account_number": "asc", "balance":"desc" }}GET bank/_search{ "query": { "match_all": {} }, "sort": [ { "balance": { "order": "desc" } } ], "from": 0, "size": 20, "_source":[ "balance","firstname"]}

分词匹配 match

GET bank/_search{ "query": { "match": { "firstname": "West" } }}//查到两条,对检索条件分词匹配查询,按照评分进行排序GET bank/_search{ "query": { "match": { "address": "Kings" } }}

地址包含mill或者road都能匹配

短语匹配 match_phrase

GET bank/_search{ "query": { "match_phrase": { "address": "mill road" } }}

只匹配包含mill road的

多条件查询

GET bank/_search{ "query": { "multi_match": { "query": "mill", "fields": [ "state", "address" ] } }}

bool符合查询

GET bank/_search{ "query": { "bool": { "must": [ 必须满足 { "match": { "gender": "F" } }, { "match": { "address": "Madison" } } ], "must_not": [ 必须不满足 { "match": { "age": "22" } } ], "should": [ 应该满足,不满足也可以查到 {"match": { "balance": "32838" }} ] } }}

filter结果过滤

GET bank/_search{ "query": { "bool": { "filter": { "range": { "age": { "gte": 18, "lte": 30 } } } } }}

filter不会返回相关性得分

2.aggregations(执行聚合)

聚合提供了从数据中分组和提取数据的能力。

搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人详情。

GET bank/_search{ "query": { "match": { "address": "mill" } }, "aggs": { "ageAgg": { "terms": { "field": "age", "size": 10 } }, "avgAgg":{ "avg": { "field": "age" } }, "balanceAvg":{ "avg": { "field": "balance" } } }, "size": 0 只看聚合,不看详情}

按照年龄聚合,并且请求这些年龄段的这些人的平均薪资

GET bank/_search{ "query": { "match_all": { } }, "aggs": { "ageAgg": { "terms": { "field": "age", "size": 10}, "aggs": { "ageAvg": { "avg": { "field": "balance" } } } } }, "size": 0}

按照年龄聚合,并且这些年龄段中M平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

GET bank/_search{ "query": { "match_all": {} }, "aggs": { "ageA": { "terms": { "field": "age", "size": 101 }, "aggs": { "genderA": { "terms": { "field": "gender.keyword", "size": 10 }, "aggs": { "balanceA": { "avg": { "field": "balance" } } } } } } }}

3.映射

创建一个索引,并配置映射

PUT /my_index{ "mappings": { "properties": { "age":{ "type": "integer" }, "email":{ "type": "keyword" }, "name":{ "type": "text" } } }}

添加新的字段映射

PUT /my_index/_mapping{ "properties":{ "employee-id":{ "type":"keyword", "index": false } }}

修改映射&数据迁移

已经存在的映射字段,不能更新,更新必须创建新的牵引进行数据迁移

数据迁移

数据迁移 bank->newbankPOST _reindex{ "source": { "index": "bank", "type": "account" }, "dest": { "index": "newbank" }}查询newbankGET /newbank/_search

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。