3

How to map Nested objects in Spring data elasticsearch

I have object 1 having list of object 2. How to efficiently map this so that querying back elasticsearch is easy ? I want to retrieve object 2 based on ID.

@Document(indexName = xxx, type = xxx)
public class Object1 {
    private List<Obj2> lstObj2;
} 

public class Obj2 {

    private Long id;
}

1 Answer 1

2

Use nested Object like this:

@Document(indexName = xxx, type = xxx)
public class Object1 {

  @Field(type = FieldType.Nested)
  private List<Obj2> lstObj2;
} 

public class Obj2 {
  private Long id;
}

As per your requirement it seems that you can use inner Object as well. Use inner object like this.

@Field(type = FieldType.Object)
private List<Obj2> lstObj2;
Sign up to request clarification or add additional context in comments.

3 Comments

Without marking it as object I could query these values. What is special about Nested or Inner Object compared to normal mapping
This is same as Mapping which you provide while creating an index using ES.
@Richa why am i getting reason=failed to parse field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.