索引信息

查询映射结构
GET /index_name

查询和聚合

常规查询,from和size是分页查询,这两个字段不显示指定也是有默认值的(from=0,size=10); must 相当于 and,should相当于or;terms相当于in,term相当于is,wildcard相当于like,range相当于between;
GET t_binding/_search { "from": 0, "size": 10, "query": { "bool": { "must": [ { "terms": { "FIELD": [ "VALUE1", "VALUE2" ] } }, { "term": { "FIELD": { "value": "VALUE" } } }, { "range": { "FIELD": { "gte": 10, "lte": 20 } } }, { "wildcard": { "FIELD": { "value": "VALUE" } } } ], "should": [ {} ] } } }
过滤查询
GET t_binding/_search { "query": { "bool": { "must": [ { "term": { "bind_no_type": { "value": "AXE" } } } ], "filter": { "term": { "bind_id": "VALUE" } } } } }
脚本查询
GET t_private_number_distribution/_search { "query": { "bool": { "filter": { "script": { "script": { "source": "doc['e_no_enable'].length + doc['e_no_disable'].length!=9000" } } } } } }
聚合查询:桶聚合, NAME, AGG_TYPE, SIZE:top多少
GET t_binding/_search?size=0 { "query": { "bool": { "must": [ { "term": { "bind_no_type": { "value": "AXE" } } } ] } }, "aggs": { "group_by_sno": { "terms": { "field": "s_no", "size": 10 }, "sub-aggs": { "NAME": { "AGG_TYPE": {} } } } } }
聚合结果:
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 22, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "group_by_sno" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "17801476624", "doc_count" : 5 }, { "key" : "18413271317", "doc_count" : 4 }, { "key" : "41000000008", "doc_count" : 4 }, { "key" : "18430870106", "doc_count" : 2 }, { "key" : "18430870107", "doc_count" : 2 }, { "key" : "12345670013", "doc_count" : 1 }, { "key" : "12345670516", "doc_count" : 1 }, { "key" : "17897515942", "doc_count" : 1 }, { "key" : "41000000002", "doc_count" : 1 }, { "key" : "41000000007", "doc_count" : 1 } ] } } }
聚合查询:SUM,MIN,MAX,AVG,VALUE_COUNT
GET t_binding/_search?size=0 { "query": { "bool": { "must": [ { "term": { "bind_no_type": { "value": "AXE" } } } ] } }, "aggs": { "sum": { "sum": { "field": "" } }, "min":{ "min": { "field": "" } }, "max":{ "max": { "field": "" } }, "avg":{ "avg": { "field": "" } }, "count":{ "value_count": { "field": "" } } } }
聚合查询:日期直方图
GET t_binding/_search?size=0 { "query": { "bool": { "must": [ { "term": { "bind_no_type": { "value": "AXE" } } } ] } }, "aggs": { "date": { "date_histogram": { "field": "create_tm", "interval": "month" } } } }
查询结果示例:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 22, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "date" : { "buckets" : [ { "key_as_string" : "2022-05-01T00:00:00.000Z", "key" : 1651363200000, "doc_count" : 1 }, { "key_as_string" : "2022-06-01T00:00:00.000Z", "key" : 1654041600000, "doc_count" : 21 } ] } } }
查询所有索引
GET _cat/indices
结果示例
green open t_sms_billing_detail_2022-09-13 ROno8AiySFWW_-iu-gHPSA 2 1 117158 0 82.4mb 41.2mb green open t_call_recoding_2021-12-08 2byyq6GrRpCP_ADY9-RRLQ 10 1 62217 58 69.8mb 34.9mb green open t_call_recoding_2022-08-06 f46geckQRVavtY4-43BqNA 2 1 126996 16 140.1mb 70mb green open t_call_recoding_2021-08-09 -M6E9CD1TmiJZDvw-BP2eA 5 1 42 7 599.4kb 299.7kb green open t_call_billing_detail_2022-07-05 eEX1pzvSQwCp6XJOyGfiSg 2 1 323633 0 336.2mb 168.1mb green open t_call_log_2022-01-19 RLvBZb5PQEuSP4KktGXH7A 10 1 703578 0 382.6mb 191.2mb green open t_binding_log_2022-01-08 ZaqlEgipRdmBY69V8mBrlg 10 1 976035 0 613.2mb 306.5mb green open t_binding_log_2021-06-23 jwgX660ZQGqT3tRQzRzF2Q 5 1 673 0 1.1mb 567.8kb green open t_call_log_2022-05-19 jyXw1bo_QK2EoYns4xI3KA 2 1 436463 0 216mb 108.5mb

创建

