弹性搜索 - 仅存储字段

Elastic Search - store only field

弹性搜索中是否有一个选项可以存储值仅用于检索目的而不用于搜索?因此,在索引时我们将索引所有字段,而在搜索时我们将仅搜索单个字段,但也需要其他数据。 比如我们要对产品进行索引,字段可以是Name、SKU、Supplier Name等,其中只需要对Name进行索引和搜索。 SKU 和 Supplier Name 仅用于存储和通过搜索检索。

由于无论如何都会存储 _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 只应在 top-level 或 object 字段中使用,因此此处没有它的位置

有了上面的映射,就可以

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