The relationship between Spring Data Elasticsearch and Elasticsearch

Spring Data Elasticsearch and Elasticsearch are actually two different products. This article will give you a brief understanding of the relationship between Spring Data Elasticsearch and Elasticsearch.

Elasticsearch

Elasticsearch is one of NoSQL and a powerful tool for building full-text retrieval of big data. See the class ( https://coding.imooc.com/class/125.html). Elasticsearch is written in Java and provides a rich API for users to choose from.

Learning to use these APIs has a learning cost. It can take months to fully master the entire API. So, how to do it? Is there a quick way to let users get started quickly? The answer is Spring Data Elasticsearch.

Spring Data Elasticsearch

If you happen to be a Spring application (like https://waylau.com/spring-boot-blog-video-release/ or https://waylau.com/spring-cloud-video-release/), then use Spring Data Elasticsearch is a very good choice. Because Spring Data Elasticsearch is a member of the entire Spring Data family and has good compatibility with Spring.

Spring Data Elasticsearch has a common interface with other members of the Spring Data family (such as Spring Data JPA), so as long as you learn the interface of Spring Data, you can be competent for any storage device, whether it is a relational database MySQL, SQL Server, Oracle or NoSQL, such as Elasticsearch, MongoDB, etc., can enjoy the convenience brought by the unified interface, it is simple, don't don't~

Here is an example:

interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
}

We must first declare a business-related interface PersonRepository. PersonRepository can inherit from Repository. No need to write specific implementation code.

class SomeClient {

  private final PersonRepository repository;

  SomeClient(PersonRepository repository) {
    this.repository = repository;
  }

  void doSomething() {
    List<Person> persons = repository.findByLastname("Lau");
  }
}

Then, injecting this interface can be used, and the specific implementation Spring Data Elasticsearch will help us provide it. Is it very Cool!

Of course, the definition of the name of the findByLastname method is still important, and it must conform to the semantics of Spring Data. In this way, Spring Data will automatically parse and guess the method name you define to generate the corresponding query statement. From the method name, we can see at a glance that this is a statement to find Person based on the Lastname field.

Summarize

Any technology has advantages and disadvantages, and the use of technology must meet the needs of your own business. Whether to use Spring Data Elasticsearch or the Elasticsearch native API, you have to do your own evaluation.

The advantage of the Elasticsearch native API is that you can use the new features of Elasticsearch for the first time. The disadvantage is that the cost of learning is high.

The advantage of using Spring Data is that it uses a unified interface to adapt to all different storage types, such as SQL, NoSQL, and so on. The downside is that sometimes the adapted version is slower than the native API. This depends on the development speed of the Spring Data Elasticsearch team. See "Spring Data Elasticsearch and Elasticsearch Version Relationship" ( https://www.imooc.com/article/19873).

References

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584110&siteId=291194637