首页
看点啥
插画图片
首页 科技看点 Elasticsearch 未做字段映射时怎么查询?

Elasticsearch 未做字段映射时怎么查询?

2026-11-26 0

先检查字段是否出现在映射中,再决定如何查。对只保存在 _source 的字段,普通 term 和 exists 都给不出相应命中。下面用 Elasticsearch 9.4.9 跑四种情况,分别观察取值、筛选和历史数据重新索引。

把每一节的请求按顺序粘贴到 Kibana 的 Dev Tools → Console,索引名须尚未使用。四个实验都写入同一组文档:ID 1 的 region 是 east,ID 2 省略 region。代码中以 # 开头的行记录响应摘录,ids 是从 hits.hits 提取的文档 ID。

1. 直接查询未映射字段

设置 dynamic: false,让 region 保留在 _source,同时禁止它自动进入映射。

PUT /field-lab-source
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    },
    "dynamic": false
  }
}

PUT /field-lab-source/_doc/1?refresh=wait_for
{
  "name": "first",
  "region": "east"
}

PUT /field-lab-source/_doc/2?refresh=wait_for
{
  "name": "second"
}

GET /field-lab-source/_mapping
# {"region_mapped": false}

GET /field-lab-source/_doc/1
# {"_source": {"name": "first", "region": "east"}}

GET /field-lab-source/_search
{
  "query": {
    "term": {
      "region": "east"
    }
  }
}
# {"ids": []}

GET /field-lab-source/_search
{
  "query": {
    "exists": {
      "field": "region"
    }
  }
}
# {"ids": []}

GET /field-lab-source/_search
{
  "query": {
    "term": {
      "name": "first"
    }
  },
  "_source": false,
  "fields": [
    {
      "field": "region",
      "include_unmapped": true
    }
  ]
}
# {"ids": ["1"], "fields": {"region": ["east"]}}

GET 文档能看到 east,但 term 和 exists 的命中列表都为空。最后一条请求先按已映射的 name 命中文档,再用 fields 的 include_unmapped 取回 region。这个选项只影响响应里的字段。

2. 让动态映射接管新字段

新建索引时省略 dynamic,让默认的动态映射为首次见到的字符串创建 text 和 keyword 子字段。

PUT /field-lab-dynamic
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    }
  }
}

PUT /field-lab-dynamic/_doc/1?refresh=wait_for
{
  "name": "first",
  "region": "east"
}

PUT /field-lab-dynamic/_doc/2?refresh=wait_for
{
  "name": "second"
}

GET /field-lab-dynamic/_mapping
# {"region_keyword": "keyword"}

GET /field-lab-dynamic/_search
{
  "query": {
    "term": {
      "region.keyword": "east"
    }
  }
}
# {"ids": ["1"]}

GET /field-lab-dynamic/_search
{
  "query": {
    "exists": {
      "field": "region"
    }
  }
}
# {"ids": ["1"]}

命中文档 1 时,region.keyword 已经在映射里。真实索引可能使用模板覆盖默认规则,选查询字段前先看 GET _mapping。

3. 在请求中定义运行时字段

在 runtime_mappings 里临时声明同名 keyword 字段,从 _source 读取字符串,缺少值时跳过 emit。

PUT /field-lab-runtime
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    },
    "dynamic": false
  }
}

PUT /field-lab-runtime/_doc/1?refresh=wait_for
{
  "name": "first",
  "region": "east"
}

PUT /field-lab-runtime/_doc/2?refresh=wait_for
{
  "name": "second"
}

GET /field-lab-runtime/_search
{
  "runtime_mappings": {
    "region": {
      "type": "keyword",
      "script": {
        "source": "def v = params['_source']['region']; if (v instanceof String) emit(v);"
      }
    }
  },
  "query": {
    "term": {
      "region": "east"
    }
  }
}
# {"ids": ["1"]}

GET /field-lab-runtime/_mapping
# {"region_mapped": false}

这次 term 命中文档 1,索引映射里仍没有 region。常规索引字段可以先缩小候选集,再计算运行时字段。运行时查询要付出读取和计算开销,集群关闭昂贵查询时会拒绝这类请求。

4. 补上映射并重建索引

先向旧索引添加 region 映射,再把同一批文档写入已配置好 keyword 映射的新索引。

PUT /field-lab-history
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    },
    "dynamic": false
  }
}

PUT /field-lab-history/_doc/1?refresh=wait_for
{
  "name": "first",
  "region": "east"
}

PUT /field-lab-history/_doc/2?refresh=wait_for
{
  "name": "second"
}

PUT /field-lab-history/_mapping
{
  "properties": {
    "region": {
      "type": "keyword"
    }
  }
}

GET /field-lab-history/_search
{
  "query": {
    "term": {
      "region": "east"
    }
  }
}
# {"ids": []}

PUT /field-lab-history-new
{
  "mappings": {
    "dynamic": false,
    "properties": {
      "name": {
        "type": "keyword"
      },
      "region": {
        "type": "keyword"
      }
    }
  }
}

POST /_reindex?refresh=true
{
  "source": {
    "index": "field-lab-history"
  },
  "dest": {
    "index": "field-lab-history-new"
  }
}
# {"created": 2, "failures": []}

GET /field-lab-history-new/_search
{
  "query": {
    "term": {
      "region": "east"
    }
  }
}
# {"ids": ["1"]}

只执行 PUT _mapping 后,旧索引里还没有可供搜索的 region 值。将文档按新定义写进目标索引后,文档 1 才能按 east 命中。迁移时应核对失败项与文档数量。

同一组文档的查询结果:直接查询和只补映射都无命中,运行时字段和重建索引都命中文档 1
实测结果摘录。四种方法使用相同输入,命中列表来自对应请求。

临时排查先确认原始字段值,然后视需要用运行时字段过滤。需要频繁用该字段筛选时,声明合适的映射,并把历史文档按新定义重新索引。示例使用 _search 的 Query DSL。

喜欢(0)

上一篇

VS Code归档设置改名后怎么迁移旧天数并保留新值

VS Code归档设置改名后怎么迁移旧天数并保留新值

下一篇

Elasticsearch 精确匹配 keyword 字段时,match 与 term 怎么选?

Elasticsearch 精确匹配 keyword 字段时,match 与 term 怎么选?
猜你喜欢