springboot~集成elasticsearch的jest


jest是一批操作es的http api接口,你可以像使用普法方法一下操作es,在springboot2.3.0之前,JestClient是支持自動注入的,而在2.3.0之后,你必須為JestClient寫一個組件類,通過注入組件類來使用jest,這一點有些麻煩了。
依賴包

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.7</version>
        </dependency>

需要定義注冊類

/**
 * springboot2.3.0之后不支持自動注冊,只能手動寫注冊配置文件.
 */
@Component
public class JestClientConfig {
    @Bean
    public io.searchbox.client.JestClient getJestCline() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder("http://localhost:9200")
                .multiThreaded(true)
                .build());
        return factory.getObject();
    }
}

在程序中使用它

    @Autowired
    JestClient jestClient;
    /**
     * 創建所引
     *
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        User user = new User("1", "張三", "test");
        Index index = new Index.Builder(user).index(INDEX).type(TYPE).build();
        try {
            JestResult jr = jestClient.execute(index);
            System.out.println(jr.isSucceeded());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

數據成功插入
es


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM