千家信息网

springBoot中elasticsearch如何使用

发表于:2025-12-05 作者:千家信息网编辑
千家信息网最后更新 2025年12月05日,今天就跟大家聊聊有关springBoot中elasticsearch如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1 安装elasti
千家信息网最后更新 2025年12月05日springBoot中elasticsearch如何使用

今天就跟大家聊聊有关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

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">         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文件

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 {

@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("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;}@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("少年的奇特");    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如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0