A retrieval
Second, the concept
Third, integration testing ElasticSearch
• 引入spring-boot-starter-data-elasticsearch
• Install Spring Data corresponding version of ElasticSearch
elasticsearch installation:
[root@localhost docker]# docker pull elasticsearch
这种方式下载一般会很慢,或者直接卡死,我们可以使用阿里云的镜像加速服务您可以通过修改 daemon配置文件 /etc/docker/daemon.json 来使用加速器
1 sudo mkdir -p /etc/docker 2 sudo tee /etc/docker/daemon.json <<-'EOF' 3 { 4 "registry-mirrors": ["https:aliyuncs.com"] 5 } 6 EOF 7 sudo systemctl daemon-reload 8 sudo systemctl restart docker
ES mirrored run
[root@localhost ~]# docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES1 c71178df2dd5
-e ES_JAVA_OPTS = "- Xms256m -Xmx256m"
because elasticsearch is the realization of java, the default initial size of the heap memory will be 2G, the virtual machine is insufficient memory error, then will start, so we limit the size of the heap memory by adding a parameter -e .- Xms is the initial heap size, the maximum heap memory -Xmx is used.
using default port 9200 for web communication the case of a distributed, hungry elasticsearch communication between respective nodes need to use port 9300, so we need to do two port mapping.
After a successful start, can be accessed in the browser:
• application.yml arrangement
• Spring Boot automatically configured ElasticsearchRepository, ElasticsearchTemplate, Jest
• Test ElasticSearch
Use springdata integrated ES
Import dependence
1 <!--spring boot 默认使用的是springdata进行操作ES--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 5 </dependency>
Configuration node name and node communication port
Spring: Data: elasticsearch: Cluster - name: the name of the node # elasticsearch Cluster -nodes: between 192.168.124.127:9300 # port communicating nodes
After the start being given, the replacement version
This error is largely due to our version of that springboot and elasticsearch version than matching caused, there are two solutions,
The first is to springboot version upgrade, (but generally after the upgrade may still not match),
The second is to replace the low version of the elastic. [Version matches the reference], (https://github.com/spring-projects/spring-data-elasticsearch), we may ask price for their pom kind of elastic and springdata after analysis of the version, in the end I decided to upgrade or downgrade.
org.elasticsearch.transport.ConnectTransportException: [][192.168.124.127:9300] connect_timeout[30s]
Run ES Mirror
[root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES2 5e9d896dc62c
test
:( two uses official website: https://github.com/spring-projects/spring-data-elasticsearch )
* 1. write a ElasticSearchRepository interface sub-interface
* 2. ElasticSearchTemplate operating ES
1. Program Interface
1 // The interface has two generic, ElasticsearchRepository <T, the extends the Serializable ID> 2 // type of the first data is to be stored, the second type is the primary key . 3 public interface BookRepository the extends ElasticsearchRepository <Book, Integer > { 4 // may be prepared by custom methods, naming specific method can refer to the official documents in the interface . 5 public List <Book> findBookByBookNameLike (String bookName); . 6 }
2, the preparation of an entity class
// @Document role is to specify the entity to be stored class name and type index @Document (indexName = "atguigu", type = "Book" ) public class Book { Private Integer ID; Private String author; Private String bookName ; }
3 test classes
1 @Autowired 2 BookRepository bookRepository; 3 4 @Test 5 public void test02(){ 6 Book book = new Book(); 7 book.setId(1); 8 book.setAuthor("刘猛"); 9 book.setBookName("我是特种兵"); 10 bookRepository.index(book); 11 } 12 13 @Test 14 public void test03(){ 15 for (Book book : bookRepository.findBookByBookNameLike("我")) { 16 System.out.println(book); 17 } 18 }
4 Access results
Use jest integrated ES
jset gitHub(https://github.com/searchbox-io/Jest/tree/master/jest)
1. Import dependence
<!-- https://mvnrepository.com/artifact/io.searchbox/jest --> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.3</version> </dependency>
2. Configure host and port
spring: elasticsearch: jest: uris: http://192.168.124.127:9200
3. Start test
@Autowired JestClient jestClient; @Test public void contextLoads () throws IOException { // ES index (stored) to a document Article This article was Article This article was = new new Article This article was (); article.setAuthor ( "zhangsan" ); article.setId ( . 1 ); article.setTitle ( "good news" ); article.setContent ( "the Hello world" ); // . create an index, then the index to atguigu article in the ES index // build a functional index index index = new new. Index.Builder (Article This article was) .index ( "atguigu") of the type ( "News" ) .build (); // execute jestClient.execute (index); } / ** * Use jest search function to search for the full text Example * / @Test public void Search () throws IOException { // build query rules String JSON = "{\ n-" + "\" query \ ": {\ n-" + "\" match \ ": {\ n-" + "\" Content \ ": \" Hello \ "\ n-" + "} \ n-" + "} \ n-" + "} " ; // building search Search = Search new new Search.Builder (json) .addIndex ( "atguigu") to addType ( "News." ) .Build (); // perform a search and get the search results SearchResult the Result = jestClient.execute (Search); System.out .println (result.getJsonString ()); }