Search code examples
elasticsearchspring-data-elasticsearch

Query by complex Object field


Consider the document with complex object field:

@Getter  @Setter
@Document
class A{
   @Id
    private String id;

 @Field(
        type = FieldType.Object,
        includeInParent = true
    )
    private B b;
}

@Getter  @Setter
class B{
String field1;
C c;
}

@Getter  @Setter
class C{
String field1;
}

public interface ARepo extends ElasticsearchRepository<A, String> {
    Optional<A> findByB(B b);
}

When I execute the aRepo.findByB(someB), the built query doesn't extract B fields, but contains the result of someB.toString() as filter for field b.

I have JPA background and expect similar behavior . If this is not supported, what is the recommended approach for these kind of queries ?

Thanks


Solution

  • What you are looking is the equivalent of Query By Example (QBE), this is not yet supported in Spring-Data-ES.

    Searching on an class object should be dependend on your mapping, if your mapping is determined by application (@Field on fields with @Mapping and dynamic mapping set to false), and by looking at public interface ElasticsearchRepository<...> - you will have to manually build your query using QueryBuilder (Elastic abstraction) or Criteria (Spring-Data-ES abstraction) and then customizing your repository.

    If your mapping is not determined by application (already set on elastic before), you would have to know it field types (is it a 'text', 'keyword', etc), then there might be different behavior expect for each type (match query, term query, filter, etc) - and this will also have to be custom-implemented with QueryBuilder or Criteria.