'How to merge documents from two different indexes using Spring elasticsearch

I am new to elastic-search and i am trying to use spring data elastic search in the application. I have a requirement where in there are two separate indexes and i want to fetch documents from both indexes in one query based on some condition.

I would try to explain it with sample example with the same scenario. There are two Different classes for individual indexes.

@Document(indexName = "Book", type = "Book")
public class Book {

@Id
private String id;

@Field(type = FieldType.String)
private String bookName;

@Field(type = FieldType.Integer)
private int price;

@Field(type = FieldType.String)
private String authorName;

//Getters and Setters

}

There is one more class Author

@Document(indexName = "Author", type = "Author")
public Class Author{

@Id
private String id;

@Field(type = FieldType.String)
private String authorName;

//Getters and setters
}

So there are two indexes one Book and Other Author. I want to fetch all the documents where authorName in Book index is equal to authorName in Author index. Can i get the details from both the index as a single document like merged result. It would be very helpful if anyone can suggest solution for this usecase.

Thanks a lot for your answer



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source