资讯动态

ES查询 索引别名

发布时间:2026/8/10 22:09:43 来源:尧图企业网站定制
新增es索引及字段设置最大长度为10240以支持字段的模糊匹配查询curl-k-u elastic:Passw0rd-HContent-Type: application/json-XPUThttp://xxx:9200/group_tag_all_boci-d{settings:{number_of_shards:2,number_of_replicas:1},mappings:{properties:{client_task:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},stock_list:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},client_goods:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},client_active:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}}}}}索引新增字段,设置最大长度为10240以支持字段的模糊匹配查询curl-k-u elastic:Passw0rd-HContent-Type: application/json-XPUThttp://xxx:9200/group_tag_all_boci/_mapping-d{properties: { discount_coup: { // 新增的字段名 type: text, fields: { keyword: { // 子字段用于精确查询 type: keyword, ignore_above: 10240 // 超过该长度的内容不索引 } } } } }droptableifexiststmp.group_tag_all_boci_test_main;createtabletmp.group_tag_all_boci_test_main(client_id stringcommentkhh,name stringcomment姓名,age stringcomment年龄)ROWFORMAT SERDEorg.elasticsearch.hadoop.hive.EsSerDeSTOREDBYorg.elasticsearch.hadoop.hive.EsStorageHandlerTBLPROPERTIES(es.index.read.missing.as.emptytrue,-- 缺失索引读为空去重es.field.read.empty.as.nulltrue,-- 空字段读为NULLes.mapping.idclient_id,-- 文档唯一ID必选es.mapping.routingclient_id,--路由值控制数据分片位置 高并发、大数据量读写 可能出现数据倾斜热点分片es.write.operationupsert,-- 存在则更新不存在则插入-- es.write.dynamicstrict, -- 严格字段映射避免类型不匹配es.net.http.auth.userelastic,-- 认证用户es.net.http.auth.passPassw,-- 认证密码es.nodesxxx.xx.xxx.xxx:9200,xxx.xx.248.1:9200,xxx.xx.248.2:9200,-- 集群节点es.index.auto.createtrue,-- 自动创建索引生产建议提前创建es.nodes.wan.onlytrue,-- 跨网段/WAN环境必开es.read.metadatatrue,es.resourcegroup_tag_all_boci_test_main/_doc,-- 索引/类型es.index.read.missing.as.emptyyes,es.batch.write.concurrent.requests2,--调低总并发可控es.refreshfalse,--写入阶段关闭refresh提升吞吐es.input.max.docs.per.split1000000-- 在读取数据源(Hive ORC)的时候做逻辑分片限制每一个map split最多处理多少行文档es.transport.pool.size30,-- 连接池大小匹配节点数和并行度es.batch.size.bytes2097152,-- 单批大小默认1MB推荐写法-- es.batch.size.bytes 2mb, -- 单批大小默认1MB es大小写敏感es.batch.size.entries2000,-- 单批条目不写默认1000,提升吞吐量-- 高版本ES适配若集群7.x-- es.nodes.discoveryfalse, -- 关闭自动发现强制用配置节点-- es.net.ssl.enabledtrue, -- 若启用SSL需同步开启你的原配置有ssl协议-- es.net.ssl.protocolTLSv1.2 -- 明确SSL协议版本es.net.ssl.protocolssl);–如果想保存es重复数据droptableifexiststmp.group_tag_all_boci_test_main;createtabletmp.group_tag_all_boci_test_main(client_id stringcommentkhh,name stringcomment姓名,age stringcomment年龄)ROWFORMAT SERDEorg.elasticsearch.hadoop.hive.EsSerDeSTOREDBYorg.elasticsearch.hadoop.hive.EsStorageHandlerTBLPROPERTIES(es.index.read.missing.as.emptytrue,es.field.read.empty.as.nulltrue,-- 移除文档ID映射或改为非唯一字段避免ES按固定ID去重-- 原配置es.mapping.idclient_id,-- es.mapping.routingclient_id, --路由值控制数据分片位置 高并发、大数据量读写es.write.operationindex,-- 保持普通插入不触发更新逻辑es.net.http.auth.userelastic,es.net.http.auth.passPassw0rd,es.nodesxxx.xx.xxx.xxx:9200,xxx.xx.248.1:9200,xxx.xx.248.2:9200,es.index.auto.createtrue,es.nodes.wan.onlytrue,es.read.metadatatrue,es.resourcegroup_tag_all_boci_test_main/_doc,es.nodes.wan.onlytrue,es.index.read.missing.as.emptyyes,es.batch.write.concurrent.requests2,--调低总并发可控es.refreshfalse,--写入阶段关闭refresh提升吞吐es.input.max.docs.per.split1000000-- 在读取数据源(Hive ORC)的时候做逻辑分片限制每一个map split最多处理多少行文档es.transport.pool.size30,-- 连接池大小匹配节点数和并行度es.net.ssl.protocolssl);es.write.operationupsertes.write.operationindex--表已创建-- 示例1将写入逻辑改为index全量覆盖ALTERTABLEjgcrm_t_corp_dd_to_esSETTBLPROPERTIES(es.write.operationindex);-- 示例2将写入逻辑改为upsert增量更新ALTERTABLEjgcrm_t_corp_dd_to_esSETTBLPROPERTIES(es.write.operationupsert);--index 操作在 _id 存在时会覆盖整个文档旧文档被删除新文档插入而 upsert 是增量更新仅修改指定字段保留未更新的原有字段更适合需要保留历史字段的场景。取值 不存在对应文档时 存在对应文档时 核心特点 适用场景index 插入新文档 完全覆盖原有文档删除未出现字段 全量同步数据强一致 每日全量更新、数据重刷upset 插入新文档 仅更新非空字段保留未覆盖字段 增量同步保留历史字段 部分字段更新、增量数据补全es.write.operation 是控制数据写入 Elasticsearch 时操作类型的配置参数当设置为 ‘index’ 时其含义如下操作行为表示执行「索引」操作。如果文档的 _id 不存在则会创建新文档如果 _id 已存在则会覆盖更新现有文档相当于先删除旧文档再插入新文档版本号会递增。与其他操作的区别若设为 ‘create’仅在 _id 不存在时创建文档若 _id 已存在则会报错避免覆盖。若设为 ‘update’仅更新已存在的文档需配合 _id 使用若 _id 不存在可能报错具体取决于工具实现。若设为 ‘upsert’结合了 update 和 create_id 存在则更新不存在则创建新文档。‘index’ 是比较常用的默认行为之一适用于需要「有则更新、无则创建」的场景例如日志同步、数据全量覆盖更新等。在 Elasticsearch 中upsert 和 index 操作的插入效率针对「文档不存在时的创建场景」基本接近但在「文档已存在时的处理场景」中两者的效率差异较明显主要取决于操作逻辑的复杂度当文档不存在时创建新文档index 操作直接创建新文档本质是 create 逻辑仅需写入一次数据。upsert 操作因 _id 不存在执行预定义的「插入逻辑」如使用 upsert 模板创建文档同样是写入一次数据。结论此时两者效率几乎无差异均为一次写入操作性能取决于数据大小和集群负载。当文档已存在时处理已有文档index 操作执行「覆盖式更新」—— 先删除旧文档再插入新文档本质是 delete create 的组合。此过程会生成新的文档版本旧文档被标记为删除后续由段合并清理但操作逻辑简单仅需处理一次完整文档写入。upsert 操作执行「增量更新」—— 仅更新指定字段保留未修改的原有字段本质是 update 逻辑。此过程需要先读取旧文档的内容合并新字段后再写入涉及「读 写」两次 I/O 操作即使启用了 retry_on_conflict 重试机制逻辑更复杂。结论index 效率通常高于 upsert因为 upsert 多了一次读取旧文档的开销尤其在文档体积较大或更新频繁时差距更明显。在 Hive 与 ElasticsearchES的关联场景中默认情况下删除 Hive 表不会影响 ES 数据但存在一种特殊情况可能导致删除 Hive 表时间接删除 ES 数据仅当满足以下条件时删除 Hive 表可能触发 ES 数据删除Hive 表使用 ESStorageHandler 且配置了 es.index.auto.create: false即 ES 索引由 Hive 表创建并强绑定索引不存在时不会自动创建需通过 Hive 表定义关联已存在的索引。Hive 表是 ES 索引的唯一 “管理入口” 且删除操作被二次开发拦截某些业务场景中可能通过自定义脚本或工具对 Hive 的 DROP TABLE 操作进行拦截在删除 Hive 表的同时自动调用 ES 的删除 API如 DELETE /index删除对应的 ES 索引。这种情况属于人为定制的联动逻辑而非 Hive 或 ES 的原生功能。注意原生机制下无联动删除Hive 与 ES 本身是独立系统Hive 的 DROP TABLE 命令仅操作 Hive 元数据和自身存储如 HDFS不会主动通知 ES 删除数据。ES 数据删除依赖显式操作若需删除 ES 数据需单独通过 ES 的 API、Kibana 或工具执行删除索引 / 文档的操作。因此删除 Hive 表导致 ES 数据删除的情况仅可能出现在人为定制的联动逻辑中而非默认行为。在 Hive 中external.table.purge 是一个表属性参数主要用于控制删除 Hive 外部表时是否同时删除外部存储系统中的数据如 HDFS、Elasticsearch、S3 等外部存储的数据。参数含义与作用参数值TRUE 或 FALSE默认通常为 FALSE。核心逻辑当 external.table.purge TRUE 时删除 Hive 外部表DROP TABLE时Hive 会尝试删除外部存储中对应的数据如 HDFS 上的文件、ES 中的索引等具体取决于外部存储类型。当 external.table.purge FALSE 时默认删除 Hive 外部表仅会移除 Hive 的元数据表定义不会影响外部存储中的实际数据。注意事项外部表的本质Hive 外部表EXTERNAL TABLE的设计初衷是 “数据独立于 Hive 管理”即数据存储在 Hive 之外的系统如 HDFS、ES、S3Hive 仅维护表结构的元数据。因此默认情况下purgeFALSE删除外部表不会触碰外部数据避免误删重要数据。兼容性与局限性该参数的效果依赖于外部存储系统的支持。例如对于 HDFS 上的外部表purgeTRUE 会删除 HDFS 路径下的数据文件。对于 Elasticsearch 关联的外部表purgeTRUE 理论上可能尝试删除 ES 索引但实际是否生效取决于 Hive 与 ES 的连接器如elasticsearch-hadoop是否支持该操作且需确保 Hive 有 ES 的删除权限通常不建议依赖此参数删除 ES 数据风险较高。部分外部存储如关系型数据库、Kafka可能完全不支持通过该参数删除数据此时purgeTRUE可能无效。风险提示生产环境中不建议轻易设置 external.table.purgeTRUE尤其是关联重要外部数据如 ES 索引、生产库表时可能因误删 Hive 表导致外部数据丢失且难以恢复。若需删除外部存储的数据建议通过外部系统自身的工具或 API如 ES 的DELETE /index、HDFS 的hdfs dfs -rm显式操作更安全可控。总结external.table.purge 是 Hive 为外部表提供的 “元数据与外部数据联动删除” 开关默认关闭不删除外部数据。使用时需谨慎尤其是关联非 HDFS 的外部存储如 ES时其效果可能不稳定且存在数据误删风险建议优先通过外部系统自身工具管理数据生命周期。curl-u elastic:Passw0rd-XGET xxx.xx.xxx.xxx:9200/_cat/indices?v--查看所有索引 curl-u elastic:Passw0rd-XGET http://xxx.xxx.xxx.xxx:9200/kgl_test?pretty#查看指定索引的信息curl-u elastic:Passw0rd-XGET xxx.xx.xxx.xxx:9200/group_tag_all_boci_test?prettytruevsindex--查看指定索引结构信息 curl-u elastic:Passw0rd-XGET xxx.xx.xxx.xxx:9200/group_tag_all_boci_test?pretty--查看指定索引结构信息--所有数据 curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_test/_search?pretty-HContent-Type: application/json-d{query: {match_all:{}}}--删除索引 curl-u elastic:Passw0rd-XDELETE xxx.xx.xxx.xxx:9200/group_tag_all_boci_test--指定客户所有标签名称 curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_search?pretty-HContent-Type: application/json-d{query: {match_phrase: {client_id: 12953399}}}--指定客户及标签名称 curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_search?pretty-HContent-Type: application/json-d{query: {match_phrase: {client_id: 12953399}} ,_source:[client_id,client_membership_level,mbr_level,old_count_mbr_level,new_count_mbr_level,client_mbr_assets]}--指定条件数据数量 curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_count?pretty-HContent-Type: application/json-d{query: {match_phrase: {client_acted_task: HDRW0988}}}curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci/_count?pretty-HContent-Type: application/json-d{query: {match_phrase: {client_acted_task.keyword: HDRW1393}}}curl-k-u elastic:Passw0rd-XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci/_count?pretty-HContent-Type: application/json-d{query: {regexp: {client_acted_task.keyword: {value: .*HDRW1393.*}}}}--指定条件数据 curl-k-u elastic:Passw0rd-XPOST http://localhost:9200/kgl_test/_search?pretty-HContent-Type: application/json-d{query: {match_phrase: {client_birthday_year: 2025}}}另外重建索引时需要设置 client_exchanged_goods,client_acted_task,client_joined_active 字段keyword类型的最大长度为10240以支持这几个字段的模糊匹配查询。所以需要执行一下语句(等导出完成后执行)curl-k-u elastic:Passw0rd-HContent-Type: application/json-XPUThttp://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_mapping-d{properties:{client_acted_task:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},stock_hldp_list:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},client_exchanged_goods:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}},client_joined_active:{type:text,fields:{keyword:{type:keyword,ignore_above:10240}}}}}curl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_search?pretty -H ‘Content-Type: application/json’ -d’{“query”: {“match_phrase”: {“client_id”: “111000204”}} ,“_source”:[“client_id”,“client_membership_level”,“mbr_level”,“old_count_mbr_level”,“new_count_mbr_level”,“client_mbr_assets”]}’查询一个用户某几个标签的样例_source下需要填你想查的标签curl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_search?pretty -H‘Content-Type: application/json’ -d’{“version”:true,“query”: {“match_phrase”: {“client_id”: “31107721”}} ,“_source”:[“client_id”,“client_membership_level”,“mbr_level”,“old_count_mbr_level”,“new_count_mbr_level”,“client_mbr_assets”]}’查询一个用户某几个标签的样例_source下需要填你想查的标签及对应的版本curl -k -u elastic:Passw0rd -XGET http://IP地址:端口/group_tag_all_boci_sc/_count?pretty -H ‘Content-Type: application/json’ -d’{“query”: { “bool”: { “must”: { “exists”: {“field”: “open_date”}}}}}’统计存在某个标签的 总数据量删除索引数据curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_delete_by_query?pretty’ -H ‘Content-Type: application/json’ -d ‘{“query”:{“match_all”:{}}}’删除指定数据 如53051459用户的所有标签curl -k -u elastic:Passw0rd -XDELETE http://xxx.xx.xxx.xxx:9200/group_tag_all_boci/_doc/53051459curl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_count查总数居量假设新的索引命名为group_tag_all_boci_sccd到see_job/create/create_table_group_tag_all_boci_es.sh脚本所在目录执行以下命令即可将ES表索引更改为group_tag_all_boci_tmpsh create_table_group_tag_all_boci_es.sh 20220819 20220819 st_export_boci ‘xxx.xx.xxx.xxx:9200,xxx.xx.248.173:9200,xxx.xx.248.174:9200’ ‘2OVFNSadprKn’ ‘r#dcenter9’ ‘group_tag_all_boci_sc’2.2执行导出命令另外重建索引时需要设置 client_exchanged_goods,client_acted_task,client_joined_active 字段keyword类型的最大长度为10240以支持这几个字段的模糊匹配查询。所以需要执行一下语句(等导出完成后执行)curl -k -u elastic:Passw0rd -H “Content-Type: application/json” -XPUT “http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_mapping” -d ‘{“properties”:{“client_acted_task”:{“type”:“text”,“fields”:{“keyword”:{“type”:“keyword”,“ignore_above”:10240}}},“stock_hldp_list”:{“type”:“text”,“fields”:{“keyword”:{“type”:“keyword”,“ignore_above”:10240}}},“client_exchanged_goods”:{“type”:“text”,“fields”:{“keyword”:{“type”:“keyword”,“ignore_above”:10240}}},“client_joined_active”:{“type”:“text”,“fields”:{“keyword”:{“type”:“keyword”,“ignore_above”:10240}}}}}’curl -k -u elastic:Boci2022 http://10.23.69.101:9200/_aliases -H ‘Content-Type:application/json’ -X POST -d ‘{“actions”:[{“remove”:{“index”:“test”,“alias”: “test-new”}}, {“add”:{“index”:“test1”,“alias”: “test-new”}}]}’es 改索引别名– 修改别名curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/_aliases’ -H ‘Content-Type: application/json’ -d’{“actions”: [{“add”: {“index”: “group_tag_all_boci_test_main”, “alias”: “group_tag_all_boci_test”}}]}’curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/_aliases’ -H ‘Content-Type: application/json’ -d’{“actions”: [{“add”: {“index”: “group_tag_all_boci_test_back”, “alias”: “group_tag_all_boci_test”}}]}’curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/_aliases’ -H ‘Content-Type: application/json’ -d’{“actions”: [{“remove”: {“index”: “group_tag_all_boci_test_main”,“alias”: “group_tag_all_boci_test”}},{“add”: {“index”: “group_tag_all_boci_test_back”,“alias”: “group_tag_all_boci_test”}}]}’–删除索引数据curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_test_main/_delete_by_query?pretty’ -H ‘Content-Type: application/json’ -d ‘{“query”:{“match_all”:{}}}’–更新curl -u elastic:Passw0rd -XPOST ‘http://xxx.xx.xxx.xxx:9200/_aliases’ -H ‘Content-Type: application/json’ -d’{“actions”: [{“remove”: {“index”: “group_tag_all_boci_test_back”,“alias”: “group_tag_all_boci_test”}},{“add”: {“index”: “group_tag_all_boci_test_main”,“alias”: “group_tag_all_boci_test”}}]}’索引group_tag_all_boci_test_main数据到备份索引group_tag_all_boci_test_backcurl -u elastic:Passw0rd -POST ‘http://xxx.xx.xxx.xxx:9200/_reindex’ -H ‘Content-Type: application/json’ -d’{“source”: {“index”: “group_tag_all_boci_test_main”},“dest”: {“index”: “group_tag_all_boci_test_back”}}’–查看索引内容curl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_test_main/_search?pretty -H ‘Content-Type: application/json’ -d’{“query”: {“match_all”:{}}}’insert overwrite table tmp.group_tag_all_boci_test_mainselect ‘1001’ as client_id,‘kgl1’ as name,101 as ageunion allselect ‘10011’ as client_id,‘kgl11’ as name,1011 as age;insert overwrite table tmp.group_tag_all_boci_test_backselect ‘1002’ as client_id,‘kgl2’ as name,102 as ageunion allselect ‘10022’ as client_id,‘kgl22’ as name,1022 as age;–查看所有索引curl -u elastic:Passw0rd -XGET xxx.xx.xxx.xxx:9200/_cat/indices?vcurl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_test_back/_search?pretty -H ‘Content-Type: application/json’ -d’{“query”: {“match_phrase”: {“client_name”: “徐x”}}}’curl -u elastic:apexsoft -XGET xxx.xx.202.146:9200/_cat/indices?vcurl -k -u elastic:apexsoft -XPOST http://xxx.xx.202.146:9200/customer_v2020/customer_info/_search?pretty -H ‘Content-Type: application/json’ -d’{“query”: {“match_all”:{}}}’curl -k -u elastic:apexsoft -XPOST http://xxx.xx.202.146:9200/customer_v2020/customer_info/_search?pretty -H ‘Content-Type: application/json’ -d’{“query”: {“match_phrase”: {“customer_no”: “11001737”}} ,“_source”:[“customer_id”,“become_due_point_amount”]}’

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价