ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

操作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();
    }
}

 

posted on 2019-11-24 08:58  哦哟这个怎么搞  阅读(900)  评论(0编辑  收藏  举报