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.

2019/04

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

./elasticsearch

Spring Data

Add following dependencies in build.gradle

compile("org.springframework.data:spring-data-elasticsearch")

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

Code

Config.java

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

    @Bean
    public Client client() {
        TransportClient client = null;

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


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

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

Member.java

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

    private String id;

    private String name;
}

MemberRepository.java

public interface MemberRepository extends ElasticsearchRepository<Member, String> {

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

ElasticSearchTestController.java

@RestController
@RequestMapping(value="/api/elasticsearch/")
public class ElasticSearchTestController {


    @Autowired
    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()) {
            list.add(member);
        }

        return  list;
    }

}

Test

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

コメント