package com.wzw.springbootelasticsearch.DocumentAPIs;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
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.RestHighLevelClient;
import org.elasticsearch.common.Strings;
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.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
@Slf4j
public class Document_Index {
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
public void testIndex() throws IOException {
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);
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");
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
@Test
public void testIndex2() throws IOException {
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");
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
@Test
public void testIndex3() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field("user", "kimchy");
builder.timeField("postDate", new Date());
builder.field("message", "trying out Elasticsearch");
}
builder.endObject();
IndexRequest request = new IndexRequest("posts")
.id("1").source(builder);
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");
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
@Test
public void testIndex4() throws IOException {
IndexRequest request = new IndexRequest("posts")
.id("1")
.source("user", "kimchy",
"postDate", new Date(),
"message", "trying out Elasticsearch");
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");
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
}
网友评论