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

Java中使用elasticsearch搜索引擎实现简单、修改等操作

程序员文章站 2023-04-08 14:14:07
以下的操作环境为:jdk:1.8;elasticsearch:5.2.0 maven架包下载坐标为: Java创建ES连接工具类: 用Java命令想elasticsearch中插入数据 使用Java根据id查询数据 根据id进行修改数据 ES模糊查询 以上功能本人已亲测过,都能实现,希望这对大家有所 ......

以下的操作环境为:jdk:1.8;elasticsearch:5.2.0

maven架包下载坐标为:

<dependency>
    <groupid>org.elasticsearch.plugin</groupid>
    <artifactid>transport-netty4-client</artifactid>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupid>org.elasticsearch</groupid>
    <artifactid>elasticsearch</artifactid>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupid>org.nlpcn</groupid>
    <artifactid>elasticsearch-sql</artifactid>
    <version>6.3.0.0</version>
</dependency>
<dependency>
    <groupid>com.alibaba</groupid>
    <artifactid>druid</artifactid>
    <version>1.1.9</version>
</dependency>
<dependency>
    <groupid>org.elasticsearch.client</groupid>
    <artifactid>transport</artifactid>
    <version>5.2.0</version>
</dependency>

java创建es连接工具类:

 

//创建连接工具类
public class esclientconnectionutil {
    public static transportclient client=null;
    public final static string host = "192.168.200.211"; //服务器部署
    public final static integer port = 9301; //端口
public static transportclient  getesclientconnection(){
    if (client == null) {
        system.setproperty("es.set.netty.runtime.available.processors", "false");
            try {
                //设置集群名称
                settings settings = settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();
                //创建client
                client = new prebuilttransportclient(settings).addtransportaddress(new inetsockettransportaddress(inetaddress.getbyname(host), port));
            } catch (exception ex) {
                ex.printstacktrace();
                system.out.println(ex.getmessage());
        }
    }
    return client;
}

}

 

用java命令想elasticsearch中插入数据

 

public map<string,object> addtopic(knowledgetopicdto knowledgetopicdto){
    map<string,object> map = new hashmap<>();
 //连接es
    transportclient transportclient =  esclientconnectionutil.getesclientconnection();
    jsonobject json = jsonobject.fromobject(knowledgetopicdto);//后台传过来的对象数据转换成json格式
    try{
        //index 索引名称(相当于数据库) type :类型(相当于数据库中的表)
        indexresponse response = transportclient.prepareindex("knowledge", "knowledge_theme").setsource(json, xcontenttype.json).get();
        if(null !=response.getid()){
            map.put("code",200);
            return map;
        }
    }catch (exception e){
        e.printstacktrace();
        map.put("code",500);
        return map;
    }
    return null;
}

 

使用java根据id查询数据

 

//连接es
    transportclient transportclient =  esclientconnectionutil.getesclientconnection();
//参数:索引名,类型(type) id
getresponse response = client.prepareget("knowledge", "knowledge_theme", "1")
        .setoperationthreaded(false)    // 线程安全
        .get();

 

根据id进行修改数据

 

//knowledgetopic为修改数据的对象
//修改状态后的对象转换成json数据
jsonobject fromobject= jsonobject.fromobject(knowledgetopic);
//参数:索引名,类型(type) id 要修改的json数据:fromobject
updateresponse updateresponse = client.prepareupdate("knowledge", "knowledge_theme", "1")
        .setdoc(fromobject).get();

 

es模糊查询

 

searchresponse searchresponse=null;
//连接elasticsearch
transportclient transportclient =  esclientconnectionutil.getesclientconnection();
searchresponse = client.preparesearch()
                    .setindices("knowledge")
                    .settypes("knowledge_theme")
                     .setsearchtype(searchtype.dfs_query_then_fetch)
                    .setscroll(timevalue.timevalueminutes(30)) //游标维持时间
                    .setsize(2 * 5)//实际返回的数量为10*index的主分片数
                     .setquery(querybuilders.wildcardquery("name", ("*"+name+"*").tolowercase()))  //查询的字段名及值
                    .execute()
                    .actionget();

 

以上功能本人已亲测过,都能实现,希望这对大家有所帮助