熟悉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.
应该是我再构建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(); }
结果证明了前面的猜测。希望能够给后来人一个参考。