3.2_springBoot2.1.x檢索之JestClient操作ElasticSearch


這里介紹Jest方式交互,

導入jest版本

<!--導入jest-->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>6.3.1</version>
        </dependency>

application.yml配置

#配置jest
spring:
  elasticsearch:
    jest:
      uris: http://Ip:9200

測試:

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03ElasticsearchApplicationTests {
    //自動注入jest
    @Autowired
    JestClient jestClient;
    
    
    //jest
    @Test
    public void contextLoads() {
        //1、給Es中索引(保存)一個文檔
        Article article = new Article();
        article.setId(1);
        article.setTitle("java入門到放棄");
        article.setAuthor("jiatp");
        article.setContent("測試內容");

        //2、構建一個索引功能 type(類型名字)
        Index build = new Index.Builder(article).index("jiatp").type("news").build();
        try {
            //3、執行
            jestClient.execute(build);
        } catch(IOException e) {
            e.printStackTrace();
        }

    }
}

 //測試搜索
    @Test
    public void testSearch(){

        String json = "{\n" + " \"query\" : {\n" + " \"match\" : {\n"
                + " \"content\" : \"測試內容\"\n" + " }\n"
                + " }\n" + "}";

        //構建搜索
        Search search = new Search.Builder(json).addIndex("jiatp").addType("news").build();
        try {
            SearchResult result = jestClient.execute(search);
            System.out.println(result.getJsonString());
        } catch(IOException e) {
            e.printStackTrace();
        }

    }

dfsdsf


免責聲明!

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



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