Spring data elasticsearch - 无法将非对象映射与对象映射

时间:2016-09-12 08:57:53

标签: spring-data spring-data-rest spring-data-elasticsearch

我在尝试使用实体类运行我的应用时遇到错误:

@Document(indexName = "index", type = "event")
public class Event {

...

    @Field(type = FieldType.Date)
    private Date dateTime;

...

}

错误是:

java.lang.IllegalArgumentException: Can't merge a non object mapping [dateTime] with an object mapping [dateTime]

为什么?

好的,然后我评论这个@Field注释。所以,我有:

org.elasticsearch.index.mapper.MapperParsingException: object mapping for [dateTime] tried to parse field [dateTime] as object, but found a concrete value

当我做

curl -XPOST -H "Content-Type:application/json" http://localhost:8080/events -d '{ "dateTime": "2008-03-01T13:00:00.345"}'

1 个答案:

答案 0 :(得分:0)

我在具有GeoPoint属性的文档中遇到了类似的问题,在该文档中,不是使用'geo_point'映射属性,而是将经度和纬度都设置为'double'。 经过几天的努力,我通过定义映射解决了问题:

if (!elasticsearchTemplate.indexExists(SEARCH_INDEX_NAME)) {
  elasticsearchTemplate.createIndex(SEARCH_INDEX_NAME);
}
elasticsearchTemplate.refresh(PlaceIndex.class);
elasticsearchTemplate.putMapping(PlaceIndex.class);

在开始索引项目(并创建索引)之前:

 for (Place place : placePage.getContent()) {
            IndexQuery indexQuery = new IndexQuery();

            PlaceIndex placeIndex = new PlaceIndex();

            placeIndex.setId(place.getId());
            placeIndex.setName(place.getName());
            placeIndex.setPosition(GeoPoint.fromPoint(new Point(place.getPosition().getY(),place.getPosition().getX())));


            indexQuery.setId(placeIndex.getId().toString());
            indexQuery.setObject(placeIndex);
            indexQuery.setSource(elasticObjectMapper.writeValueAsString(placeIndex));

            indexQuery.setIndexName(SEARCH_INDEX_NAME);
            indexQuery.setType(PLACE_INDEX_TYPE);

            queries.add(indexQuery);
            if (counter % INDEX_COMMIT_SIZE == 0) {
                elasticsearchTemplate.bulkIndex(queries);
                queries.clear();
            }
            counter++;
        }

有些晚了,但希望对您或其他人有帮助。