springboot检索之整合elasticsearch并使用jest操作


1、新建一个springboot项目,选择web、nosql中的elasticsearch。

2、springboot默认使用spring-data操作elasticsearch,也可以使用jest操作elasticsearch。

3、在pom.xml中注释掉spring-data的elasticsearch依赖,并添加一下依赖:

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>2.4.0</version>
        </dependency>

根据elasticsearch版本的不同导入不同版本的jest依赖,我使用的elasticsearch版本是2.4.9。

4、进行测试:

首先要在application.properties中配置连接地址,否则不会启动jest。

spring.elasticsearch.jest.uris=192.168.124.22:9200

我这个版本的这个配置被移除了。。。。,接下去测试不了了,还是写下测试的代码。

Article.java

package com.gong.spingbootes.bean;

import io.searchbox.annotations.JestId;

public class Article {
    @JestId
    private Integer id;
    private String author;
    private String title;
    private String content;

    public Article(){}

    public Article(Integer id, String author, String title, String content) {
        this.id = id;
        this.author = author;
        this.title = title;
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", author='" + author + '\'' +
                ", title='" + title + '\'' +
                ", content='" + 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;
    }
}

SrpingbootEsApplicationTests.java

package com.gong.spingbootes;

import com.gong.spingbootes.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpingbootEsApplicationTests {

    @Autowired
    JestClient jestClient;

    @Test
    public void contextLoads() {
        //给es索引中保存五个文档
        Article article = new Article(1,"好消息","张三","你好啊");
        //构建一个索引功能
        Index index = new Index.Builder(article).index("gong").type("news").build();
        try {
            //执行
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
//查询 @Test
public void search(){ String json="{" + "\"query\" : {" + " \"match\" : {" + " \"content\" : \"smith\" "+ " }" + " }" + "}" ; Search search = new Search.Builder(json).addIndex("gong").addType("news").build(); try { SearchResult searchResult = jestClient.execute(search); System.out.println(searchResult.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM