如何使用弹性搜索RestHighLevelClient在Java中编写json映射? [英] How to write json mapping in java using elastic search RestHighLevelClient?

查看:2380
本文介绍了如何使用弹性搜索RestHighLevelClient在Java中编写json映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下映射,想知道如何使用RestHighLevelClient在Java中编写相同的映射

I have the below mapping and wondering how to write the same mapping in java using RestHighLevelClient

{
  "mappings": {
    "properties": {
      "events": {
        "type": "nested",
        "properties": {
          "ecommerceData": {
            "type": "nested",
            "properties": {
              "comments": {
                "type": "nested",
                "properties": {
                  "recommendationType": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

推荐答案

最简单的创建方式(作为其嵌套方式)是将此映射放在文件的JSON format中,然后以字符串格式读取(提供的实用程序方法) )并创建如下所示的映射:

The easiest way to create(as its nested) is to put this mapping in JSON format in the file and then read it in string format(provided utility methods) and creates the mapping as shown below:

官方文档.

创建一个名为nested.mapping的文件,我将使用nested作为索引名称.

Create a file named nested.mapping and I will use the nested as an index name.

使用以下实用程序方法读取文件并以string格式返回文件

Use following utility method to read the file and return it in string format

public String getStringFromFile(String fileName) throws IOException {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        InputStream in = classLoader.getResourceAsStream(fileName); --> file name
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString(StandardCharsets.UTF_8.name());
    }

Resthighlevel客户端代码,使用上述实用程序方法创建索引

 if (!isIndexExist(client, indexName)) {
                JsonUtil jsonUtil = new JsonUtil();
                String indexString = jsonUtil.getStringFromFile(indexName + ".mapping");
                CreateIndexRequest request = new CreateIndexRequest(indexName);
                request.source(indexString, XContentType.JSON);
                client.indices().create(request, RequestOptions.DEFAULT);
            }

请参阅我的Java调试器屏幕截图,该屏幕截图会正确地以JSON格式读取此文件.

Please see my java debugger screen-shot, which read this file properly in JSON format.

最后,弹性映射API结果,显示成功创建索引.

Finally, Elastic mapping API result, which shows index created successfully.

{
  "nested": {
    "aliases": {

    },
    "mappings": {
      "properties": {
        "events": {
          "type": "nested",
          "properties": {
            "ecommerceData": {
              "type": "nested",
              "properties": {
                "comments": {
                  "type": "nested",
                  "properties": {
                    "recommendationType": {
                      "type": "keyword"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}

这篇关于如何使用弹性搜索RestHighLevelClient在Java中编写json映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