ElasticSearch使用动态属性创建索引

时间:2016-07-26 18:44:40

标签: elasticsearch indexing

是否可以创建索引,限制索引父属性?

例如,

$ curl -XPOST 'http://localhost:9200/actions/action/' -d '{
  "user": "kimchy",
  "message": "trying out Elasticsearch",
  "actionHistory": [
    { "timestamp": 123456789, "action": "foo" },
    { "timestamp": 123456790, "action": "bar" },
    { "timestamp": 123456791, "action": "buz" },
    ...
  ]
}'

我不希望{em} 将actionHistory编入索引。怎么办呢?

对于上述文档,我相信索引将创建为

$ curl -XPOST localhost:9200/actions -d '{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "action": {
      "properties" : {
        "user": { "type": "string", "index" : "analyzed" },
        "message": { "type": "string": "index": "analyzed" },
        "actionHistory": {
          "properties": {
            "timestamp": { 
              "type": "date", 
              "format": "strict_date_optional_time||epoch_millis"
            },
            "action": { "type": "string", "index": "analyzed" }
          }
        }
      }
    }
  }
}'

properties删除actionHistory并将其替换为"index": "no"是否是正确的解决方案?

这是一个示例,但我的实际情况是具有动态属性的文档(即actionHistory包含所有文档中的各种自定义,非重复属性),并且此特定类型的映射定义具有超过2000个不同的属性,使搜索速度极慢(即比数据库中的全文搜索更糟糕)。

1 个答案:

答案 0 :(得分:1)

您可以使用dynamic templates离开,在所有actionHistory子字段上匹配,并为所有子字段设置"index": "no"

PUT actions
{
  "mappings": {
    "action": {
      "dynamic_templates": [
        {
          "actionHistoryRule": {
            "path_match":   "actionHistory.*",
            "mapping": {
              "type": "{dynamic_type}",
              "index": "no"
            }
          }
        }
      ]
    }
  }
}