In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use elasticsearch in springBoot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1 install elasticsearch
2 run elasticsearch
Docker run-d-p 9200 Xmx216m 9200-p 9300-e "ES_JAVA_OPTS=-Xms216m-Xmx216m"-- name ES01 elasticsearch:2.4.0
3 Test installation results
4 create a new springbootweb project pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.21.RELEASE com.elasticspring-elasticsearch0.0.1-SNAPSHOTspring-elasticsearchDemo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-elasticsearch Io.searchbox jest org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
Application.properties file
Spring.elasticsearch.jest.uris= http://192.168.1.139:9200
Spring.data.elasticsearch.cluster-name=elasticsearch
Spring.data.elasticsearch.cluster-nodes=192.168.1.139:9300
Spring.data.elasticsearch.repositories.enabled=true
5 Test jest
1 create a new entity class
Public class Article {
@ JestIdprivate Integer id;private String author;private String title;private String content;public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getAuthor () {return author;} public void setAuthor (String author) {this.author = author;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title;} public String getContent () {return content } public void setContent (String content) {this.content = content;}
}
Public class SpringElasticsearchApplicationTests {
@ Autowire JestClient jestClient
[@ Test] (https://my.oschina.net/azibug)public void contextLoads () throws IOException {Article article = new Article (); article.setId (1); article.setTitle ("jestElasticSearch"); article.setAuthor ("mao"); article.setContent ("hello this is jestElasticSearch test") / / build an index function Index index = new Index.Builder (article) .index ("mao") .type ("news") .build (); jestClient.execute (index) } [@ Test] (https://my.oschina.net/azibug)public void search () throws IOException {String index = "{\ n" + "\" query\ ": {\ n" + "\" match\ ": {\ n" + "\" content\ ":\" hello\ "\ n" + "}\ n" + "}\ n" + "}" / / build search function Search search = new Search.Builder (index) .addIndex ("mao"). AddType ("news"). Build (); SearchResult result=jestClient.execute (search); System.out.println (result.getJsonString ());}
}
2 testing using ElasticsearchRepository
Create a new Book entity
/ / specify index and type
@ Document (indexName = "mao", type = "book")
Public class Book {
Private Integer id;private String name;public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} @ Overridepublic String toString () {return "Book {" + "id=" + id + ", name='" + name +'\'+'}';}
}
BookReposity . Java
Public interface BookReposity extends ElasticsearchRepository {
Public List findByNameLike (String name)
}
@ Autowired BookReposity bookReposity; @ Test
@ Testpublic void test2 () {Book book = new Book (); book.setId (1); book.setName ("the Strange of Youth"); bookReposity.index (book);} public void test1 () {
/ / Book book = new Book (); / / book.setId (1); / / book.setName ("the Strange of teenagers"); / / bookReposity.index (book); for (Book book:bookReposity.findByNameLike ("less")) {System.out.println (book.toString ());}}
Test result
After reading the above, do you have any further understanding of how to use elasticsearch in springBoot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.