Elasticsearch JestClient 使用

一、索引操作

//创建索引
     public static void main(String[] args) throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 创建索引
        CreateIndex createIndex = new CreateIndex.Builder("my_index").build();
        JestResult result = jestClient.execute(createIndex);

        // 5. 输出创建结果
        System.out.println(result.getJsonString());

    }
//删除索引
    public static void main(String[] args) throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 删除索引
        DeleteIndex deleteIndex = new DeleteIndex.Builder("my_index").build();
        JestResult result = jestClient.execute(deleteIndex);

        // 5. 输出创建结果
        System.out.println(result.getJsonString());

    }
//设置Mapping
    public static void main(String[] args) throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 创建 json 格式的 mapping
        /**
         * {
         *     "mappings":{
         *         "properties":{
         *             "field1":{
         *                 "type":"keyword"
         *             },
         *             "field2":{
         *                 "type":"byte"
         *             }
         *         }
         *     }
         * }
         */
        Map<String, Object> map = new HashMap<String, Object>() {
  {
            this.put("mappings", new HashMap<String, Object>() {
  {
                this.put("properties", new HashMap<String, Object>() {
  {
                    this.put("name", new HashMap<String, String>() {
  {
                        //this.put("type", "keyword");
                    }});
                    this.put("age", new HashMap<String, String>() {
  {
                        //this.put("type", "integer");
                    }});
                }});
    
可以使用Elasticsearch的聚合(Aggregation)功能来实现查询某一个字段的出现次数。以下是使用JestClient进行查询的Java代码示例: ```java import io.searchbox.client.JestClient; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import io.searchbox.params.Parameters; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import java.io.IOException; public class ElasticsearchQuery { public static void main(String[] args) throws IOException { // 创建JestClient实例 JestClient jestClient = JestClientFactoryUtil.getJestClient(); // 构建查询语句 Search search = new Search.Builder("{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"aggs\": {\n" + " \"count_by_field\": {\n" + " \"terms\": {\n" + " \"field\": \"field_name.keyword\"\n" + // 要查询的字段名 " }\n" + " }\n" + " }\n" + "}\n") .addIndex("index_name") .addType("doc_type") .setParameter(Parameters.SIZE, 0) .build(); // 执行查询并获取结果 SearchResult result = jestClient.execute(search); // 处理结果 Terms terms = result.getAggregations().getTermsAggregation("count_by_field"); for (Terms.Bucket bucket : terms.getBuckets()) { String fieldValue = bucket.getKeyAsString(); long count = bucket.getDocCount(); System.out.println(fieldValue + "出现次数:" + count); } // 关闭JestClient实例 jestClient.shutdownClient(); } } ``` 在上面的代码示例中,我们使用Elasticsearch的聚合功能来统计某一个字段的出现次数。具体来说,我们通过`AggregationBuilders.terms()`创建了一个Terms聚合,指定了要统计的字段名`field_name.keyword`,并将该聚合命名为`count_by_field`。在执行查询后,我们可以使用`result.getAggregations().getTermsAggregation("count_by_field")`方法获取该聚合的结果,然后遍历每一个Bucket,得到字段值和出现次数。 需要注意的是,由于我们只需要获取聚合结果而不需要查询的具体文档,因此可以将查询的`size`设置为0,从而减少查询的开销。另外,我们也可以使用`QueryBuilders`类来构建查询语句,以实现更复杂的查询需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值