欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程

程序员文章站 2023-11-19 16:29:52
前言 在上一篇中介绍了 "ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程 图文详解" ,本篇文章就来讲解下 ElasticSearch 6.x官方Java API的使用。 ElasticSearch JAVA API 目前市面上有几种常见的ElasticSea ......

前言

在上一篇中介绍了elasticsearch实战系列二: elasticsearch的dsl语句使用教程---图文详解,本篇文章就来讲解下 elasticsearch 6.x官方java api的使用。

elasticsearch java api

目前市面上有几种常见的elasticsearch java api架包,jestclient、springboot整合的springdata、spring整合的elasticsearchtemplate、elasticsearch bboss等一些开源架包,上述这些第三方整合的架包中,基本已经支持日常的使用,除了支持的es版本会低一些而已。

本文介绍的是elasticsearch官方的java high level rest client的使用,java high level rest client是elasticsearch官方目前推荐使用的,适用于6.x以上的版本,要求jdk在1.8以上,可以很好的在大版本中进行兼容,并且该架包自身也包含java low level rest client中的方法,可以应对一些特需的情况进行特殊的处理, 它对于一些常用的方法封装restful风格,可以直接对应操作名调用使用即可,支持同步和异步(async)调用。

这里我们的使用也可以直接对应上一篇文章中的dsl语句使用,这样的话可以非常方便的对照和学习使用。

在对下述进行操作时,我们先来看下elasticsearch java high level rest client的初始化连接写法吧。

    resthighlevelclient client = new resthighlevelclient(restclient.builder(new httphost(elasticip, elasticport)));

是不是很简单呢,关闭也很简单,client不为空直接close即可!

一、新增数据

elasticsearch可以直接新增数据,只要你指定了index(索引库名称)和type(类型)即可。在新增的时候你可以自己指定主键id,也可以不指定,由 elasticsearch自身生成。elasticsearch java high level rest client新增数据提供了三种方法,这里我们就来看一下这三种写法吧。

新增数据代码示例一,通过jsonstring进行创建:

    string index = "test1";
    string type = "_doc";
    // 唯一编号
    string id = "1";
    indexrequest request = new indexrequest(index, type, id);

    string jsonstring = "{" + "\"uid\":\"1234\","+ "\"phone\":\"12345678909\","+ "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\","
            + "\"message\":\"xuwujing study elasticsearch\"" + "}";
    request.source(jsonstring, xcontenttype.json);
    indexresponse indexresponse = client.index(request, requestoptions.default);

新增数据代码示例二,通过map创建,会自动转换成json的数据:

    string index = "test1";
    string type = "_doc";
    // 唯一编号
    string id = "1";
    indexrequest request = new indexrequest(index, type, id);
    map<string, object> jsonmap = new hashmap<>();
    jsonmap.put("uid", 1234);
    jsonmap.put("phone", 12345678909l);
    jsonmap.put("msgcode", 1);
    jsonmap.put("sendtime", "2019-03-14 01:57:04");
    jsonmap.put("message", "xuwujing study elasticsearch");
    request.source(jsonmap);
    indexresponse indexresponse = client.index(request, requestoptions.default);

新增数据代码示例三,通过xcontentbuilder对象进行创建:

   string index = "test1";
    string type = "_doc";
    // 唯一编号
    string id = "1";
    indexrequest request = new indexrequest(index, type, id);
    xcontentbuilder builder = xcontentfactory.jsonbuilder();
    builder.startobject();
    {
        builder.field("uid", 1234);
        builder.field("phone", 12345678909l);
        builder.field("msgcode", 1);
        builder.timefield("sendtime", "2019-03-14 01:57:04");
        builder.field("message", "xuwujing study elasticsearch");
    }
    builder.endobject();
    request.source(builder);
    indexresponse indexresponse = client.index(request, requestoptions.default);

上述三种方法中,个人推荐第二种,比较容易理解和使用。

二、创建索引库

在上述示例中,我们通过直接通过创建数据从而创建了索引库,但是没有创建索引库而通过es自身生成的这种并不友好,因为它会使用默认的配置,字段结构都是text(text的数据会分词,在存储的时候也会额外的占用空间),分片和索引副本采用默认值,默认是5和1,es的分片数在创建之后就不能修改,除非reindex,所以这里我们还是指定数据模板进行创建。
使用java api 创建索引库的方法和上述中新增数据的一样,有三种方式,不过这里就只介绍一种。

新增索引库的代码示例:

