【问题标题】:Insert object using java client 6.5使用 java 客户端 6.5 插入对象
【发布时间】:2020-04-22 11:40:46
【问题描述】:

我想使用 java 高级别的客户端插入一个对象。

我的映射如下:

"images":{
           "type":"nested",
           "properties":{
              "name":{
                  "type":"text"
              },
              "url":{
                 "type":"text"
              }
           }
        }

和java代码:

//productDb.getImages() 返回Image类型的ArrayList:带有属性“name”和“url”

productToIndex.put("images", new Gson().toJson(productDb.getImages()));
      IndexRequest indexRequest = new IndexRequest(ProductIndex.PRODUCT_INDEX, ProductIndex.TYPE)
                    .source(productToIndex, XContentType.JSON);
            try {              
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);              
            } catch (Exception e) {
                logger.error("Data access error occured the entity persistence...", e);
                throw new Exception(e);
            }

错误是:

ElasticsearchStatusException[Elasticsearch 异常 [type=mapper_parsing_exception, reason=object mapping for [images] 试图将字段 [images] 解析为对象,但找到了具体值]]

具体值:

images -> [{"name":"Capture.PNG","url":"/api/trader/files/2132132/Capture.PNG"},{"name":"payement.jpg","url":"/api/trader/files/31231321/payement.jpg"}]

==> 对我来说似乎还可以,如果我将 productDb.getImages() 替换为 new Arraylist(); ,我不明白为什么它会返回异常;它工作正常(索引值为:“图像”:[])

我找不到如何使用 java 索引数组对象的示例,有解决方案吗?

谢谢。

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    如果您发现 ES 期望您的 JSON 格式与您发送有效负载的格式有所不同。

    索引嵌套文档的正确格式

    {
        "images": [ --> note image array
            {
                "name": "opster.PNG",
                "url": "/api/trader/files/2132132/Capture.PNG"
            },
            {
                "name": "data.jpg",
                "url": "/api/trader/files/31231321/payement.jpg"
            }
        ]
    }
    

    java代码生成的JSON,注意没有images键

    {
      "name": "Capture.PNG",
      "url": "/api/trader/files/2132132/Capture.PNG"
    },
    {
      "name": "payement.jpg",
      "url": "/api/trader/files/31231321/payement.jpg"
    }
    

    由于 Elasticsearch 无法找到 images 键并且您的不是有效的 JSON 数组,因此 Elasticsearch 会抛出错误:

    解决方案:

    请发送开头提到的 JSON,然后使用下面的代码将其转换为适当的格式。

    public void saveToEs(NestedImages nestedImages) throws IOException {
            ObjectMapper Obj = new ObjectMapper();
            final String images = Obj.writeValueAsString(nestedImages);
            final IndexRequest indexRequest = new IndexRequest("nestedimage")
                    .source(images, XContentType.JSON);
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    
        }
    

    nested data type index def and indexing exampleREST high-level indexing java code 的重要链接。

    【讨论】:

    • 谢谢,是的,我也试过这个解决方案,代码如下: ObjectMapper mapper = new ObjectMapper();字符串图像 = mapper.writeValueAsString(productDb.getImages()); productToIndex.put(ProductIndex.IMAGES, 图像); productToIndex 是一个 HashMap,key= images 和 value = [{"name":"Capture.PNG","url":"/api/trader/files/kjghk/Capture.PNG"},{"name":" payement.jpg","url":"/api/trader/files/kjghk/payement.jpg"}] 但它不起作用。
    • @Nicolas,你能在发送到 ES 之前调试你的代码并检查 JSON 吗?如果它不是我在开始时提供的格式,它将失败
    • 这段代码解决了我的问题: List> images = new ArrayList(); productDb.getImages().stream().forEach(image -> { Map map = new HashMap(); map.put(ProductIndex.IMAGE_NAME, image.getName()); map.put( ProductIndex.IMAGE_URL, image.getUrl()); images.add(map); });谢谢!
    • 太好了,是的,现在看起来它会生成正确的 JSON,很高兴它有帮助,请不要忘记投票并将其标记为答案 :)
    猜你喜欢
    • 2013-03-29
    • 2017-05-31
    • 2014-08-25
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多