A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

you丶

初级黑马

  • 黑马币:15

  • 帖子:5

  • 精华:0

[报到] elasticsearch

© you丶 初级黑马   /  2019-9-19 13:47  /  1074 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.one;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.util.HashMap;
import java.util.Map;

public class Test01 {

    public static void main(String[] args) throws Exception {
        // 1.连接接口                   第一个ip地址,第二个端口,第三个协议
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

        // 2.封装请求对象
        IndexRequest indexRequest = new IndexRequest("sku", "doc", "3");
        Map skuMap = new HashMap();
        skuMap.put("name", "魅族");
        skuMap.put("brandName", "魅族");
        skuMap.put("categoryName", "手机");
        skuMap.put("price", 1010221);
        skuMap.put("createTime", "2019-05-01");
        skuMap.put("saleNum", 101021);
        skuMap.put("commentNum", 10102321);
        Map spec = new HashMap();
        spec.put("网络制式", "全网通");
        spec.put("屏幕尺寸", "5");
        skuMap.put("spec", spec);
        indexRequest.source(skuMap);

        // 3.获取执行结果
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        int status = index.status().getStatus();
        System.out.println(status);

        restHighLevelClient.close();
    }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马