private static void createindex() throws ioexception {
    string type = "_doc";
    string index = "test1";
    // setting 的值
    map<string, object> setmapping = new hashmap<>();
    // 分区数、副本数、缓存刷新时间
    setmapping.put("number_of_shards", 10);
    setmapping.put("number_of_replicas", 1);
    setmapping.put("refresh_interval", "5s");
    map<string, object> keyword = new hashmap<>();
    //设置类型
    keyword.put("type", "keyword");
    map<string, object> lon = new hashmap<>();
    //设置类型
    lon.put("type", "long");
    map<string, object> date = new hashmap<>();
    //设置类型
    date.put("type", "date");
    date.put("format", "yyyy-mm-dd hh:mm:ss");

    map<string, object> jsonmap2 = new hashmap<>();
    map<string, object> properties = new hashmap<>();
    //设置字段message信息
    properties.put("uid", lon);
    properties.put("phone", lon);
    properties.put("msgcode", lon);
    properties.put("message", keyword);
    properties.put("sendtime", date);
    map<string, object> mapping = new hashmap<>();
    mapping.put("properties", properties);
    jsonmap2.put(type, mapping);

    getindexrequest getrequest = new getindexrequest();
    getrequest.indices(index);
    getrequest.local(false);
    getrequest.humanreadable(true);
    boolean exists2 = client.indices().exists(getrequest, requestoptions.default);
    //如果存在就不创建了
    if(exists2) {
        system.out.println(index+"索引库已经存在!");
        return;
    }
    // 开始创建库
    createindexrequest request = new createindexrequest(index);
    try {
        // 加载数据类型
        request.settings(setmapping);
        //设置mapping参数
        request.mapping(type, jsonmap2);
        //设置别名
        request.alias(new alias("pancm_alias"));
        createindexresponse createindexresponse = client.indices().create(request, requestoptions.default);
        boolean falg = createindexresponse.isacknowledged();
        if(falg){
            system.out.println("创建索引库:"+index+"成功!" );
        }
    } catch (ioexception e) {
        e.printstacktrace();
    }

}

注:创建索引库的时候,一定要先判断索引库是否存在!!!
这里创建索引库的时候顺便也指定了别名(alias),这个别名是一个好东西,使用恰当可以提升查询性能,这里我们留着下次在讲。

三、修改数据

es提供修改api的时候,有两种方式,一种是直接修改,但是若数据不存在会抛出异常,另一种则是存在更新,不存着就插入。相比第一种,第二种会更加好用一些,不过在写入速度上是不如第一种的。

es修改的代码示例:

private static void update() throws ioexception {
    string type = "_doc";
    string index = "test1";
    // 唯一编号
    string id = "1";
    updaterequest upaterequest = new updaterequest();
    upaterequest.id(id);
    upaterequest.index(index);
    upaterequest.type(type);

    // 依旧可以使用map这种集合作为更新条件
    map<string, object> jsonmap = new hashmap<>();
    jsonmap.put("uid", 12345);
    jsonmap.put("phone", 123456789019l);
    jsonmap.put("msgcode", 2);
    jsonmap.put("sendtime", "2019-03-14 01:57:04");
    jsonmap.put("message", "xuwujing study elasticsearch");
    upaterequest.doc(jsonmap);
    // upsert 方法表示如果数据不存在,那么就新增一条
    upaterequest.docasupsert(true);
    client.update(upaterequest, requestoptions.default);
    system.out.println("更新成功!");

}

注:upsert 方法表示如果数据不存在,那么就新增一条,默认是false。

四、删除数据

根据上述的几个操作,想必不用多说,已经知道了是delete方法了,那我们就直接开始吧。

es根据id删除代码示例:

private static void delete() throws ioexception {

    string type = "_doc";
    string index = "test1";
    // 唯一编号
    string id = "1";
    deleterequest deleterequest = new deleterequest();
    deleterequest.id(id);
    deleterequest.index(index);
    deleterequest.type(type);
    // 设置超时时间
    deleterequest.timeout(timevalue.timevalueminutes(2));
    // 设置刷新策略"wait_for"
    // 保持此请求打开,直到刷新使此请求的内容可以搜索为止。此刷新策略与高索引和搜索吞吐量兼容,但它会导致请求等待响应,直到发生刷新
    deleterequest.setrefreshpolicy(writerequest.refreshpolicy.wait_until);
    // 同步删除
    deleteresponse deleteresponse = client.delete(deleterequest, requestoptions.default);
}       

