JestClient 操作ES的几种常用方法,持续更新

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("offline")
public class JestClientTest {
    @Autowired
    public JestClient jestClient;
    /**
     * 查询文档
     */
    @Test
    public void getDocumentTest() {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("empNo", "123"));
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(30);
        Search search = new Search.Builder(searchSourceBuilder.toString())
                // multiple index or types can be added.
                .addIndex("*dol*")
                .addType("cloud_logs")
                .build();
        try {
            SearchResult result = jestClient.execute(search);
            List> hits = result.getHits(Post.class);
            List articles = result.getSourceAsObjectList(Post.class);
            System.out.println(articles);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 给索引添加别名
     */
    @Test
    public void createAliasTest() {
        List indics = new ArrayList<>();
        indics.add("posts");
        indics.add("postsss");
        try {
            AddAliasMapping addAliasMapping = new AddAliasMapping.Builder(indics, "mypost").build();
            JestResult jestResult = jestClient.execute(new ModifyAliases.Builder(addAliasMapping).build());
            System.out.println("result:" + jestResult.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据index获取别名
     */
    @Test
    public void getIndexAlias() {
        String index = "company";
        try {
            JestResult jestResult = jestClient.execute(new GetAliases.Builder().addIndex(index).build());
            System.out.println(jestResult.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断index是否存在
     */
    @Test
    public void indicesExists(){
        String index = "posoooots";
        IndicesExists indicesExists=new IndicesExists.Builder(index).build();
        JestResult jestResult=null;
        try{
            jestResult = jestClient.execute(indicesExists);
            System.out.println(jestResult.isSucceeded());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * count
     * 必须要指定精确的index
     */
    @Test
    public void countTest() throws IOException {
        Count count = new Count.Builder()
                .addIndex("cvf_2018-10-09")
                .addType("cloud_logs")
                .build();

        CountResult result = jestClient.execute(count);
        System.out.println(result.getCount());
    }

    @Test
    public void getIndexList() throws Exception{
        GetMapping getMapping=new GetMapping.Builder().build();
        JestResult jestResult=jestClient.execute(getMapping);
        System.out.println(jestResult.getJsonString());
    }
}

 

你可能感兴趣的:(java)