Search code examples
javaspring-dataspring-data-elasticsearch

Migration from Embedded ElasticSearch to spring-data-elasticsearch 4.0.6.RELEASE


I am working on a small project that needs to be migrated to a more current Spring version.

I am having troubles fixing this query:

SearchResponse response = new SearchRequestBuilder(node.client())
    .setIndices(INDEX)
    .setTypes(TYPE_MYCLASS)
    .setFrom(page * PAGE_SIZE)
    .setSize(PAGE_SIZE)
    .setQuery(
        new FilteredQueryBuilder(
            new MatchAllQueryBuilder()
            , new TermFilterBuilder("mostRecent", true)
        )
    )
    .get();

SearchHit[] hits = response.getHits().getHits();

I have already migrated the DTO-Class (TYPE_MYCLASS) to Spring-Data Document annotation

@Mapping(
    mappingPath = "es/mappings/myclass.json")
@Document(
    indexName = ElasticsearchService.INDEX
    , type = ElasticsearchService.TYPE_MYCLASS)
public class MyClass {
    public Boolean mostRecent;
    // other Fields
}

Hoping that an expert could help me.


Solution

  • When you use spring-data you usually define repositories to query the data. It also supports naitive queries.

    Check this out: https://www.baeldung.com/spring-data-elasticsearch-tutorial