Elasticsearch Java API addMapping()和setSettings()用法
问题:如何使用 json文件包含索引Elasticsearch Java API addMapping()和setSettings()用法,java,json,
elasticsearch,mapping,Java,Json,
elasticsearch,Mapping,问题:如何使用 json文件包含索引de_手册的定义。它还定义了一个analyzerde_analyzer,其中包含相应索引使用的自定义过滤器。 由于json与curl和Sense一起工作,我假设我必须调整它的语法以与JavaAPI一起工作 我不想使用XContentFactory.jsonBuilder(),因为json来自一个文件 我有以下json文件来创建映射并设置设置: 使用带有PUT/indexname的Sense,它确实可以从中创建索引 { "mappings": { "
de_手册
的定义。它还定义了一个analyzerde_analyzer
,其中包含相应索引使用的自定义过滤器。
由于json与curl和Sense一起工作,我假设我必须调整它的语法以与JavaAPI一起工作
我不想使用XContentFactory.jsonBuilder(),因为json来自一个文件
我有以下json文件来创建映射并设置设置:
使用带有PUT/indexname的Sense,它确实可以从中创建索引
{
"mappings": {
"de_brochures": {
"properties": {
"text": {
"type": "string",
"store": true,
"index_analyzer": "de_analyzer"
},
"classification": {
"type": "string",
"index": "not_analyzed"
},
"language": {
"type": "string",
"index": "not_analyzed"
}
}
}
"settings": {
"analysis": {
"filter": {
"de_stopwords": {
"type": "stop",
"stopwords": "_german_"
},
"de_stemmer": {
"type": "stemmer",
"name": "light_german"
}
},
"analyzer": {
"de_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"de_stopwords",
"de_stemmer"
]
}
}
}
}
}
由于上述方法无法单独使用addMapping(),我尝试将其拆分为两个单独的文件(我意识到我必须删除“映射”:
和“设置”:
部分):
这是我加载和添加/设置json的java代码
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
// CREATE SETTINGS
String settings_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.setSettings(settings_json);
// CREATE MAPPING
String mapping_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.addMapping("de_brochures", mapping_json);
CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();
没有更多关于映射文件结构的投诉,但它现在失败并出现错误:
Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [de_analyzer] not found for field [text]
我认为问题在于映射文件的结构 下面是一个示例
mapping.json
{
"en_brochures": {
"properties": {
"text": {
"type": "string",
"store": true,
"index_analyzer": "en_analyzer",
"term_vector": "yes"
},
"classification": {
"type": "string",
"index": "not_analyzed"
},
"language": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
String mapping = new String(Files.readAllBytes(Paths.get("mapping.json")));
createIndexRequestBuilder.addMapping('en_brochures', mapping);
CreateIndexResponse indexResponse =createIndexRequestBuilder.execute().actionGet();
这在我的系统中有效,你可以试试。解决方案:
我使用
createIndexRequestBuilder.setSource(settings_json)使用我的原始json文件实现了这一点代码>您知道我如何将“analyzer”部分放入您的json结构中以使其工作吗?请查看!我改变了,我的答案。而且,为了使其工作,您必须添加设置(正如您以前所做的那样),我假设您已经添加了en_分析器?你是怎么做到的?否则,它会抱怨没有找到“文本”的“en_分析器”。请不要添加问题的解决方案。解决方案不是问题。解决方案就是答案。Stackoverflow鼓励您在找到解决方案时自行回答问题。但请在回答中这样做。编辑问题以包含解决方案只会让读者感到困惑。谢谢
mapping.json
{
"en_brochures": {
"properties": {
"text": {
"type": "string",
"store": true,
"index_analyzer": "en_analyzer",
"term_vector": "yes"
},
"classification": {
"type": "string",
"index": "not_analyzed"
},
"language": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
String mapping = new String(Files.readAllBytes(Paths.get("mapping.json")));
createIndexRequestBuilder.addMapping('en_brochures', mapping);
CreateIndexResponse indexResponse =createIndexRequestBuilder.execute().actionGet();