SpringBoot學習之路---整合jest操作ElasticSearch

我們可以使用java代碼的方式去操作ElasticSearch,而SpringBoot也爲我們提供了整合的方式,整合ElasticSearch並操作的方式有兩種,這一篇博客來記錄其中的一種 (整合jest去操作ES)


老規矩,我們要使用某種技術,那麼就要去導入它的maven相關依賴:

<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>6.3.1</version>
</dependency>

導入相關依賴之後,就可以使用它來操作ElasticSearch(以下簡稱ES)了。jest其實是基於Http風格去操作ES的,關於它的介紹可以去GitHub上查閱官方文檔
.

之後在全局配置文件中配置安裝ES的主機ip地址與端口號:

spring:
  elasticsearch:
    jest:
      uris: http://*********:9200

我們測試操作ES,並且往裏面存對象和取對象,在此之前編寫一個User類,生成Getter/Setter方法:

public class User implements Serializable {
    
    @JestId
    private int user_id;
    private String username;
    private String password;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_id=" + user_id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

@JestId是標註當前屬性爲主鍵,這點和SpringData有點類似,基於ORM的思想。

之後在測試類中編寫代碼,要操作ES需要用到一個類,這個類JestClient在我們導入相關依賴時,SpringBoot就幫我們配置好了,我們可以直接拿來使用。

保存數據到索引中:

@SpringBootTest
class SpringbootElasticsearchApplicationTests {

    @Autowired
    private JestClient jestClient;

    @Test
    void save() {

        User user = new User();
        user.setUser_id(1);
        user.setUsername("leslie");
        user.setPassword("123456");

        Index index = new Index.Builder(user).index("school").type("user").build();

        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

這一行代碼new Index.Builder(user).index("school").type("user").build();作用是把user對象放到索引school的user類型中

之後運行代碼,沒有報錯的話我們直接去瀏覽器中訪問(你安裝es的主機ip地址):9200/school/user/1,因爲瀏覽器默認發送get請求,我們也不需要去改變它,get請求能夠直接查詢數據:

在這裏插入圖片描述
我們可以拿到剛剛存儲的數據。

接下來通過Java代碼去獲取數據:

@Test
    void get(){

        String json = "{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"username\" : \"leslie\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        Search search = new Search.Builder(json).addIndex("school").addType("user").build();

        try {
            SearchResult execute = jestClient.execute(search);
            System.out.println(execute.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

其中new Search.Builder(json).addIndex("school").addType("user").build();作用爲使用ES的查詢表達式(那個json對象)在索引school的user類型查詢,之後調用jestClient.execute(search)執行並返回結果集,打印結果:

Result: {"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"school","_type":"user","_id":"1","_score":0.2876821,"_source":{"user_id":1,"username":"leslie","password":"123456"}}]}}, isSucceeded: true, response code: 200, error message: null

可以看到打印出結果了。這是用jest來操作ES的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章