enabled

编辑

Elasticsearch 尝试索引您提供的所有字段,但有时您只想存储字段而不进行索引。例如,假设您正在使用 Elasticsearch 作为 Web 会话存储。您可能希望索引会话 ID 和上次更新时间,但您不需要查询或对会话数据本身运行聚合。

enabled 设置,只能应用于顶层映射定义和object字段,使 Elasticsearch 跳过对字段内容的解析。JSON 仍然可以从 _source 字段中检索,但它不可搜索,也不以任何其他方式存储。

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "user_id": {
                "type": "keyword"
            },
            "last_updated": {
                "type": "date"
            },
            "session_data": {
                "type": "object",
                "enabled": False
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="session_1",
    document={
        "user_id": "kimchy",
        "session_data": {
            "arbitrary_object": {
                "some_array": [
                    "foo",
                    "bar",
                    {
                        "baz": 2
                    }
                ]
            }
        },
        "last_updated": "2015-12-06T18:20:22"
    },
)
print(resp1)

resp2 = client.index(
    index="my-index-000001",
    id="session_2",
    document={
        "user_id": "jpountz",
        "session_data": "none",
        "last_updated": "2015-12-06T18:22:13"
    },
)
print(resp2)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'keyword'
        },
        last_updated: {
          type: 'date'
        },
        session_data: {
          type: 'object',
          enabled: false
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 'session_1',
  body: {
    user_id: 'kimchy',
    session_data: {
      arbitrary_object: {
        some_array: [
          'foo',
          'bar',
          {
            baz: 2
          }
        ]
      }
    },
    last_updated: '2015-12-06T18:20:22'
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 'session_2',
  body: {
    user_id: 'jpountz',
    session_data: 'none',
    last_updated: '2015-12-06T18:22:13'
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      user_id: {
        type: "keyword",
      },
      last_updated: {
        type: "date",
      },
      session_data: {
        type: "object",
        enabled: false,
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: "session_1",
  document: {
    user_id: "kimchy",
    session_data: {
      arbitrary_object: {
        some_array: [
          "foo",
          "bar",
          {
            baz: 2,
          },
        ],
      },
    },
    last_updated: "2015-12-06T18:20:22",
  },
});
console.log(response1);

const response2 = await client.index({
  index: "my-index-000001",
  id: "session_2",
  document: {
    user_id: "jpountz",
    session_data: "none",
    last_updated: "2015-12-06T18:22:13",
  },
});
console.log(response2);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user_id": {
        "type":  "keyword"
      },
      "last_updated": {
        "type": "date"
      },
      "session_data": { 
        "type": "object",
        "enabled": false
      }
    }
  }
}

PUT my-index-000001/_doc/session_1
{
  "user_id": "kimchy",
  "session_data": { 
    "arbitrary_object": {
      "some_array": [ "foo", "bar", { "baz": 2 } ]
    }
  },
  "last_updated": "2015-12-06T18:20:22"
}

PUT my-index-000001/_doc/session_2
{
  "user_id": "jpountz",
  "session_data": "none", 
  "last_updated": "2015-12-06T18:22:13"
}

session_data 字段已禁用。

任何任意数据都可以传递给 session_data 字段,因为它将被完全忽略。

session_data 也会忽略不是 JSON 对象的值。

整个映射也可以禁用,在这种情况下,文档存储在 _source 字段中,这意味着它可以被检索,但其内容不会以任何方式被索引。

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "enabled": False
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="session_1",
    document={
        "user_id": "kimchy",
        "session_data": {
            "arbitrary_object": {
                "some_array": [
                    "foo",
                    "bar",
                    {
                        "baz": 2
                    }
                ]
            }
        },
        "last_updated": "2015-12-06T18:20:22"
    },
)
print(resp1)

resp2 = client.get(
    index="my-index-000001",
    id="session_1",
)
print(resp2)

resp3 = client.indices.get_mapping(
    index="my-index-000001",
)
print(resp3)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      enabled: false
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 'session_1',
  body: {
    user_id: 'kimchy',
    session_data: {
      arbitrary_object: {
        some_array: [
          'foo',
          'bar',
          {
            baz: 2
          }
        ]
      }
    },
    last_updated: '2015-12-06T18:20:22'
  }
)
puts response

response = client.get(
  index: 'my-index-000001',
  id: 'session_1'
)
puts response

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    enabled: false,
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: "session_1",
  document: {
    user_id: "kimchy",
    session_data: {
      arbitrary_object: {
        some_array: [
          "foo",
          "bar",
          {
            baz: 2,
          },
        ],
      },
    },
    last_updated: "2015-12-06T18:20:22",
  },
});
console.log(response1);

const response2 = await client.get({
  index: "my-index-000001",
  id: "session_1",
});
console.log(response2);

const response3 = await client.indices.getMapping({
  index: "my-index-000001",
});
console.log(response3);
PUT my-index-000001
{
  "mappings": {
    "enabled": false 
  }
}

PUT my-index-000001/_doc/session_1
{
  "user_id": "kimchy",
  "session_data": {
    "arbitrary_object": {
      "some_array": [ "foo", "bar", { "baz": 2 } ]
    }
  },
  "last_updated": "2015-12-06T18:20:22"
}

GET my-index-000001/_doc/session_1 

GET my-index-000001/_mapping 

整个映射已禁用。

文档可以被检索。

检查映射显示没有添加任何字段。

现有字段和顶层映射定义的 enabled 设置无法更新。

请注意,由于 Elasticsearch 完全跳过了对字段内容的解析,因此可以将非对象数据添加到禁用的字段中。

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "session_data": {
                "type": "object",
                "enabled": False
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="session_1",
    document={
        "session_data": "foo bar"
    },
)
print(resp1)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        session_data: {
          type: 'object',
          enabled: false
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 'session_1',
  body: {
    session_data: 'foo bar'
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      session_data: {
        type: "object",
        enabled: false,
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: "session_1",
  document: {
    session_data: "foo bar",
  },
});
console.log(response1);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "session_data": {
        "type": "object",
        "enabled": false
      }
    }
  }
}

PUT my-index-000001/_doc/session_1
{
  "session_data": "foo bar" 
}

即使 session_data 包含非对象数据,文档也会成功添加。