One : What is? elasticsearch,elasticsearch What is the structure in memory

elasticsearch Is a full-text search engine Apache Lucene Search engine for encapsulation , It can be used for full-text search , Structured search and analysis .

elasticsearch Structure in memory :

Data exists in memory in this form , An index is equivalent to a database , A document is equivalent to a table , file id Property unique . Finally, a pair in the domain is specified json To generate keywords , And pass the generated words to the index (index) assembly .

Two : How to integrate

One : Add dependency
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
</dependency> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
ok!! The first problem arises , Version problem

pringboot There is one spring data assembly , It can be used to connect various data sources . Used to connect elasticsearch Yes
spring-data-elasticsearch. 
spring-data-elasticsearch Update is slow , Its highest version elasticsearch Of 6.x edition .

so, I unified the version as 1.5.9,elasticsearch Version is 2.4.2,ki Version is 4.6.3
<parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version> </parent>
 OK, Another question two ,elasticsearch stay java The port of the client is 9300,Restful
Http The port of is 9200,So configuration file application.yml Write in
spring: data: elasticsearch: cluster-nodes: 127.0.0.1:9300
Two : create object
@Document(indexName = "hello",type = "product") public class product { private
int id; private String name; get.set... }
indexName  Is the name of the index ,type Is the document type , Use both mysql To make a metaphor ,index Equivalent to database name ,type Equivalent to table name

Three : Defining interfaces

Define a proDao Interface ElasticsearchRepository<T, Serializable>

T Class defined for ,Serializable by Integer
public interface proDao extends ElasticsearchRepository<product, Integer>{ }
This is like jpa equally , Ready to eat ,So, Let's have one Controller It's done

Four : write Controller
@RestController public class proController { @Autowired private proDao proDao;
@GetMapping("/add") public void add() { product p = new product(1, " Sunglasses ");
proDao.save(p); System.out.println(" Added successfully !!"); } }
Write an add product , Take a look at the effect .

Five :kibana Take a look at it

There was no index at first , We need to create one ourselves .

step :Setting-》 take iIndex contains time-based events Of √ cancel -》 Enter your index name -》create

Finally, click Discover, You can see it

  complete controller,controller There are two problems : One : No, query Query method , Two :getAll() Method cannot be resolved to after return json

with swagger test , Others are normal .
@RestController public class proController { @Autowired private proDao proDao;
@GetMapping("/add") public Object add(@RequestParam("id")int
id,@RequestParam("name")String name) { product p = new product(id,name);
proDao.save(p); return p; } @GetMapping("/delete/{id}") public Object
del(@PathVariable("id")int id) { product p = proDao.findOne(id);
proDao.delete(id); return p; } @GetMapping("/update") public Object
update(@RequestParam("id")int id,@RequestParam("name")String name) { product p
= new product(id, name); proDao.save(p); return p; }
@GetMapping("/getone/{id}") public Object getone(@PathVariable("id")int id) {
product p = proDao.findOne(id); return p ; } @GetMapping("/getAll") public
Object getAll() { Iterable<product> all = proDao.findAll(); for (product
product : all) { System.out.println(product); } return all; } }
swagger Interface

  actually , Finally, there should be a question , Why? elasticsearch This search engine will be so fast ????

ha-ha , I'm also working on it !!!!

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Some East 14 Pay change 16 salary , Sincerity or routine ? Browser kernel ( understand )java Four functional interfaces ( a key , simple )HashMap Explain in detail html Writing about cherry trees , Writing about cherry trees os Simple use of module