zoukankan      html  css  js  c++  java
  • java操作elecsearch

    操作Java

    import org.elasticsearch.action.bulk.BulkRequestBuilder;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.get.GetRequestBuilder;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.index.IndexRequestBuilder;
    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.sort.SortOrder;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    import org.junit.Test;
    
    import java.net.InetAddress;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class sad {
    
        public TransportClient getClient() throws Exception{
            Settings settings = Settings.builder()
                    .put("client.transport.sniff", true).build();
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
            return client;
        }
        //保存数据
        @Test
        public void save() throws Exception {
            TransportClient client = getClient();
            IndexRequestBuilder builder = client.prepareIndex("crm", "employee", "" + 1);
            Map map = new HashMap<String,String>();
            map.put("name","jiedada");
            map.put("id",1);
            map.put("age",18);
            builder.setSource(map).get();
            client.close();
        }
        //查询数据
        @Test
        public void find() throws Exception {
            TransportClient client = getClient();
            GetResponse response = client.prepareGet("crm", "employee", "1").get();
            System.out.println(response.getSource());
            client.close();
        }
        //批量添加elec
        @Test
        public void findBatch() throws Exception {
            TransportClient client = getClient();
    
            BulkRequestBuilder builder = client.prepareBulk();
            Map map = new HashMap();
            map.put("id",1);
            map.put("name","jiedada");
            map.put("age",18);
            IndexRequestBuilder request = client.prepareIndex("crm","employee","1").setSource(map);
            Map map2 = new HashMap();
            map2.put("id",2);
            map2.put("name","laojie");
            map2.put("age",40);
            IndexRequestBuilder request2 = client.prepareIndex("crm","employee","2").setSource(map2);
            builder.add(request);
            builder.add(request2);//该方法ES默认是分片1秒钟后刷新,即插入成功后马上查询,插入的数据不能马上被查出
            BulkResponse response = builder.get();
            System.out.println(response);
            if(response.hasFailures()) {
                System.out.println("操作失败");
            }
        }
        //条件查询
        @Test
        public void serchBatch() throws Exception {
            TransportClient client = getClient();
            //是往那个索引库中查询数据
            SearchRequestBuilder builder = client.prepareSearch("crm")
                    .setTypes("employee");
            //查询的范围
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            List<QueryBuilder> must = boolQuery.must(); //query bool must
            must.add(QueryBuilders.matchQuery("name","jiedada")); //可以添加多个
            //filter
            boolQuery.filter(QueryBuilders.rangeQuery("age").gte(10).lte(30));//query bool filter
            //query
            builder.setQuery(boolQuery); //query bool
            //排序
            builder.addSort("age", SortOrder.ASC);
            //分页
            builder.setFrom(0).setSize(10);
            //展示内容
            SearchResponse response = builder.get();
            //获取总数
            SearchHits hits = response.getHits(); //搜索出来东西都放到这个对象里面,里面包含总数+数据
            System.out.println("总数:"+hits.totalHits());
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit searchHit : searchHits) {
                System.out.println(searchHit.getSource());
            }
            client.close();
        }
    }
  • 相关阅读:
    直击 KubeCon 2019 现场,阿里云 Hands-on Workshop 亮点回顾
    分享 KubeCon 2019 (上海)关于 Serverless 及 Knative 相关演讲会议
    MaxCompute 费用暴涨之新增SQL分区裁剪失败
    UI2CODE复杂背景无法识别?闲鱼工程师这样打造高准确率方案
    阿里云发布边缘容器,云边端一体化时代来临
    中间件性能挑战赛上线了两大黑科技,是高手就盘它!!
    MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大
    通知: Spring Cloud Alibaba 仓库迁移
    MaxCompute 项目子账号做权限管理
    性能压测工具选型对比
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11921312.html
Copyright © 2011-2022 走看看