我正在尝试创建以下索引和映射。
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("my_index");
XContentBuilder mappingBuilder = jsonBuilder()
.startObject()
.startObject("my_type")
.startObject("properties")
.startObject("nombre")
.field("analyzer", "keyword_lowercase")
.field("type", "string")
.endObject()
.startObject("codigo")
.field("type", "string")
.field("analyzer", "keyword_lowercase")
.endObject()
.endObject()
.endObject()
.endObject();
createIndexRequestBuilder.addMapping(document.type(), mappingBuilder);
XContentBuilder mappingSettingsBuilder = jsonBuilder()
.startObject()
.startObject("index")
.startObject("analisis")
.startObject("analyzer")
.startObject("keyword_lowercase")
.field("type","custom")
.field("tokenizer","keyword")
.array("filter", "lowercase")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
createIndexRequestBuilder.setSettings(mappingSettingsBuilder);
createIndexRequestBuilder.execute().actionGet();
但我得到以下例外:
org.elasticsearch.index.mapper.MapperParsingException:analyzer 字段[codigo]找不到[keyword_lowercase]
我想我必须以任何方式关闭/打开索引才能获得成功,但我不知道什么时候......我已经测试了几件事但没有成功。有什么想法吗?
非常感谢
答案 0 :(得分:0)
我认为当我们使用create api时,不可能在单独的线程中进行映射和索引。解决方案是同时进行的:
XContentBuilder mappingCreateBuilder = jsonBuilder()
.startObject()
.startObject("mappings")
.startObject(document.type())
.startObject("properties")
.startObject("nombre")
.field("analyzer", "keyword_lowercase")
.field("type", "string")
.endObject()
.startObject("codigo")
.field("type", "string")
.field("analyzer", "keyword_lowercase")
.endObject()
.endObject()
.endObject()
.endObject()
.startObject("settings")
.startObject("index")
.startObject("analisis")
.startObject("analyzer")
.startObject("keyword_lowercase")
.field("type","custom")
.field("tokenizer","keyword")
.array("filter", "lowercase")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
createIndexRequestBuilder = client.admin().indices().prepareCreate(document.indexName());
createIndexRequestBuilder.setSettings(mappingCreateBuilder);
createIndexRequestBuilder.execute().actionGet();