弹性搜索 - 仅存储字段

赛伯

弹性搜索中是否有一个选项来存储值,仅用于检索而不用于搜索?因此,在建立索引时,我们将索引所有字段,而在搜索时,我们将仅搜索单个字段,但还需要其他数据。例如,我们将索引产品,字段可以是名称、SKU、供应商名称等。其中,只有名称需要被索引和搜索。SKU 和供应商名称仅用于通过搜索存储和检索。

瓦尔

由于_source无论如何都会存储文档,因此实现您想要的最佳方法是既不存储也不索引任何字段,除了您正在搜索的字段,如下所示:

PUT my-index
{
  "mappings": {
    "_source": {
      "enabled": true           <--- true by default, but adding for completeness
    },
    "properties": {
      "name": {
        "type": "text",
        "index": true           <--- true by default, but adding for completeness
      },
      "sku": {
        "type": "keyword",
        "index": false,         <--- don't index this field
        "store": false          <--- false by default, but adding for completeness
      },
      "supplier": {
        "type": "keyword",
        "index": false,         <--- don't index this field
        "store": false          <--- false by default, but adding for completeness
      },
    }
  }
}

所以总结一下:

  • 您要搜索的字段必须有index: true
  • 您不想搜索的字段必须有index: false
  • store默认为 false,因此您无需指定它
  • _source默认启用,因此您无需指定它
  • enabled应该只在顶级或object字段上使用,所以它在这里没有它的位置

通过上面的映射,你可以

  • 搜索name
  • _source从文档中检索所有字段,因为该_source字段默认存储并包含原始文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章