javaapi文档_走进大数据丨ElasticSearch6.X的JavaAPI使用一

e22fde49e2da1a61c494af8c0b07b2ec.gif

通过Java操作Elastic Search6,环境请看:走进大数据丨 ElasticSearch6.X的JavaAPI环境部署

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.get.MultiGetItemResponse;import org.elasticsearch.action.get.MultiGetResponse;import org.elasticsearch.action.index.IndexRequest;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.action.update.UpdateRequest;import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.Requests;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.TransportAddress;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentFactory;import org.elasticsearch.common.xcontent.XContentType;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.SearchHits;import org.elasticsearch.transport.client.PreBuiltTransportClient;import java.net.InetAddress;import java.util.HashMap;import java.util.Map;

e22fde49e2da1a61c494af8c0b07b2ec.gif

初始化获取Elastic Search链接

    public final static String HOST = "192.168.1.111";    // http请求的端口是9200,客户端是9300    public final static int PORT = 9300;    /**     * getConnection:(获取es连接).     * @author xbq Date:2018年3月21日上午11:52:02     * @return     * @throws Exception     */    @SuppressWarnings({"resource", "unchecked"})    public static TransportClient getConnection() throws Exception {        // 设置集群名称        Settings settings = Settings.builder().put("cluster.name", "plus").build();        // 创建client        TransportClient client = new PreBuiltTransportClient(settings)                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));        //client.close();        return client;    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

创建索引

    public static void createIndex() throws Exception {        TransportClient client = getConnection();        client.admin().indices().prepareCreate("blog12").get();        client.close();    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

删除索引

    public static void deleteIndex() throws Exception {        TransportClient client = getConnection();        client.admin().indices().prepareDelete("blog1").get();        client.close();    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

使用Json文件,创建document

注意:在ES6.x版本之前可以直接使用jaon传,之后更新需要添加参数XContentType.JSON

    public static void createDocument() throws Exception {        //构建一个json文件        String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\","                + "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}";        //创建文档,        IndexResponse indexResponse = getConnection().prepareIndex("blog", "article", "2").setSource(json, XContentType.JSON).execute().actionGet();        //打印返回结果        System.out.println("index"+indexResponse.getIndex());        System.out.println("Type"+indexResponse.getType());        System.out.println("ID"+indexResponse.getId());        System.out.println("version"+indexResponse.getVersion());        System.out.println("Result"+indexResponse.getResult());        getConnection().close();    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

使用map集合创建document
public static void createDocumentmap() throws Exception {        //构建一个map        Map json = new HashMap();        json.put("id","1");        json.put("title","基于Lucene的搜索服务器");        json.put("content","它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");        //创建文档,        IndexResponse indexResponse = getConnection().prepareIndex("blog", "article", "1").setSource(json).execute().actionGet();        //打印返回结果        System.out.println("index: "+indexResponse.getIndex());        System.out.println("Type: "+indexResponse.getType());        System.out.println("ID: "+indexResponse.getId());        System.out.println("version: "+indexResponse.getVersion());        System.out.println("Result: "+indexResponse.getResult());        getConnection().close();    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

使用es提供的建议使用的方式
public static void createDocumentXcontent() throws Exception {        //构建一个Xcontebt        XContentBuilder field = XContentFactory.jsonBuilder()                .startObject()                .field("id", "4")                .field("title", "基于Lucene的搜索服务器")                .field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口")                .endObject();        //创建文档,        IndexResponse indexResponse = getConnection().prepareIndex("blog", "article").setSource(field).execute().actionGet();        //打印返回结果        System.out.println("index: "+indexResponse.getIndex());        System.out.println("Type: "+indexResponse.getType());        System.out.println("ID: "+indexResponse.getId());        System.out.println("version: "+indexResponse.getVersion());        System.out.println("Result: "+indexResponse.getResult());        getConnection().close();    }

e22fde49e2da1a61c494af8c0b07b2ec.gif

eb0002409cc29641c78bb8d308917519.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值