es根据条件进行删除:

   private static void deletebyquery() throws ioexception {
    string type = "_doc";
    string index = "test1";
    deletebyqueryrequest request = new deletebyqueryrequest(index,type);
    // 设置查询条件
    request.setquery(querybuilders.termsquery("uid",1234));
    // 同步执行
    bulkbyscrollresponse bulkresponse = client.deletebyquery(request, requestoptions.default);
}

测试结果

示例图:

查询语句

几个常用的查询api这里就简单的介绍下用法,然后再直接给出所有的查询语句代码。

查询api

  • 等值(term查询:querybuilders.termquery(name,value);
  • 多值(terms)查询:querybuilders.termsquery(name,value,value2,value3...);
  • 范围(range)查询:querybuilders.rangequery(name).gte(value).lte(value);
  • 存在(exists)查询:querybuilders.existsquery(name);
  • 模糊(wildcard)查询:querybuilders.wildcardquery(name,+value+);
  • 组合(bool)查询: boolquerybuilder boolquerybuilder = new boolquerybuilder();

查询所有代码示例

 private static void allsearch() throws ioexception {
    searchrequest searchrequestall = new searchrequest();
    searchsourcebuilder searchsourcebuilder = new searchsourcebuilder();
    searchsourcebuilder.query(querybuilders.matchallquery());
    searchrequestall.source(searchsourcebuilder);
    // 同步查询
    searchresponse searchresponseall = client.search(searchrequestall, requestoptions.default);
    system.out.println("所有查询总数:" + searchresponseall.gethits().gettotalhits());
}

一般查询代码示例

其实就是等值查询,只不过在里面加入了分页、排序、超时、路由等等设置,并且在查询结果里面增加了一些处理。

   private static void gensearch() throws ioexception {
    string type = "_doc";
    string index = "test1";
    // 查询指定的索引库
    searchrequest searchrequest = new searchrequest(index);
    searchrequest.types(type);
    searchsourcebuilder sourcebuilder = new searchsourcebuilder();
    // 设置查询条件
    sourcebuilder.query(querybuilders.termquery("uid", "1234"));
    // 设置起止和结束
    sourcebuilder.from(0);
    sourcebuilder.size(5);
    sourcebuilder.timeout(new timevalue(60, timeunit.seconds));
    // 设置路由
//      searchrequest.routing("routing");
    // 设置索引库表达式
    searchrequest.indicesoptions(indicesoptions.lenientexpandopen());
    // 查询选择本地分片,默认是集群分片
    searchrequest.preference("_local");

    // 排序
    // 根据默认值进行降序排序
//  sourcebuilder.sort(new scoresortbuilder().order(sortorder.desc));
    // 根据字段进行升序排序
//  sourcebuilder.sort(new fieldsortbuilder("id").order(sortorder.asc));

    // 关闭suorce查询
//  sourcebuilder.fetchsource(false);

    string[] includefields = new string[]{"title", "user", "innerobject.*"};
    string[] excludefields = new string[]{"_type"};
    // 包含或排除字段
//  sourcebuilder.fetchsource(includefields, excludefields);

    searchrequest.source(sourcebuilder);
    system.out.println("普通查询的dsl语句:"+sourcebuilder.tostring());
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);

    // http状态代码、执行时间或请求是否提前终止或超时
    reststatus status = searchresponse.status();
    timevalue took = searchresponse.gettook();
    boolean terminatedearly = searchresponse.isterminatedearly();
    boolean timedout = searchresponse.istimedout();

    // 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
    int totalshards = searchresponse.gettotalshards();
    int successfulshards = searchresponse.getsuccessfulshards();
    int failedshards = searchresponse.getfailedshards();
    // 失败的原因
    for (shardsearchfailure failure : searchresponse.getshardfailures()) {
        // failures should be handled here
    }
    // 结果
    searchresponse.gethits().foreach(hit -> {
        map<string, object> map = hit.getsourceasmap();
        system.out.println("普通查询的结果:" + map);
    });
    system.out.println("\n=================\n");
}

或查询

其实这个或查询也是bool查询中的一种,这里的查询语句相当于sql语句中的

select * from test1 where (uid = 1 or uid =2) and phone = 12345678919

代码示例:

