Skip to content

Spring Data Repository not taking into account @Field(name) annotation when generating queries for Object Fields #1752

Closed
@Mynameisbt

Description

@Mynameisbt

I am having an issue when trying to do a search with a Spring Data Elasticsearch 4 repository when using a Object Field Type. Below is a simple example that is replicating the issue I am having in my application. With Spring Data Elasticsearch 3 the query generated by the repository would use the @Field(name) value but it no longer does with Spring Data Elasticsearch 4.

The query that it is generating for the Authors.FirstName field repository method according to the logs is:

Request body: {"from":0,"size":10000,"query":{"bool":{"must":[{"query_string":{"query":"John","fields":["authors.firstName^1.0"],"type":"best_fields","default_operator":"and","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true}

I would have expected the fields query to be:

["Authors.FirstName^1.0"]

based on the @Field annotation. That is what Spring Data Elasticsearch 3 would generate

Is there something I am missing in my configuration for the query to use the @Field(name) value in Spring Data Elasticsearch 4?

I have a github test setup that reproduces the problem. I just run a Elasticsearch docker container prior to executing the test.

https://github.com/Mynameisbt/nestedobjectquerytest/blob/master/src/test/java/com/mynameisbt/nestedobjectquerytest/NestedobjectquerytestApplicationTests.java

If I remove the custom names for authors and first name then the documents get returned.

@Document(indexName = "blog", type = "article")
public class Article {
    @Id
    private String id;
    @MultiField(mainField = @Field(type = Text, fielddata = true), otherFields = {@InnerField(suffix = "verbatim", type = Keyword)})
    private String title;

    @Field(type = FieldType.Object, name = "Authors")
    private List<Author> authors;

    @Field(type = Keyword)
    private String[] tags;

    public Article() {
    }

    public Article(String title) {
        this.title = title;
    }
}

Subclass

public class Author {
    @Field("FirstName")
    private String firstName;
    @Field("LastName")
    private String lastName;

Repository

public interface ArticleRepository extends ElasticsearchRepository<Article, String> {

    Page<Article> findByAuthorsFirstName(String name, Pageable pageable);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions