store

编辑

默认情况下,字段值会被索引以便于搜索,但它们不会被存储。这意味着可以查询该字段,但无法检索原始字段值。

通常这无关紧要。字段值已经包含在_source字段中,该字段默认情况下会被存储。如果您只想检索单个字段或几个字段的值,而不是整个_source,那么可以使用源过滤来实现。

在某些情况下,store 字段是有意义的。例如,如果您的文档包含 titledate 和一个非常大的 content 字段,您可能只想检索 titledate,而不必从大型的 _source 字段中提取这些字段。

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "title": {
                "type": "text",
                "store": True
            },
            "date": {
                "type": "date",
                "store": True
            },
            "content": {
                "type": "text"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="1",
    document={
        "title": "Some short title",
        "date": "2015-01-01",
        "content": "A very long content field..."
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    stored_fields=[
        "title",
        "date"
    ],
)
print(resp2)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        title: {
          type: 'text',
          store: true
        },
        date: {
          type: 'date',
          store: true
        },
        content: {
          type: 'text'
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    title: 'Some short title',
    date: '2015-01-01',
    content: 'A very long content field...'
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    stored_fields: [
      'title',
      'date'
    ]
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      title: {
        type: "text",
        store: true,
      },
      date: {
        type: "date",
        store: true,
      },
      content: {
        type: "text",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    title: "Some short title",
    date: "2015-01-01",
    content: "A very long content field...",
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  stored_fields: ["title", "date"],
});
console.log(response2);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "store": true 
      },
      "date": {
        "type": "date",
        "store": true 
      },
      "content": {
        "type": "text"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "title":   "Some short title",
  "date":    "2015-01-01",
  "content": "A very long content field..."
}

GET my-index-000001/_search
{
  "stored_fields": [ "title", "date" ] 
}

titledate 字段被存储了。

此请求将检索 titledate 字段的值。

存储的字段作为数组返回

为了保持一致性,存储的字段始终以数组的形式返回,因为无法知道原始字段值是单个值、多个值还是空数组。

如果您需要原始值,则应从 _source 字段中检索。