ES查询 索引别名
2026/8/10 22:09:35 网站建设 项目流程

新增es索引及字段,设置最大长度为10240,以支持字段的模糊匹配查询

curl-k-u elastic:Passw0rd-H"Content-Type: application/json"-XPUT"http://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-H"Content-Type: application/json"-XPUT"http://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 stringcomment'khh',name stringcomment'姓名',age stringcomment'年龄')ROWFORMAT SERDE'org.elasticsearch.hadoop.hive.EsSerDe'STOREDBY'org.elasticsearch.hadoop.hive.EsStorageHandler'TBLPROPERTIES('es.index.read.missing.as.empty'='true',-- 缺失索引读为空(去重)'es.field.read.empty.as.null'='true',-- 空字段读为NULL'es.mapping.id'='client_id',-- 文档唯一ID,必选'es.mapping.routing'='client_id',--路由值,控制数据分片位置 高并发、大数据量读写 可能出现数据倾斜,热点分片'es.write.operation'='upsert',-- 存在则更新,不存在则插入-- 'es.write.dynamic'='strict', -- 严格字段映射,避免类型不匹配'es.net.http.auth.user'='elastic',-- 认证用户'es.net.http.auth.pass'='Passw',-- 认证密码'es.nodes'='xxx.xx.xxx.xxx:9200,xxx.xx.248.1:9200,xxx.xx.248.2:9200',-- 集群节点'es.index.auto.create'='true',-- 自动创建索引(生产建议提前创建)'es.nodes.wan.only'='true',-- 跨网段/WAN环境必开'es.read.metadata'='true','es.resource'='group_tag_all_boci_test_main/_doc',-- 索引/类型'es.index.read.missing.as.empty'='yes','es.batch.write.concurrent.requests'='2',--调低,总并发可控'es.refresh'='false',--写入阶段关闭refresh,提升吞吐'es.input.max.docs.per.split'='1000000'-- 在读取数据源(Hive ORC)的时候,做逻辑分片,限制每一个map split最多处理多少行文档'es.transport.pool.size'='30',-- 连接池大小(匹配节点数和并行度)'es.batch.size.bytes'='2097152',-- 单批大小(默认1MB,推荐写法)-- 'es.batch.size.bytes' = '2mb', -- 单批大小(默认1MB es大小写敏感)'es.batch.size.entries'='2000',-- 单批条目(不写默认1000,提升吞吐量)-- 高版本ES适配(若集群>7.x)-- 'es.nodes.discovery'='false', -- 关闭自动发现,强制用配置节点-- 'es.net.ssl.enabled'='true', -- 若启用SSL,需同步开启(你的原配置有ssl协议)-- 'es.net.ssl.protocol'='TLSv1.2' -- 明确SSL协议版本'es.net.ssl.protocol'='ssl');

–如果想保存es重复数据

