Elasticsearch中多索引数据合并与Nested嵌套类型操作全解析

多索引数据合并操作,以及嵌套类型(nested)的相关操作指南。

Elasticsearch中多索引数据合并与Nested嵌套类型操作全解析

合并多索引数据到指定索引

现在需要将index_name-2025q1、index_name-2025q2、index_name-2025q3、index_name-2025q4这4个索引的数据合并到index_name-2025这一个索引里,可用如下操作

elm 复制代码
POST _reindex
{
  "source": {
    "index": "index_name-2025q*"
  },
  "dest": {
    "index": "index_name-2025"
  }
}

嵌套类型(nested)操作指南

一、数据结构说明

json 复制代码
{
    "mappings": {
        "properties": {
	          "id": {
                "type": "keyword"
            },
            "orderId": {
                "type": "keyword"
            },
            "products": {
                "type": "nested",
                "properties": {
                  	"productName": {
                        "type": "text"
                    },
                    "productPrice": {
                        "type": "scaled_float",
                        "scaling_factor": 100
                    },
                    "productQuantity": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

这个数据结构里的products是一个典型的嵌套数据类型(nested)结构,包含了一个text类型的productName,浮点类型的productPrice和一个整数型的productQuantity。

二、基本CRUD操作

1. 插入文档
elm 复制代码
POST /your_index/_doc/1
{
	"id": 1,
  "orderId": "20250528000001",
  "products": [
    {
    	"productName": "手机",
      "productPrice": 5123.45,
      "productQuantity": 1
    },
    {
    	"productName": "钢化膜",
      "productPrice": 53.42,
      "productQuantity": 2
    }
  ]
}
2. 更新嵌套字段
elm 复制代码
POST /your_index/_update/1
{
  "script": {
    "source": """
      // 修改productName为'钢化膜'的记录的productPrice
      for(int i=0; i<ctx._source.products.length; i++) {
        if(ctx._source.products[i].productName == params.productName) {
          ctx._source.products[i].productPrice = params.productPrice;
          break;
        }
      }
    """,
    "params": {
      "productName": "钢化膜",
      "productPrice": 130.50
    }
  }
}
3. 删除嵌套数组中的特定元素
elm 复制代码
POST /your_index/_update/1
{
  "script": {
    "source": """
      // 删除productName为'钢化膜'的记录
      ctx._source.products.removeIf(item -> item.productName == params.productName);
    """,
    "params": {
      "productName": "钢化膜"
    }
  }
}

三、查询操作

1. 基础嵌套查询
elm 复制代码
GET /your_index/_search
{
  "query": {
    "nested": {
      "path": "products",
      "query": {
        "bool": {
          "must": [
            { "match": { "products.productName": "钢材" }},
            { "range": { "products.productPrice": { "gte": 100 }}}
          ]
        }
      }
    }
  }
}
2. 嵌套聚合分析
elm 复制代码
GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "material_analysis": {
      "nested": {
        "path": "products"
      },
      "aggs": {
        "by_stuff": {
          "terms": {
            "field": "products.productName",
            "size": 10
          },
          "aggs": {
            "avg_fact": {
              "avg": {
                "field": "products.productPrice"
              }
            }
          }
        }
      }
    }
  }
}
3. 多条件嵌套查询
elm 复制代码
GET /your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "products",
            "query": {
              "bool": {
                "must": [
                  { "term": { "products.productName": "手机" }},
                  { "range": { "products.productPrice": { "gt": 150 }}}
                ]
              }
            }
          }
        },
        {
          "match": { "otherField": "value" }
        }
      ]
    }
  }
}

四、特殊操作

1. 获取嵌套字段的内联命中
elm 复制代码
GET /your_index/_search
{
  "query": {
    "nested": {
      "path": "products",
      "query": {
        "match": { "products.productName": "钢化膜" }
      },
      "inner_hits": {}  // 获取匹配的嵌套对象
    }
  }
}
2. 嵌套排序
elm 复制代码
GET /your_index/_search
{
  "sort": [
    {
      "products.productPrice": {
        "order": "desc",
        "nested": {
          "path": "products",
          "filter": {
            "term": { "products.productName": "钢化膜" }
          }
        }
      }
    }
  ]
}

五、性能优化建议

  1. 合理设置 nested 字段数量 :嵌套文档会显著增加索引大小

  2. **使用 include_in_parent ** :如需频繁查询父文档字段

    json 复制代码
    "products": {
      "type": "nested",
      "include_in_parent": true,
      "properties": {...}
    }
  3. 控制嵌套文档大小 :单个文档的嵌套数组不宜过大(建议<100个元素)

  4. 使用 scaled_float 的优势 :比普通 float 更节省空间,适合有明确精度的数值

相关推荐
做cv的小昊1 天前
【TJU】信息检索与分析课程笔记和练习(8)(9)发现系统和全文获取、专利与知识产权基本知识
大数据·笔记·学习·全文检索·信息检索
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-01-09
人工智能·经验分享·神经网络·搜索引擎·产品运营
Cx330❀1 天前
Git 多人协作全攻略:从入门到高效协同
大数据·elasticsearch·搜索引擎·gitee·github·全文检索·gitcode
bigHead-2 天前
Git合并操作详解:安全高效地合并远程分支
git·安全·elasticsearch
奇树谦2 天前
FastDDS阿里云DDSRouter安装和使用(失败)
elasticsearch·阿里云·云计算
色空大师2 天前
服务打包包名设置
java·elasticsearch·maven·打包
TOPGUS2 天前
谷歌Chrome浏览器即将对HTTP网站设卡:突出展示“始终使用安全连接”功能
前端·网络·chrome·http·搜索引擎·seo·数字营销
AC赳赳老秦2 天前
Unity游戏开发实战指南:核心逻辑与场景构建详解
开发语言·spring boot·爬虫·搜索引擎·全文检索·lucene·deepseek
码农很忙2 天前
从0到1搭建实时日志监控系统:基于WebSocket + Elasticsearch的实战方案
websocket·网络协议·elasticsearch
Elastic 中国社区官方博客2 天前
在 ES|QL 中的混合搜索和多阶段检索
大数据·人工智能·sql·elasticsearch·搜索引擎·ai·全文检索