private static void orsearch() throws ioexception {
    searchrequest searchrequest = new searchrequest();
    searchrequest.indices("test1");
    searchrequest.types("_doc");
    searchsourcebuilder searchsourcebuilder = new searchsourcebuilder();
    boolquerybuilder boolquerybuilder = new boolquerybuilder();
    boolquerybuilder boolquerybuilder2 = new boolquerybuilder();
 
      /**
     *  select * from test1 where (uid = 1234 or uid =12345)  and phone = 12345678909
     * */
    boolquerybuilder2.should(querybuilders.termquery("uid", 1234));
    boolquerybuilder2.should(querybuilders.termquery("uid", 12345));
    boolquerybuilder.must(boolquerybuilder2);
    boolquerybuilder.must(querybuilders.termquery("phone", "12345678909"));
    searchsourcebuilder.query(boolquerybuilder);
    system.out.println("或查询语句:" + searchsourcebuilder.tostring());
    searchrequest.source(searchsourcebuilder);
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);

    searchresponse.gethits().foreach(documentfields -> {

        system.out.println("查询结果:" + documentfields.getsourceasmap());
    });

}

模糊查询

相当于sql语句中的like查询。

private static void likesearch() throws ioexception {
    string type = "_doc";
    string index = "test1";
    searchrequest searchrequest = new searchrequest();
    searchrequest.indices(index);
    searchrequest.types(type);
    searchsourcebuilder searchsourcebuilder = new searchsourcebuilder();
    boolquerybuilder boolquerybuilder = new boolquerybuilder();

   /**
     *  select * from p_test where  message like '%xu%';
     * */
    boolquerybuilder.must(querybuilders.wildcardquery("message", "*xu*"));
    searchsourcebuilder.query(boolquerybuilder);
    system.out.println("模糊查询语句:" + searchsourcebuilder.tostring());
    searchrequest.source(searchsourcebuilder);
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);
    searchresponse.gethits().foreach(documentfields -> {
        system.out.println("模糊查询结果:" + documentfields.getsourceasmap());
    });
    system.out.println("\n=================\n");
}

多值查询

也就是相当于sql语句中的in查询。

     private static void insearch() throws ioexception {
        string type = "_doc";
        string index = "test1";
        // 查询指定的索引库
        searchrequest searchrequest = new searchrequest(index,type);
        searchsourcebuilder sourcebuilder = new searchsourcebuilder();
        /**
         *  select * from p_test where uid in (1,2)
         * */
        // 设置查询条件
        sourcebuilder.query(querybuilders.termsquery("uid", 1, 2));
        searchrequest.source(sourcebuilder);
        system.out.println("in查询的dsl语句:"+sourcebuilder.tostring());
        // 同步查询
        searchresponse searchresponse = client.search(searchrequest, requestoptions.default);
        // 结果
        searchresponse.gethits().foreach(hit -> {
            map<string, object> map = hit.getsourceasmap();
            string string = hit.getsourceasstring();
            system.out.println("in查询的map结果:" + map);
            system.out.println("in查询的string结果:" + string);
        });

        system.out.println("\n=================\n");
    }

存在查询

判断是否存在该字段,用法和sql语句中的exist类似。

  private static void existsearch() throws ioexception {
    string type = "_doc";
    string index = "test1";
    // 查询指定的索引库
    searchrequest searchrequest = new searchrequest(index);
    searchrequest.types(type);
    searchsourcebuilder sourcebuilder = new searchsourcebuilder();

    // 设置查询条件
     sourcebuilder.query(querybuilders.existsquery("msgcode"));
    searchrequest.source(sourcebuilder);
    system.out.println("存在查询的dsl语句:"+sourcebuilder.tostring());
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);
    // 结果
    searchresponse.gethits().foreach(hit -> {
        map<string, object> map = hit.getsourceasmap();
        string string = hit.getsourceasstring();
        system.out.println("存在查询的map结果:" + map);
        system.out.println("存在查询的string结果:" + string);
    });
    system.out.println("\n=================\n");
}

范围查询

和sql语句中<>使用方法一样,其中gt是大于,lt是小于,gte是大于等于,lte是小于等于。

private static void rangesearch() throws ioexception{
    string type = "_doc";
    string index = "test1";
    searchrequest searchrequest = new searchrequest(index);
    searchrequest.types(type);
    searchsourcebuilder sourcebuilder = new searchsourcebuilder();

    // 设置查询条件
    sourcebuilder.query(querybuilders.rangequery("sendtime").gte("2019-01-01 00:00:00").lte("2019-12-31 23:59:59"));
    searchrequest.source(sourcebuilder);
     system.out.println("范围查询的dsl语句:"+sourcebuilder.tostring());
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);
    // 结果
    searchresponse.gethits().foreach(hit -> {
        string string = hit.getsourceasstring();
        system.out.println("范围查询的string结果:" + string);
    });
    system.out.println("\n=================\n");
}

