您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關springBoot中elasticsearch如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1 安裝elasticsearch
2 運行elasticsearch
docker run -d -p 9200:9200 -p 9300:9300 -e "ES_JAVA_OPTS=-Xms216m -Xmx216m" --name ES01 elasticsearch:2.4.0
3 測試安裝結果
4 新建一個springbootweb項目 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.elastic</groupId> <artifactId>spring-elasticsearch</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-elasticsearch</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--SpringBoot默認使用SpringData ElasticSearch模塊進行操作--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> </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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
application.properties文件
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 測試jest
1 新建一個實體類
public class Article {
@JestId private 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("jest ElasticSearch"); article.setAuthor("mao"); article.setContent("hello this is jestElasticSearch test"); //構建一個索引功能 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" + "}"; //構建搜索功能 Search search = new Search.Builder(index).addIndex("mao").addType("news").build(); SearchResult result=jestClient.execute(search); System.out.println(result.getJsonString()); }
}
2 測試 使用 ElasticsearchRepository
新建一個Book實體
//指明索引和類型
@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; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + '}'; }
}
BookReposity。java
public interface BookReposity extends ElasticsearchRepository<Book,Integer> {
public List<Book> findByNameLike(String name) ;
}
@Autowired BookReposity bookReposity; @Test
@Test public void test2(){ Book book = new Book(); book.setId(1); book.setName("少年的奇特"); bookReposity.index(book); } public void test1(){
// Book book = new Book(); // book.setId(1); // book.setName("少年的奇特"); // bookReposity.index(book); for(Book book:bookReposity.findByNameLike("少")){ System.out.println(book.toString()); } }
測試結果
看完上述內容,你們對springBoot中elasticsearch如何使用有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。