创建模版:以t_binding_log_template为例子
# t_binding_log_template是模版名称,t_binding_log*是索引的模版,t_binding_log是所有索引的别名,_doc是索引的type PUT _template/t_binding_log_template { "index_patterns": [ "t_binding_log*" ], "aliases": { "t_binding_log": {} }, "settings": { "number_of_shards": 10, "number_of_replicas": 1, "refresh_interval": "60s" }, "mappings": { "_doc": { "properties": { "log_source": { "type": "keyword" }, "log_type": { "type": "keyword" }, "fk_bind_id": { "type": "keyword" }, "fk_enterprise_id": { "type": "keyword" }, "fk_account_id": { "type": "keyword" }, "fk_supplier_id":{ "type":"keyword" }, "fk_account_name":{ "type":"keyword" }, "fk_supplier_name":{ "type":"keyword" }, "fk_enterprise_name":{ "type":"keyword" }, "fk_pool_sn": { "type": "keyword" }, "a_no": { "type": "keyword" }, "s_no": { "type": "keyword" }, "s_no_tag": { "type": "keyword" }, "b_no": { "type": "keyword" }, "e_no": { "type": "keyword" }, "bind_no_type": { "type": "keyword" }, "bind_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "unbind_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "expired_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "b_expired_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "need_record": { "type": "integer" }, "call_display_type": { "type": "integer" }, "before_connection_audio": { "type": "keyword" }, "after_connection_audio": { "type": "keyword" }, "user_data": { "type": "keyword" }, "create_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "update_tm": { "type": "date", "format": "date_optional_time||epoch_millis" }, "creator": { "type": "keyword" }, "updater": { "type": "keyword" }, "status_code": { "type": "integer" }, "fail_cause": { "type": "text" } } } } }
创建索引:以t_binding为例子
PUT t_binding { "mappings": { "_doc": { "properties": { "_class": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "a_no": { "type": "keyword" }, "after_connection_audio": { "type": "keyword" }, "b_expired_tm": { "type": "date", "format": "date_optional_time" }, "b_no": { "type": "keyword" }, "before_connection_audio": { "type": "keyword" }, "bind_id": { "type": "keyword" }, "bind_no_type": { "type": "keyword" }, "bind_tm": { "type": "date", "format": "date_optional_time" }, "call_display_type": { "type": "integer" }, "create_tm": { "type": "date", "format": "date_optional_time" }, "creator": { "type": "keyword" }, "e_no": { "type": "keyword" }, "expired_tm": { "type": "date", "format": "date_optional_time" }, "fk_account_id": { "type": "keyword" }, "fk_account_name": { "type": "keyword" }, "fk_enterprise_id": { "type": "keyword" }, "fk_enterprise_name": { "type": "keyword" }, "fk_pool_sn": { "type": "keyword" }, "fk_supplier_id": { "type": "keyword" }, "fk_supplier_name": { "type": "keyword" }, "id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "need_record": { "type": "integer" }, "s_no": { "type": "keyword" }, "s_no_tag": { "type": "keyword" }, "update_tm": { "type": "date", "format": "date_optional_time" }, "updater": { "type": "keyword" }, "user_data": { "type": "keyword" }, "version": { "type": "long" } } } }, "settings": { "index": { "refresh_interval": "2s", "number_of_shards": "40", "number_of_replicas": "1" } } }
创建别名
POST /_aliases { "actions":[ { "add":{ "index": ["index_name"], "alias": alias_name } } ] }
批量插入数据,官方bulk api
POST /t_region/_doc/_bulk { "index":{} } {"province":"上海市","city":"市辖区","province_city":"/上海市/市辖区"} { "index":{} } {"province":"云南省","city":"临沧市","province_city":"/云南省/临沧市"} { "index":{} } {"province":"云南省","city":"丽江市","province_city":"/云南省/丽江市"}

更新

局部更新(update by query)
POST index_name/_update_by_query?slices=auto&conflicts=proceed&wait_for_completion=false { "script": { "source": "ctx._source.likes++", "lang": "painless" }, "query": { "term": { "user": "kimchy" } } }
更新索引配置
索引只能增加字段,不能修改和删除字段
PUT index_name/_mapping/_doc { "properties": { "email": { "type": "keyword" } } }
PUT /index_name/_settings { "index" : { "number_of_replicas" : 2, "refresh_interval" : "1s" } }
重建索引
POST /_reindex?wait_for_completion=false { "source": { "index": index_source_name, "size":5000 }, "dest": { "index": index_dest_name, "op_type": "create" }, "conflicts": "proceed" }

删除

删除记录(Delete By Query
//多线程并发删除满足查询条件的数据 POST /index-name/_delete_by_query?slices=auto&conflicts=proceed&wait_for_completion=false { "query": { "bool": {} } }
删除索引
DELETE index_name
 
 

参考

「Elasticsearch」ES重建索引怎么才能做到数据无缝迁移呢?
众所周知,Elasticsearch是⼀个实时的分布式搜索引擎,为⽤户提供搜索服务。当我们决定存储某种数据,在创建索引的时候就需要将数据结构,即Mapping确定下来,于此同时索引的设定和很多固定配置将不能改变。 那如果后续业务发生变化,需要改变数据结构或者更换ES更换分词器怎么办呢?为此,Elastic团队提供了很多通过辅助⼯具来帮助开发⼈员进⾏重建索引的方案。 如果对 reindex API 不熟悉,那么在遇到重构的时候,必然事倍功半,效率低下。反之,就可以方便地进行索引重构,省时省力。 假设之前我们已经存在一个blog索引,因为更换分词器需要对该索引中的数据进行重建索引,以便支持业务使用新的分词规则搜索数据,并且尽可能使这个变化对外服务没有感知,大概分为以下几个步骤: 在这里推荐一个ES管理工具 Kibana ,主要针对数据的探索、可视化和分析。 同步等待 接⼝将会在 reindex 结束后返回 在 kibana 中的使用如下所示 当然高版本(7.1.1)中,ES都有提供对应的 Java REST Client ,比如 为了防止赘述,接下来举例全部以 kibana中请求介绍,如果有需要用 Java REST Client ,可以自行去ES官网查看。 异步执⾏ 如果 reindex 时间过⻓,建议加上 wait_for_completion=false 的参数条件,这样 reindex 将直接返回 taskId 。 返回: op_type 参数 op_type 参数控制着写入数据的冲突处理方式,如果把 op_type 设置为 create【默认值】,在 _reindex API 中,表示写入时只在 dest
「Elasticsearch」ES重建索引怎么才能做到数据无缝迁移呢?
badge