如何将对象数组传递给Elastic search高级Rest API

时间:2019-05-24 12:01:50

标签: java elasticsearch angular7

我正在从ui传递json对象数组,如下所示。

[{
  "name":"Mandoline",
  "description":"A mandoline consists of two parallel working surfaces, one of which can be adjusted in height.[3] A food item is slid along the adjustable surface until it reaches a blade mounted on the fixed surface, slicing it and letting it fall.",
  "type":"creative",
  "link":"/vrushak/files/mandoline.mp3",
  "noofcomments":1,
  "noofviews":2,
  "nooflikes":3,
  "id":7,
  "userobj" :{"user_id":1,name:"test user"}
 },
 {
  "name":"Baratanatyam",
  "description":"Bharatanatyam (Tamil: folk dance) originally known as Sathiraattam(karakattam), is a major genre of Indian classical dance that originated in Tamil Nadu. Traditionally, Bharatanatyam has been a solo dance performed exclusively by women,and it expressed South Indian religious themes and spiritual ideas, particularly of Shaivism, Vaishnavism and Shaktism.",
  "type":"creative",
  "link":"/vrushak/files/baratanatya.mp4",
  "noofcomments":1,
  "noofviews":2,
  "nooflikes":3,
  "id":6,
  "userobj" :{"user_id":2,name:"test user"}
 }]

以上数组已传递给弹性搜索api。下面是它的代码。

@Override
    public void index(Collection<IndexedEntity> entity) throws Exception {
        System.out.println(entity);
        IndexRequest indexRequest = new IndexRequest(index).id("entities");
        entity.forEach(element->{
            convertProfileDocumentToMap(element);
            indexRequest.source(element);//
        });
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
   }




private Map<String, Object> convertProfileDocumentToMap(IndexedEntity profileDocument) {
            return objectMapper.convertValue(profileDocument, Map.class);
        }

带索引的实体只是一个模型,其字段与json数组中的字段相同,并且类型也相同。我正在使用foreach循环,以便可以将每个元素添加到索引,但是不起作用(上面的索引方法)。

以上代码正确吗?因为上面的数组没有被添加到索引中。

请帮助?

0 个答案:

没有答案