2

假设我有一个对象映射,因此映射是:

{"my_type":
    {"properties":
        {"name":{"type":"string","store":"yes","index":"not_analyzed"},
         "more":{"type":"object",
                 "properties":{"a_known_number":{"type":"long","index":"yes"},
                               "some_json_object":{"type":"object"}
                              }
                }
        }
    }
}

我不知道“some_json_object”会有哪些子字段,但我知道我只想存储这个对象,而不是索引它的任何子字段。我可不可以做:

    {"my_type":
    {"properties":
        {"name":{"type":"string","store":"yes","index":"not_analyzed"},
         "more":{"type":"object",
                 "properties":{"a_known_number":{"type":"long","index":"yes"},
                               "some_json_object":{"type":"object","store":"yes","index":"no"}
                              }
                }
        }
    }
}

并影响所有生成的子字段?

4

1 回答 1

6

不,您不能将整个“对象”指定为未编入索引。但是,您可以使用 dynamic_templates ( http://www.elasticsearch.org/guide/reference/mapping/root-object-type/ ) 来执行此操作:

{
   "my_type":{
      "properties":{
         "name":{
            "type":"string",
            "store":"yes",
            "index":"not_analyzed"
         }
      },
      "dynamic_templates":[
         {
            "stored_json_object_template":{
               "path_match":"some_json_object.*",
               "mapping":{
                  "store":"yes",
                  "index":"no"
               }
            }
         }
      ]
   }
}

这告诉映射器将“some_json_object”的所有属性映射为存储的字符串。

更新 从映射中删除的类型以匹配所有属性类型(match_path => path_match)。

更新 2 如果您随后创建索引:

{
   "mappings":{
      "my_type":{
         "properties":{
            "name":{
               "type":"string",
               "store":"yes",
               "index":"not_analyzed"
            }
         },
         "dynamic_templates":[
            {
               "stored_json_object_template":{
                  "path_match":"some_json_object.*",
                  "mapping":{
                     "store":"yes",
                     "index":"no"
                  }
               }
            }
         ]
      }
   }
}

并索引一个对象:

{
   "Name":"Henrik",
   "some_json_object":{
      "string":"string",
      "long":12345
   }
}

然后它将获得以下映射:

{
   "testindex":{
      "my_type":{
         "dynamic_templates":[
            {
               "stored_json_object_template":{
                  "mapping":{
                     "index":"no",
                     "store":"yes"
                  },
                  "path_match":"some_json_object.*"
               }
            }
         ],
         "properties":{
            "name":{
               "type":"string",
               "index":"not_analyzed",
               "store":true,
               "omit_norms":true,
               "index_options":"docs"
            },
            "some_json_object":{
               "properties":{
                  "long":{
                     "type":"long",
                     "index":"no",
                     "store":true
                  },
                  "string":{
                     "type":"string",
                     "index":"no",
                     "store":true
                  }
               }
            }
         }
      }
   }
}
于 2013-06-11T13:37:59.197 回答