[英]Elasticsearch High Level Rest Client - java Map with typed (sub) fields - dates, numbers etc
(clarification copied from a comment) (从评论中复制的澄清)
I have a java.util.Map
that has different key value pairs, and some of the values are dates, some numbers, some are strings, and some are also java.util.Map
s that can also contain all kinds of above mentioned types. 我有一个
java.util.Map
,它有不同的键值对,一些值是日期,一些数字,一些是字符串,还有一些也是java.util.Map
,它们也可以包含各种上述类型。 I am able to put it into the index, I see that the elasticsearch mapping is created automatically with correct field types, now I want to retrieve that Map
and see dates, numbers, strings and nested Map
s instead of what I currently have - just strings and Maps 我能够将它放入索引中,我看到弹性搜索映射是使用正确的字段类型自动创建的,现在我想要检索该
Map
并查看日期,数字,字符串和嵌套Map
而不是我现在拥有的 - 只是字符串和地图
Further story: 更多故事:
I'm putting a java.util.Map
in Elasticsearch using the following code: 我正在使用以下代码在Elasticsearch中放置一个
java.util.Map
:
public void putMap(String key, Map<String, ?> value) {
try {
IndexRequest ir = Requests.indexRequest(_index)
.id(key)
.type("doc")
.source(value);
Factory.DB.index(ir); // the high level rest client here
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
I am not able to create mappings explicitly as per my task. 我无法根据我的任务明确创建映射。
For one of my indices it has created the mapping like this, which is quite fine: 对于我的一个索引,它创建了这样的映射,这很好:
{
"rex": {
"mappings": {
"doc": {
"properties": {
"variables": {
"properties": {
"customer": {
"properties": {
"accounts": {
"properties": {
"dateOpen": {
"type": "date"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"dateOfBirth": {
"type": "date"
},
"firstName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"middleName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
}
}
Now I am retrieving my structure back with the following code, 现在我用以下代码检索我的结构,
public Map<String, ?> getMap(String key) {
try {
GetRequest gr = new GetRequest(_index, "doc", key);
try {
GetResponse response = Factory.DB.get(gr);
if (!response.isExists()) {
return null;
}
Map<String, ?> ret = response.getSourceAsMap();
return ret;
} catch (ElasticsearchStatusException ee) {
if (ee.status().getStatus() == RestStatus.NOT_FOUND.getStatus()) {
return null;
} else {
throw new RuntimeException(ee);
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
The dates are returned as strings like "1957-04-29T00:00:00.000Z" 日期以字符串形式返回,如“1957-04-29T00:00:00.000Z”
There's no Java object to map this document to as I have only Maps of Maps/Lists/values. 没有用于映射此文档的Java对象,因为我只有地图/列表/值的地图。
How do I make the Java Rest Client respect the mapping the Elasticsearch created for the index? 如何使Java Rest Client尊重Elasticsearch为索引创建的映射?
response.getFields()
returns empty map. response.getFields()
返回空映射。
In case it is impossible (like 'source is json/strings by design' etc etc), I am ready to retrieve the mapping in the most convenient form possible and walk through the result by myself. 如果不可能(比如'source is json / strings by design'等),我准备以最方便的形式检索映射,并自己遍历结果。 The code to retrieve elasticsearch mapping will be appreciated.
检索弹性搜索映射的代码将不胜感激。
Big thank you! 非常感谢!
If you still want to fetch the type mapping and do the conversion manually, the Indices API has a Get Mapping command you can invoke with the Java Low Level REST Client . 如果您仍想获取类型映射并手动执行转换,则Indices API具有可以使用Java低级REST客户端调用的Get Mapping命令。
String getMapping(RestHighLevelClient client, String index, String type) throws IOException {
String endpoint = index + "/_mapping/" + type;
Response response = client.getLowLevelClient().performRequest("GET", endpoint);
return EntityUtils.toString(response.getEntity());
}
But really I would recommend instead using something like Jackson for data binding. 但实际上我建议使用像Jackson这样的东西进行数据绑定。 Bind the
Map
you get from Elasticsearch to a Java object that models the document, and let Jackson handle the type conversion for you. 将您从Elasticsearch获取的
Map
绑定到对文档建模的Java对象,让Jackson为您处理类型转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.