不同类型属性的映射

时间:2015-07-08 10:01:43

标签: elasticsearch

在我的应用程序中,我有一个由另外两个类扩展的类。这两个其他类在json中序列化并在elasticsearch上编入索引。

我面临的问题是,扩展第一个类的那两个类提供了一个具有相同名称但不同类型(一个字符串和另一个对象)的属性,如下所示:

{
   "property1" : "a string",
   "property2" : "another string"
}

{
   "property1" : "this is ok as the first one is string too",
   "property2" : {
      "propertyFromProperty2" : "this is not ok"
   }
}

索引时我收到以下异常:

org.elasticsearch.index.mapper.MapperParsingException: failed to parse [property2]
    at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:418)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeObject(ObjectMapper.java:517)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:459)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeObject(ObjectMapper.java:517)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:459)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeObject(ObjectMapper.java:517)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:459)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeObject(ObjectMapper.java:517)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:459)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:515)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:462)
    at org.elasticsearch.index.shard.service.InternalIndexShard.prepareCreate(InternalIndexShard.java:363)
    at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:215)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:556)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:426)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.elasticsearch.ElasticsearchIllegalArgumentException: unknown property [propertyFromProperty2]
    at org.elasticsearch.index.mapper.core.StringFieldMapper.parseCreateFieldForString(StringFieldMapper.java:331)
    at org.elasticsearch.index.mapper.core.StringFieldMapper.parseCreateField(StringFieldMapper.java:277)
    at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:408)

我有什么选择?我可能会使用带有名称的字段类型以避免解析错误,这是否可以使用elasticsearch?

是否还有另一种解决方案?

1 个答案:

答案 0 :(得分:0)

正如你所说,你做不到。在第一个索引请求后,property2获得了类型string

当您尝试第二个索引请求时,您基本上是在尝试索引相同的property2type object

这意味着同一字段的不同字段类型。这在Elasticsearch中是不可能的。

作为选项:使第一个索引请求也与object匹配。也许是这样的,但我不知道你的用例的具体细节:

{
   "property1" : "a string",
   "property2" : {
      "something" : "another string"
   }
}