---
title: "ElasticSearch1.7.3 报错Root type mapping not empty after parsing!"
author: "xiang37"
date: "2015-12-16T23:02:40Z"
tags: []
categories: ["编程语言"]
slug: "elasticsearch1-7-3-root-type-mapping-not-empty-after-parsing"
draft: false
lastmod: "2015-12-16T23:04:09Z"
id: 2264640
url: "https://xiva.iteye.com/blog/2264640"
---

熟悉Lucene也比较久了，最近想了解一下ElasticSearch的原理以及简单的使用一下。

代码如下：

```
 try
        {
            XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("xivaik").startObject("properties").startObject("ajmc").field("type", "string")
                    .field("store", "yes").endObject().startObject("ajbh").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
                    .startObject("jyaq").field("type", "string").field("indexAnalyzer", "ik").field("searchAnalyzer", "ik").field("index", "analyzed").endObject()
                    .startObject("addDate").field("type", "date").endObject().endObject().endObject().endObject();

            PutMappingRequest mappingRequest = Requests.putMappingRequest("xivaik").type("infoaj").source(mapping);

            client.admin().indices().putMapping(mappingRequest).actionGet();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
```

 

执行时，结果报错Root type mapping not empty after parsing!

后来看到这个帖子，给我一点启发。

```
I removed the "mappings" json object and it fixed the issue.
```

 

[http://stackoverflow.com/questions/29304374/elasticsearch-java-root-type-mapping-not-empty-after-parsing](http://stackoverflow.com/questions/29304374/elasticsearch-java-root-type-mapping-not-empty-after-parsing)

 应该是我在构建mappingRequest 出现了节点重复构造。

我本想构造的信息是：

{"xivaik":{"properties":{"ajmc":{"type":"string","store":"yes"},"ajbh":{"type":"string","store":"yes","index":"not\_analyzed"},"jyaq":{"type":"string","indexAnalyzer":"ik","searchAnalyzer":"ik","index":"analyzed"},"addDate":{"type":"date"}}}}

可以看得出，startObject("xivaik")已经将我的索引xivaik添加到了结构信息中，但是Requests.putMappingRequest("xivaik").type("infoaj")中又指定了索引xivaik，我想是不是重复了。

去掉.putMappingRequest("xivaik")是不可能的，所以我将构建结构的语句中去掉startObject("xivaik")，结果成功执行。

```
try
        {
            XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("ajmc").field("type", "string")
                    .field("store", "yes").endObject().startObject("ajbh").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
                    .startObject("jyaq").field("type", "string").field("indexAnalyzer", "ik").field("searchAnalyzer", "ik").field("index", "analyzed").endObject()
                    .startObject("addDate").field("type", "date").endObject().endObject().endObject();

            PutMappingRequest mappingRequest = Requests.putMappingRequest("xivaik").type("infoaj").source(mapping);

            client.admin().indices().putMapping(mappingRequest).actionGet();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
```

 

结果证明了前面的猜测。希望能够后来人一个参考。