正则查询

es可以使用正则进行查询,查询方式也非常的简单,代码示例如下:

 private static void regexpsearch() throws ioexception{
    string type = "_doc";
    string index = "test1";
    // 查询指定的索引库
    searchrequest searchrequest = new searchrequest(index);
    searchrequest.types(type);
    searchsourcebuilder sourcebuilder = new searchsourcebuilder();
    // 设置查询条件
    sourcebuilder.query(querybuilders.regexpquery("message","xu[0-9]"));
    searchrequest.source(sourcebuilder);
     system.out.println("正则查询的dsl语句:"+sourcebuilder.tostring());
    // 同步查询
    searchresponse searchresponse = client.search(searchrequest, requestoptions.default);
    // 结果
    searchresponse.gethits().foreach(hit -> {
        map<string, object> map = hit.getsourceasmap();
        string string = hit.getsourceasstring();
        system.out.println("正则查询的map结果:" + map);
        system.out.println("正则查询的string结果:" + string);
    });

    system.out.println("\n=================\n");
}

查询测试结果

所有查询总数:6
普通查询的dsl语句:{"from":0,"size":5,"timeout":"60s","query":{"term":{"uid":{"value":"1234","boost":1.0}}}}

=================

或查询语句:{"query":{"bool":{"must":[{"bool":{"should":[{"term":{"uid":{"value":1234,"boost":1.0}}},{"term":{"uid":{"value":12345,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"term":{"phone":{"value":"12345678909","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
或查询结果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}

=================

模糊查询语句:{"query":{"bool":{"must":[{"wildcard":{"message":{"wildcard":"xu","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
模糊查询结果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study elasticsearch}
模糊查询结果:{uid=123456, phone=12345678909, message=xu1, sendtime=2019-03-14 01:57:04}

=================

存在查询的dsl语句:{"query":{"exists":{"field":"msgcode","boost":1.0}}}
存在查询的map结果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study elasticsearch}
存在查询的string结果:{"uid":12345,"phone":123456789019,"msgcode":2,"sendtime":"2019-03-14 01:57:04","message":"xuwujing study elasticsearch"}
存在查询的map结果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}
存在查询的string结果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}

=================

范围查询的dsl语句:{"query":{"range":{"sendtime":{"from":"2019-01-01 00:00:00","to":"2019-12-31 23:59:59","include_lower":true,"include_upper":true,"boost":1.0}}}}
范围查询的string结果:{"uid":12345,"phone":123456789019,"msgcode":2,"sendtime":"2019-03-14 01:57:04","message":"xuwujing study elasticsearch"}
范围查询的string结果:{"uid":"123456","phone":"12345678909","message":"xu1","sendtime":"2019-03-14 01:57:04"}
范围查询的string结果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}

=================

正则查询的dsl语句:{"query":{"regexp":{"message":{"value":"xu[0-9]","flags_value":65535,"max_determinized_states":10000,"boost":1.0}}}}
正则查询的map结果:{uid=123456, phone=12345678909, message=xu1, sendtime=2019-03-14 01:57:04}
正则查询的string结果:{"uid":"123456","phone":"12345678909","message":"xu1","sendtime":"2019-03-14 01:57:04"}

=================

组合查询的dsl语句:{"query":{"bool":{"must":[{"term":{"uid":{"value":12345,"boost":1.0}}},{"term":{"msgcode":{"value":1,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
组合查询的string结果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}

=================

其它

参考es官方文档:

关于springboot集成elasticsearch和jestclient的使用可以查看这篇文章:springboot整合elasticsearch实现多版本的兼容

关于elasticsearch java api的选择,如果elasticsearch版本在6.x以前的话,推荐使用jestclient。如果是6.x之后并且有意升级到7.x的话,那么直接使用es官方的java high level rest client,因为在7.x之后将直接会舍弃transport client的连接方式,目前spring和springboot集成的es就是使用该方式(不知后续是否会做调整)。

本篇文章的代码已收录在本人的项目中,若有兴趣,欢迎star、fork和issues。
项目地址:

elasticsearch实战系列:
elasticsearch实战系列一: elasticsearch集群+kinaba安装教程
elasticsearch实战系列二: elasticsearch的dsl语句使用教程---图文详解

音乐推荐

原创不易,如果感觉不错,希望给个推荐!您的支持是我写作的最大动力!
版权声明:
作者:虚无境
博客园出处:
csdn出处:    
个人博客出处: