Home > Software design >  Creating subfileds of nested array in java
Creating subfileds of nested array in java

Time:01-07

POST feeds/_doc
{
  
  "feed_id": "3",
  "title": "jose ",
  "body": "This is Jose allen",
  "comment": [
    {
      "c_id": "13",
      "feed_id": "2",
      "c_text": "hey"
    },
    {
      "c_id": "13",
      "feed_id": "3",
      "c_text": "  here"
    }
  ]
}

this is the mapping

PUT feeds
{
  "mappings": {
    "properties": {
      "comment":{
        "type": "nested"
      }
    }
  }
}

I want to create feed class with mentioned feilds the comment field has nested type fields with sub fileds of c_text,c_id .how can i create it

@Document(indexName = "feeds")
public class Feed {
    @Id
    private String feed_id;
    
    @Field(type = FieldType.Text, name = "title")
    private String title;
    
    @Field(type = FieldType.Text, name = "body")
    private String body;
    
    @Field(type= FieldType.Nested , name="comment")
     private String[] comment;

}

how to intialize subfields of comment field as i am using elasticsearch 7.17.3

CodePudding user response:

You can create a Comment class like this:

public class Comment {

    @JsonProperty("c_id")
    private String cId;

    @JsonProperty("feed_id")
    private String feedId;

    @JsonProperty("c_text")
    private String cText;

    // Getters, setters, ...
}

and instead of using private String[] comment, you should use:

@Field(type= FieldType.Nested , name="comment")
private List<Comment> comment;
  • Related