Java Code Examples for org.elasticsearch.client.RestHighLevelClient#index()

The following examples show how to use org.elasticsearch.client.RestHighLevelClient#index() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void index(RestHighLevelClient client, String indexName) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.field("author", "Marker");
        builder.field("title", "Java programing.");
    }
    builder.endObject();
    IndexRequest indexRequest = new IndexRequest(indexName, "_doc", "1").source(builder);

    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    if (indexResponse.status().getStatus() >= 400) {
        String message = "elasticsearch index data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example 2
Source File: Save.java    From code with Apache License 2.0 5 votes vote down vote up
/**
 * HttpHost : url地址封装
 * RestClientBuilder: rest客户端构建器
 * RestHighLevelClient: rest高级客户端
 * IndexRequest: 新增或修改请求
 * IndexResponse:新增或修改的响应结果
 */
public static void main(String[] args) throws IOException {
    //1.连接rest接口
    HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
    // rest构建器
    RestClientBuilder builder = RestClient.builder(http);
    // 高级客户端对象
    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
    //2.封装请求对象
    IndexRequest indexRequest = new IndexRequest("sku", "doc", "3");
    Map<String, Object> skuMap = new HashMap<String, Object>();
    skuMap.put("name", "华为p30pro");
    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<String, Object> spec = new HashMap<String, Object>();
    spec.put("网络制式", "移动4G");
    spec.put("屏幕尺寸", "5");
    skuMap.put("spec", spec);
    indexRequest.source(skuMap);
    //3.获取响应结果
    IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    int status = response.status().getStatus();
    System.out.println("响应状态:" + status);
    restHighLevelClient.close();
}
 
Example 3
Source File: IndexApiMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
private static void indexAll() throws IOException {

        RestHighLevelClient client = HighLevelClient.getInstance();

        int count = 10000;
        StringBuilder content = new StringBuilder();
        for (int i=1; i<=count; i++){
            IndexRequest indexRequest = new IndexRequest("jingma2_test", "testlog", String.valueOf(i));
            int interestCount = random.nextInt(4)+1;
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("name","马靖"+i);
            data.put("age",30);
            boolean isFirst = true;
            for (int index = 0; index<interestCount; index++){
                if(isFirst){
                    content.append(interests[random.nextInt(interests.length)]);
                    isFirst = false;
                }else{
                    content.append(",").append(interests[random.nextInt(interests.length)]);
                }
            }
            data.put("interests",content.toString());
            content.setLength(0);
            indexRequest.source(buildIndexData());

            client.index(indexRequest);
        }

        HighLevelClient.close();
    }
 
Example 4
Source File: IndexApiMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
private static void index() throws IOException {
    IndexRequest indexRequest = new IndexRequest("jingma2_test", "testlog", "1");
    indexRequest.source(buildIndexData());

    RestHighLevelClient client = HighLevelClient.getInstance();
    client.index(indexRequest);
    HighLevelClient.close();
}