Spring Elastic Search

Elastic Search

Elastic Search is one of product in Elastic Stack. This is Lucene (Apache Lucene) base Index search. It’s powerful for search in product.

Spring Data support Elastic Search like other Spring Data (repository, model etc…)

Elastic Search Version

Elastic Search version and Spring Data Elastic Search component has version limitation.


Elastic Search VersionSpring Data Version
Elastic Search 6.xSpring Data Elastic Search
Elastic Search 7.xNot Support

I tried to resolve Spring Data Elastic Search version.(Use elastic-core 7.x but not work)

Use Elastic Search in Mac

Download tar from website and find bin/elasticsearch

You can access to run command via http request


Spring Data

Add following dependencies in build.gradle


Populate Data for Test

Create Index, Type(_doc), and populate data

curl -X PUT “localhost:9200/member?pretty”

curl -X PUT “localhost:9200/_doc/1?pretty” -H ‘Content-Type: application/json’ -d’
“name”: “John Doe”

Create member Index and _doc type and populate one record



@EnableElasticsearchRepositories(basePackages ="com.daiji110.springsample.repository")
@ComponentScan(basePackages = { "com.daiji110.springsample" })
public class Config {

    public Client client() {
        TransportClient client = null;

        Settings esSettings = Settings.builder()
                .put("cluster.name", "elasticsearch")

        try {
            client = new PreBuiltTransportClient(esSettings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
        } catch(Exception oops) {
        return client;

    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());


@Document(indexName= "member", type="_doc")
public class Member {

    private String id;

    private String name;


public interface MemberRepository extends ElasticsearchRepository<Member, String> {

    Page<Member> findByName(String name, Pageable pageable);


public class ElasticSearchTestController {

    private MemberRepository memberRepository;

    @RequestMapping(value = "/members", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    List<Member> getMember() {

        // PageRequest p = new PageRequest(0, 20);

        // Page<Member> page = memberRepository.findByName("John Doe", p);

        List<Member> list = new ArrayList<>();
        for (Member member : memberRepository.findAll()) {

        return  list;



Access http://localhost:8080/api/elasticsearch/members