elasticsearch java 基本操作 demo

搜索推荐

2017-07-22

144

0

技术:java8 + elasticsearch

运行环境:IDEA 15.2 + jdk8 + windows 10 + elasticsearch-5.5.0 on windows 10

demo功能:提供一个elasticsearch-5.5.0基础操作demo

文档参考:http://demoworld.tech/books

初始化客户端5.5.0

添加依赖


            org.elasticsearch.client
            transport
            5.5.0
        
        
            junit
            junit
            4.12
添加客户端支持
private static TransportClient client;

static {
    Settings esSettings = Settings.builder()
            .put("cluster.name", "elasticsearch") //设置ES实例的名称
            .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
            .build();
    client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
    //此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
    try {
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

添加索引(这里索引和solr rediearch中是一样的,都是名词)

@Test
public void add() throws InterruptedException {
    Map<String, Object> infoMap = new HashMap<String, Object>();
    infoMap.put("name", "广告信息11 test1");
    infoMap.put("title", "我的广告 test1 java net python");
    infoMap.put("createTime", new Date());
    infoMap.put("count", 1251);
    IndexResponse indexResponse = client.prepareIndex("test1", "info1", "2545").setSource(infoMap).execute().actionGet();
    System.out.println("id:" + indexResponse.getId());
    client.close();
}

常用查询-更多操作请看源码

//范围查询
@Test
public void query() throws Exception {
    //range查询
    QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("count").gt(50);
    SearchResponse searchResponse = client.prepareSearch("test")
            .setTypes("info")
            .setQuery(rangeQueryBuilder)
            .addSort("count", SortOrder.DESC)
            .setSize(20)
            .execute()
            .actionGet();
    SearchHits hits = searchResponse.getHits();
    System.out.println("查到记录数:" + hits.getTotalHits());
    SearchHit[] searchHists = hits.getHits();
    if (searchHists.length > 0) {
        for (SearchHit hit : searchHists) {
            String name = (String) hit.getSource().get("name");
            System.out.format("name:%s ,count :%s \n", name, hit.getSource().get("count"));
        }
    }
}

//相等查询
@Test
public void query1() throws Exception {
    //term查询
    QueryBuilder queryBuilder = QueryBuilders.termQuery("count", 1022);
    SearchResponse searchResponse = client.prepareSearch("test")
            .setTypes("info")
            .setQuery(queryBuilder)
            .addSort("count", SortOrder.DESC)
            .setSize(20)
            .execute()
            .actionGet();
    SearchHits hits = searchResponse.getHits();
    System.out.println("查到记录数:" + hits.getTotalHits());
    SearchHit[] searchHists = hits.getHits();
    if (searchHists.length > 0) {
        for (SearchHit hit : searchHists) {
            String name = (String) hit.getSource().get("name");
            System.out.format("name:%s ,count :%s \n", name, hit.getSource().get("count"));
        }
    }
}

//模糊查询
@Test
public void query2() throws Exception {
    //term查询
    QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("title", "java");
    SearchResponse searchResponse = client.prepareSearch("test")
            .setTypes("info")
            .setQuery(queryBuilder)
            .addSort("count", SortOrder.DESC)
            .setSize(20)
            .execute()
            .actionGet();
    SearchHits hits = searchResponse.getHits();
    System.out.println("查到记录数:" + hits.getTotalHits());
    SearchHit[] searchHists = hits.getHits();
    if (searchHists.length > 0) {
        for (SearchHit hit : searchHists) {
            String name = (String) hit.getSource().get("name");
            System.out.format("name:%s ,count :%s \n", name, hit.getSource().get("count"));
        }
    }
}

测试请自己跑单元测试看吧

es结构图 

es api 帮助

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think