码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch 索引文档

时间:2019-08-30 13:38:34      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:for   evel   png   arch   factory   html   ring   ONBUILD   lang   

1. 创建索引名,可前往官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html

package com.ruhuanxingyun.indexDoc;

import org.apache.commons.lang3.time.DateFormatUtils;
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.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Date;

/**
 * @description: 创建索引文档
 * @author: ruphie
 * @date: Create in 2019/8/29 20:16
 * @company: ruhuanxingyun
 */
public class NginxIndexCreate {

    public static void main(String[] args) {
        IndexRequest request = new IndexRequest("nginx_20190832");
        // 文档ID不设置会自动创建
        //request.id("ESgd4GwBL_dFtmUfcPwC");

        RestHighLevelClient client = null;
        try {
            XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
            xContentBuilder.startObject()
                                .timeField("@timestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
                                .field("mac", "00-11-22-33-44-55")
                                .field("sn", "1234567890")
                                .field("productType", "SUV3")
                                .field("status", 200)
                                .timeField("updateTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
                            .endObject();
            request.source(xContentBuilder);
            // 设置操作类型type就必须设置id,否则报错org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: an id must be provided if version type or value are set;
            //request.opType(DocWriteRequest.OpType.CREATE);

            client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            if (response.status() == RestStatus.CREATED) {
                System.out.println(String.format("创建索引名为%s成功!", request.index()));
            } else {
                System.out.println(String.format("创建索引名为%s失败!", request.index()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭资源
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 2. 删除文档,可前往官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete.html

package com.ruhuanxingyun.indexDoc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @description: 删除索引文档
 * @author: rup
 * @date: Create in 2019/8/30 12:36
 * @company: ruhuanxingyun
 */
public class NginxIndexDelete {

    public static void main(String[] args) {
        DeleteRequest request = new DeleteRequest("nginx_20190832", "Eyiy4GwBL_dFtmUfW_yE");

        RestHighLevelClient client = null;
        try {
            client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));
            DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
            if (response.getResult() == DocWriteResponse.Result.DELETED) {
                System.out.println(String.format("删除索引名为%s的文档id为%s成功!", response.getIndex(), response.getId()));
            } else {
                System.out.println(String.format("删除索引名为%s的文档id为%s失败!", response.getIndex(), response.getId()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭资源
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

技术图片

 

技术图片

 

 技术图片

 3. 更新文档,可前往官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html

package com.ruhuanxingyun.indexDoc;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Date;

/**
 * @description: 更新索引文档
 * @author: rup
 * @date: Create in 2019/8/30 12:50
 * @company: ruhuanxingyun
 */
public class NignxIndexPut {

    public static void main(String[] args) {
        UpdateRequest request = new UpdateRequest("nginx_20190832", "ESgd4GwBL_dFtmUfcPwA");

        RestHighLevelClient client = null;
        try {
            XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
            xContentBuilder.startObject()
                    .timeField("@timestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
                    .field("mac", "00-11-22-33-44-77")
                    .field("sn", "1234567890123")
                    .timeField("updateTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
                    .endObject();
            request.doc(xContentBuilder);
            client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));

            UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
            if (response.status() == RestStatus.OK) {
                System.out.println(String.format("更新索引名为%s的文档id为%s成功!", response.getIndex(), response.getId()));
            } else {
                System.out.println(String.format("更新索引名为%s的文档id为%s失败!", response.getIndex(), response.getId()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭资源
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

技术图片

 

技术图片

 

 技术图片

 

 4. 获取文档索引

Elasticsearch 索引文档

标签:for   evel   png   arch   factory   html   ring   ONBUILD   lang   

原文地址:https://www.cnblogs.com/ruhuanxingyun/p/11434385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!