droptableifexiststmp.group_tag_all_boci_test_main;createtabletmp.group_tag_all_boci_test_main(client_id stringcomment'khh',name stringcomment'姓名',age stringcomment'年龄')ROWFORMAT SERDE'org.elasticsearch.hadoop.hive.EsSerDe'STOREDBY'org.elasticsearch.hadoop.hive.EsStorageHandler'TBLPROPERTIES('es.index.read.missing.as.empty'='true','es.field.read.empty.as.null'='true',-- 移除文档ID映射(或改为非唯一字段),避免ES按固定ID去重-- 原配置:'es.mapping.id'='client_id',-- 'es.mapping.routing'='client_id', --路由值,控制数据分片位置 高并发、大数据量读写'es.write.operation'='index',-- 保持普通插入,不触发更新逻辑'es.net.http.auth.user'='elastic','es.net.http.auth.pass'='Passw0rd','es.nodes'='xxx.xx.xxx.xxx:9200,xxx.xx.248.1:9200,xxx.xx.248.2:9200','es.index.auto.create'='true','es.nodes.wan.only'='true','es.read.metadata'='true','es.resource'='group_tag_all_boci_test_main/_doc','es.nodes.wan.only'='true','es.index.read.missing.as.empty'='yes','es.batch.write.concurrent.requests'='2',--调低,总并发可控'es.refresh'='false',--写入阶段关闭refresh,提升吞吐'es.input.max.docs.per.split'='1000000'-- 在读取数据源(Hive ORC)的时候,做逻辑分片,限制每一个map split最多处理多少行文档'es.transport.pool.size'='30',-- 连接池大小(匹配节点数和并行度)'es.net.ssl.protocol'='ssl');
'es.write.operation'='upsert''es.write.operation'='index'--表已创建-- 示例1:将写入逻辑改为index(全量覆盖)ALTERTABLEjgcrm_t_corp_dd_to_esSETTBLPROPERTIES('es.write.operation'='index');-- 示例2:将写入逻辑改为upsert(增量更新)ALTERTABLEjgcrm_t_corp_dd_to_esSETTBLPROPERTIES('es.write.operation'='upsert');--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 操作的插入效率(针对「文档不存在时的创建场景」)基本接近,但在「文档已存在时的处理场景」中,两者的效率差异较明显,主要取决于操作逻辑的复杂度:

  1. 当文档不存在时(创建新文档)
    index 操作:直接创建新文档(本质是 create 逻辑),仅需写入一次数据。
    upsert 操作:因 _id 不存在,执行预定义的「插入逻辑」(如使用 upsert 模板创建文档),同样是写入一次数据。
    结论:此时两者效率几乎无差异,均为一次写入操作,性能取决于数据大小和集群负载。
  2. 当文档已存在时(处理已有文档)
    index 操作:执行「覆盖式更新」—— 先删除旧文档,再插入新文档(本质是 delete + create 的组合)。此过程会生成新的文档版本,旧文档被标记为删除(后续由段合并清理),但操作逻辑简单,仅需处理一次完整文档写入。
    upsert 操作:执行「增量更新」—— 仅更新指定字段,保留未修改的原有字段(本质是 update 逻辑)。此过程需要先读取旧文档的内容,合并新字段后再写入,涉及「读 + 写」两次 I/O 操作(即使启用了 retry_on_conflict 重试机制,逻辑更复杂)。
    结论:index 效率通常高于 upsert,因为 upsert 多了一次读取旧文档的开销,尤其在文档体积较大或更新频繁时,差距更明显。

在 Hive 与 Elasticsearch(ES)的关联场景中,默认情况下删除 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、S3),Hive 仅维护表结构的元数据。因此,默认情况下(purge=FALSE),删除外部表不会触碰外部数据,避免误删重要数据。
兼容性与局限性:
该参数的效果依赖于外部存储系统的支持。例如:
对于 HDFS 上的外部表:purge=TRUE 会删除 HDFS 路径下的数据文件。
对于 Elasticsearch 关联的外部表:purge=TRUE 理论上可能尝试删除 ES 索引,但实际是否生效取决于 Hive 与 ES 的连接器(如elasticsearch-hadoop)是否支持该操作,且需确保 Hive 有 ES 的删除权限(通常不建议依赖此参数删除 ES 数据,风险较高)。
部分外部存储(如关系型数据库、Kafka)可能完全不支持通过该参数删除数据,此时purge=TRUE可能无效。
风险提示:
生产环境中不建议轻易设置 external.table.purge=TRUE,尤其是关联重要外部数据(如 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?pretty=true&v&s=index--查看指定索引结构信息 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-H'Content-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-H'Content-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-H'Content-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-H'Content-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-H'Content-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-H'Content-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-H'Content-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-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: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/53051459

curl -k -u elastic:Passw0rd -XPOST http://xxx.xx.xxx.xxx:9200/group_tag_all_boci_sc/_count
查总数居量

假设新的索引命名为group_tag_all_boci_sc
cd到see_job/create/create_table_group_tag_all_boci_es.sh脚本所在目录,执行以下命令,即可将ES表索引更改为group_tag_all_boci_tmp
sh 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_back
curl -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_main
select ‘1001’ as client_id,‘kgl1’ as name,101 as age
union all
select ‘10011’ as client_id,‘kgl11’ as name,1011 as age;

insert overwrite table tmp.group_tag_all_boci_test_back
select ‘1002’ as client_id,‘kgl2’ as name,102 as age
union all
select ‘10022’ as client_id,‘kgl22’ as name,1022 as age;

–查看所有索引
curl -u elastic:Passw0rd -XGET xxx.xx.xxx.xxx:9200/_cat/indices?v

curl -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?v

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_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”]}’

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询