0

I have two entities Merchant and Customer:

public class Merchant{
  private UUID id;
  private String name;
  //... other fields and getters/setters
}    

public class Customer{
      private UUID id;
      private String name;
      //... other fields and getters/setters
    }    

These two entities are sightly different from each-other.

What I'am trying to to do is when I search with the term "John" I want to get both a merchant named "John Market" and a customer called "John Smith".

To achieve this I indexed these entities to a single index.

@Document(indexName = "merchant_customer_index", type = "merchantorcustomer")
public class MerchantOrCustomer {
    @Id
    private UUID id;
    private String name;
    private int type;
    //...

My query can return both Merchant and Customer:

List<MerchantOrCustomer> result = elasticsearchTemplate.queryForList(nativeSearchQuery, MerchantOrCustomer.class);

I distinguish them programmatic(if(result.get(i).getType() == 0 we received Merchant else Customer) Then use their id to extract actual object from relational db.

I searched a lot, but couldn't find anything that can help to estimate if it is a good practice. Is it a good practice?

Please, give me a hint if there is a better way.

valijon
  • 1,304
  • 2
  • 20
  • 35

1 Answers1

0

There doesn't seem to be anything wrong with what you did unless there is some collusion as mentioned by @Ivan in comments. Here is another possible way to do if you were using elasticTemplate- Spring Data Elasticsearch: Multiple Index with same Document or if you are using queryBuilder - https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html

nitzien
  • 1,117
  • 9
  • 23