package org.fanli.elastic;
|
|
import java.io.IOException;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import org.apache.http.HttpHost;
|
import org.elasticsearch.action.DocWriteRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.support.WriteRequest;
|
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.index.VersionType;
|
import org.yeshi.utils.JsonUtil;
|
|
public class ADDUtils {
|
|
private static String clusterName = "my-application";
|
private static String host = "192.168.1.200";
|
private static Integer port = 9200;
|
|
// 相当于数据库名称
|
public static String indexName = "shose";
|
|
// 初始化api客户端
|
public static RestHighLevelClient client = new RestHighLevelClient(
|
RestClient.builder(new HttpHost(host, port, "http")));
|
|
public static void add1() {
|
IndexRequest request = new IndexRequest("posts");
|
request.id("1");
|
String jsonString = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
|
+ "\"message\":\"trying out Elasticsearch\"" + "}";
|
request.source(jsonString, XContentType.JSON);
|
|
try {
|
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void add2() {
|
Map<String, Object> jsonMap = new HashMap<>();
|
jsonMap.put("user", "kimchy");
|
jsonMap.put("postDate", new Date());
|
jsonMap.put("message", "trying out Elasticsearch");
|
IndexRequest request = new IndexRequest("posts").id("1").source(jsonMap);
|
|
/* 可选参数 */
|
// 路由
|
request.routing("routing");
|
|
// 超时
|
request.timeout(TimeValue.timeValueSeconds(1));
|
request.timeout("1s");
|
|
// 刷新策略
|
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
|
request.setRefreshPolicy("wait_for");
|
|
// 版本要求
|
request.version(2);
|
// 版本类型
|
request.versionType(VersionType.EXTERNAL);
|
|
// 操作类型
|
request.opType(DocWriteRequest.OpType.CREATE);
|
request.opType("create");
|
|
// 索引文档之前要执行的线的名称
|
request.setPipeline("pipeline");
|
}
|
|
public static void add3() {
|
// 提供为的文档源,该源Map自动转换为JSON格式
|
try {
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
builder.startObject();
|
{
|
builder.field("user", "kimchy2");
|
builder.timeField("postDate", new Date());
|
builder.field("message", "trying ");
|
}
|
builder.endObject();
|
IndexRequest request = new IndexRequest("posts").id("2").source(builder);
|
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
|
// // 作为Object密钥对提供的文档源,被转换为JSON格式
|
// IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "kimchy", "postDate", new Date(),
|
// "message", "trying out Elasticsearch");
|
}
|
|
|
public static void add4() {
|
// Product product = new Product();
|
// product.setId("995");
|
// product.setName("测试中的5");
|
// product.setPrice("494985");
|
// product.setDetail("测试进欧冠任何5");
|
//
|
String json = JsonUtil.getSimpleGson().toJson("");
|
|
IndexRequest request = new IndexRequest("shose").id("1");
|
request.source(json, XContentType.JSON);
|
|
|
|
// 同步执行
|
try {
|
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
|
// // 异步执行
|
// ActionListener listener = new ActionListener<IndexResponse>() {
|
// @Override
|
// public void onResponse(IndexResponse indexResponse) {
|
//
|
// }
|
//
|
// @Override
|
// public void onFailure(Exception e) {
|
//
|
// }
|
// };
|
// client.indexAsync(request, RequestOptions.DEFAULT, listener);
|
}
|
|
public static void main(String[] args) {
|
// add1();
|
|
add3();
|
|
// add4();
|
System.out.println("添加成功");
|
}
|
|
}
|
|