A few concepts

1, Index Type Document

And usually when we’re starting out, we’re going to compare this to a database to make sense of it

  • Index->Database
  • Type->Table (The latest version no longer uses Type, so many people will wonder why? ES is not the same as a database, so don’t just think of ES as a database.)
  • Document->Row

2, inverted index

Refer to this article :(generally, we find the corresponding article from the table of contents as the forward index, if we find the corresponding article from the keyword index, that is, the inverted index)

ES index analysis (| inverted index structure is row index)

Two, several Java call ES way

  • Rest API Test
  • Using SpringBoot+Spring Data Repositories
  • Using the SpringBoot+RestClient
  • Using Transport Client

SpringBoot+Spring Data Repositories is recommended, which is complete and easy to use. SpringBoot+RestClient(RestHighLevelClient) is recommended for advanced search.

SpringBoot+Spring Data Repositories

  • Github repository on the official website and corresponding demo

[github.com/spring-proj…

github.com

] (link.zhihu.com/?target=htt…).

The following is a demo on the official website, you can see that it is relatively easy to get started

Public Interface PersonRepository extends CrudRepository<Person, Long> {/* * SpringDatarepository implements functions directly from the semantics of method names. List<Person> findByLastname(String lastName); List<Person> findByLastname(String lastName); List<Person> findByFirstnameLike(String firstname); } @Service public class MyService { private final PersonRepository repository; public MyService(PersonRepository repository) { this.repository = repository; } public void doWork() {// Create & update Person = new Person(); person.setFirstname("Oliver"); person.setLastname("Gierke"); repository.save(person); / / delete repository. DeleteAll (); / / precise query List < Person > lastNameResults = repository. The findByLastname (" Gierke "); / / fuzzy query List < Person > firstNameResults = repository. FindByFirstnameLike (" Oli "); }}Copy the code
  • Below is the corresponding semantic comparison table of method names

  • Some corresponding test cases

    @SpringBootTest class PersonRepositoryTest { @Autowired PersonRepository personRepository;

    @beforeeach void setUp() {} /** * Create or update index document * delete similar, Personpersonator.delete */ @test void create_update() {Person Person = person.builder ().id("101") .firstname("hanko java") .lastname("zhou") .build(); Person result = personRepository.save(person); assertNotNull(result); assertEquals(person.getId(),result.getId()); } / * * * through accurate query * / Lastname @ Test void findByLastname () {List < Person > result = personRepository. FindByLastname (" zhou "); assertNotNull(result); Result.foreach (res-> system.out.println (" query result: "+res)); } /** * use Firstname fuzzy query * end Like Contains Contains, i.e. fuzzy query * Like support wildcard Like han will automatically change to han* * Like StartingWith */ @test void findByFirstnameLike() { List<Person> result = personRepository.findByFirstnameLike("han"); assertNotNull(result); Result.foreach (res-> system.out.println (" query result: "+res)); } / Firstname fuzzy query * to * * * Like the Contains Containing end namely fuzzy query * the Contains support wildcards Like han will automatically become * ank / * * * @ Test void findByFirstnameContains() { List<Person> result = personRepository.findByFirstnameContains("ank"); assertNotNull(result); Result.foreach (res-> system.out.println (" query result: "+res)); }Copy the code

Iv. Relevant notes

Springboot DataRepositories can also support complex queries, which require the @Query annotation

@Query("{\n" + " \"match\": {\n" + " \"firstname\": {\n" + " \"query\": \"? 0\"\n" + " }\n" + " }\n" + " }") List<Person> queryByScript(String firstname);Copy the code
  • Corresponding source code

[hanko/springboot-elasticsearch​

gitee.com

] (link.zhihu.com/?target=htt…).

Java Elasticsearch 7.10 tutorial

Java Elasticsearch 7.10

zhuanlan.zhihu.com] (zhuanlan.zhihu.com/p/321248369)