注意:在 ES 7.X 之后 删除了映射类型
参见 ES 官方说明:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/removal-of-types.html
一般碰到报错:”Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true”
,都是因为 ElasticSearch 从低版本升级到高版本造成的
新的 ElasticSearch 7.x,做了调整,要求 mapping 时不需要提交 type,为了确保旧代码的兼容性,加入了一个include_type_name
的参数,而这样的请求,Head 插件自带的请求时无法完成的这时候我么就需要借助 postman 工具。
创建索引和设置 Mapping 一步完成
创建文档
这一切都可以在 Head 的可视化界面中得到验证
GET /ssns-message-202104/_search
{ "size": 0, "query": { "terms": { "chatroomId": [ "4596907111", "5860373121", "5860373121" ] } }, "aggs": { "agg_chatroomId": { "terms": { "field": "chatroomId" } } } }结果
{ "took": 1, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 16829, "max_score": 0, "hits": [] }, "aggregations": { "agg_chatroomId": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "5860373121", "doc_count": 13999 }, { "key": "4596907111", "doc_count": 2830 } ] } } }
相当于 MySQL 中的语句如下
select count(1) from ssns-message-202104 where chatroomId in ('4596907111','5860373121','5860373121') group by chatroomId只查询指定字段的值
只查询文档中的_id 和 memberList 字段
GET xxxx/room/_search
{ "_source": [ "_id", "memberList" ], "query": { "term": { "departments.tree": { "value": "/xx/xx" } } } }使用正则表达式
查找 uin 字段中包含@chatroom 字符串的数据
GET xxx/account/_search
{ "query": { "bool": { "must": [ { "regexp": { "uin": { "value": ".*@chatroom.*" } } } ] } } }按照多个字段排序
先按照创建时间字段降序,再按照打分降序
GET xxx/account/_search
{ "query": {}, "sort": [ { "createTime": { "order": "desc" } }, { "_score": { "order": "desc" } } ] }查找存在某个字段的数据
查找存在 uin 字段的数据
GET xxx/account/_search
{ "query": { "bool": { "filter": [ { "exists": { "field": "uin" } } ] } } }分页查询
分页查询数据,相当于数据库中的 limit 关键字的用法,from 默认是 0,size 默认是 20
GET xxx/account/_search
{ "from": 0, "size": 20 }
假如页大小是 pageSize,页号为 pageNum
from=(pageNum-1)*pageSize
size=pageSize
例如:如果页大小是 20,要查询第三页的数据,那么 from 为(3-1)*`20=60,size 为 20
{ "query": { "bool": { "filter": [ { "script": { "script": { "source": "doc['rooms'].length >= 1", "lang": "painless" } } } ] } } }自定义排序脚本 语法
{ "query": {}, "sort": [ { "_script": { "script": { "inline": "doc['rooms'].values.size()" }, "type": "number", "order": "desc" } } ] }Java API
script script = new script("doc['rooms'].values.size()"); scriptSortBuilder sortBuilder = SortBuilders.scriptSort(script, scriptSortBuilder.scriptSortType.NUMBER).order(SortOrder.DESC);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)