通过JAVA API将映射放在ElasticSearch中
我想通过JAVA API为字段设置映射,但失败了。详情如下: 我的数据结构是:通过JAVA API将映射放在ElasticSearch中,java,elasticsearch,resthighlevelclient,Java,elasticsearch,Resthighlevelclient,我想通过JAVA API为字段设置映射,但失败了。详情如下: 我的数据结构是: { "mje-test-execution-id": "464b66ea6c914ddda217659c84a3cb9d", "jvm-free-memory": 315245608, "jvm-total-memory": 361758720, "system-free-memory": 0, "jvm-max-memory": 7600078848, "system-total-memor
{
"mje-test-execution-id": "464b66ea6c914ddda217659c84a3cb9d",
"jvm-free-memory": 315245608,
"jvm-total-memory": 361758720,
"system-free-memory": 0,
"jvm-max-memory": 7600078848,
"system-total-memory": 34199306240,
"memory-time-stamp": "2020-03-12T05:12:16.835Z",
"mje-host-name": "CN-00015345",
"mje-test-suite-name": "SCF Test no mje version",
"mje-version": "1.8.7771-SNAPSHOT",
"mje-test-artifact-id": "msran-regression-tests",
"mje-test-version": "1.8.7771-SNAPSHOT",
"stp-id": "vran-stp",
"mje-test-location": {
"lat": 58.41,
"lon": 15.62
}
}
我想做的是:将“mje测试位置”类型设置为“geo_point”
我的代码片段:
public void postMapping(String indexName, String field, String type) throws IOException {
GetIndexRequest request = new GetIndexRequest(indexName);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if (!exists) {
LOGGER.info("index {} does not exist. Now to post mapping.", indexName);
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject(field);
{
builder.field("type", type);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
putMappingRequest.source(builder);
//
AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest,
RequestOptions.DEFAULT);
boolean acknowledged = putMappingResponse.isAcknowledged();
if (acknowledged) {
LOGGER.info("Succeed to put mapping: field:{}, type: {}", field, type);
}
}
LOGGER.info("Fail to put mapping due to index {} already exist, ", indexName);
}
错误信息:
15:59:54.397 [main] DEBUG org.elasticsearch.client.RestClient - request [PUT http://seliiuapp00269.lmera.ericsson.se:9208/mje-scf-v2-20200313-post/_mapping?master_timeout=30s&timeout=30s] returned [HTTP/1.1 400 Bad Request]
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: mapping type is missing;]
ElasticSearch JAVA API版本:
7.0.0
您需要如下指定文档类型:
putMappingRequest.type("_doc");
还需要在此处指定字段的类型:
builder.field("type", type);
例如:(“类型”、“文本”)
或(“类型”、“长”)
,(“类型”、“日期”)
如果我理解,您可以在中看到数据类型,为文档的每个字段调用方法
postMapping()
?是的。你对此有什么想法吗?@Littlesun,为什么你要为每个字段调用它,这会导致大量的n/w调用,而不是一次创建一个包含所有字段的映射。你应该编写一个方法postMapping(indexName)
,处理所有字段,例如,使用json文件或中的另一种方法:对不起,可能我不清楚。我没有为每个字段调用postMapping(indexName)。事实上,我只调用了一次此方法,将字段“mje测试位置”设置为“geo_point”类型。还有更好的主意吗?