简体   繁体   English

Elasticsearch高级休息客户端 - 带有类型(子)字段的Java Map - 日期,数字等

[英]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.

相关问题 如何使用Java High Level Rest Client API映射elasticsearch查询的结果集? - How to map the resultset of elasticsearch query with Java High Level Rest Client API? Elasticsearch高级Rest Client Java排序不正常 - Elasticsearch High Level Rest Client Java sorting not working properly ElasticSearch 多词查询与 Java 高级 REST 客户端 - ElasticSearch Multi Term Query With Java High-Level REST Client ElasticSearch Java高级Rest客户端:建议搜索 - ElasticSearch Java high Level Rest Client: Suggest-search ElasticSearch Java 高级 REST 客户端:过滤文档和或查询 - ElasticSearch Java High Level REST Client: filter documents and or query 使用 java 中的 Rest 高级客户端重新索引选定的 _source 字段 - Reindex selected _source fields using Rest high level client in java ElasticSearch Rest 高级客户端重新映射错误 - ElasticSearch Rest High Level Client remapping wrong 在 Java 的 elasticsearch 高级客户端中添加身份验证 - Add authentication in elasticsearch high level client for Java 我们如何在 Java 中为 ElasticSearch 7.4.2 Java 高级 REST 客户端创建一个 IndexRequest? - How do we create an IndexRequest in Java for ElasticSearch 7.4.2 Java High Level REST Client? 使用 Elasticsearch 的 Java High Level Rest Client 收到对异步请求的响应后立即返回某些内容 - return something as soon as a response to an async request is received using Elasticsearch's Java High Level Rest Client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM