You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch创建含long类型并初始化字段的索引语法求助

问题原因与正确实现方案

你当前的写法错误在于:把字段类型定义直接放在了文档数据里,Elasticsearch会将{"type": "long"}识别成普通的嵌套文档内容,导致spec_label被自动推断为text类型,而非你想要的long类型。

要实现需求,需要先创建索引并定义字段映射,再插入带初始值的文档,步骤如下:

1. 创建索引并配置字段映射

执行以下PUT请求,显式定义索引的字段类型,确保spec_label为long类型:

PUT /highlighttest2
{
  "mappings": {
    "properties": {
      "Id": {"type": "keyword"},
      "Reference": {"type": "date"},
      "Title": {"type": "text"},
      "Description": {"type": "text"},
      "DescriptionHtml": {"type": "text"},
      "Organisation": {"type": "keyword"},
      "myNested": {
        "type": "nested",
        "properties": {
          "spec_label": {"type": "long"}
        }
      }
    }
  }
}
  • myNested设为nested类型(如果不需要嵌套结构,可改为object类型)
  • spec_label明确指定为long类型,避免自动推断错误

2. 插入带初始值的文档

索引创建完成后,执行以下POST请求插入文档,将spec_label的初始值设为1:

POST /highlighttest2/_doc/1
{
  "Id": "1",
  "Reference": "2012-01-01",
  "Title": "Sales Manager",
  "Description": "We are looking for an organized Project Administrator to perform Commercial duties on projects. The Project Administrator is responsible for scheduling meetings, managing office inventory, and tracking expenses. To be successful as a Project Administrator you must be able to multitask. A good Project Administrator has excellent attention to detail.",
  "DescriptionHtml": "<section><p>We are looking for an organized Project Administrator to perform clerical duties on projects.</p> <p>The Project Administrator is responsible for scheduling meetings, managing office inventory, and tracking expenses. To be successful as a Project Administrator you must be able to multitask.</p> <p>A good Project Administrator has excellent attention to detail.</p></section>",
  "Organisation": "US4",
  "myNested": {
    "spec_label": 1
  }
}

验证配置结果

执行以下请求查看字段映射,确认spec_label的类型是否正确:

GET /highlighttest2/_mapping/field/myNested.spec_label

返回结果中会显示type: "long",说明配置生效。


内容的提问来源于stack exchange,提问作者Timothy Clotworthy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:17:40