项目整合,增加测试项目,es改为rest客户端接口查询
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.zyplayer.doc.test;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 程序启动器
|
||||
*/
|
||||
@EnableScheduling
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.zyplayer.doc.test", "com.zyplayer.doc.data"})
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(Application.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.zyplayer.doc.test.config;
|
||||
|
||||
import org.dozer.DozerBeanMapperBuilder;
|
||||
import org.dozer.DozerConverter;
|
||||
import org.dozer.Mapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@Configuration
|
||||
public class MapperConfig {
|
||||
|
||||
@Bean
|
||||
public Mapper dozerBeanMapper() {
|
||||
DozerBeanMapperBuilder builder = DozerBeanMapperBuilder.create()
|
||||
.withCustomConverter(new DateStringConvert(Date.class, String.class))
|
||||
.withCustomConverter(new BigdecimalToStringConvert(BigDecimal.class, String.class));
|
||||
return builder.build();
|
||||
// return DozerBeanMapperBuilder.buildDefault();
|
||||
}
|
||||
|
||||
private class DateStringConvert extends DozerConverter<Date, String> {
|
||||
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public DateStringConvert(Class<Date> prototypeA, Class<String> prototypeB) {
|
||||
super(prototypeA, prototypeB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertTo(Date source, String destination) {
|
||||
destination = dateFormat.format(source);
|
||||
return destination;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertFrom(String source, Date destination) {
|
||||
try {
|
||||
destination = dateFormat.parse(source);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
|
||||
private class BigdecimalToStringConvert extends DozerConverter<BigDecimal, String> {
|
||||
|
||||
public BigdecimalToStringConvert(Class<BigDecimal> prototypeA, Class<String> prototypeB) {
|
||||
super(prototypeA, prototypeB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertTo(BigDecimal source, String destination) {
|
||||
return source.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal convertFrom(String source, BigDecimal destination) {
|
||||
return BigDecimal.valueOf(Double.parseDouble(source));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.zyplayer.doc.test.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* 接口文档,方便直接测试
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-07-07
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig {
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("接口文档")
|
||||
.description("欢迎使用")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
//package com.zyplayer.doc.test.elasticsearch;
|
||||
//
|
||||
//import cn.hutool.core.date.DateTime;
|
||||
//import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
//import com.zyplayer.doc.core.json.ResponseJson;
|
||||
//import io.swagger.annotations.ApiImplicitParam;
|
||||
//import org.elasticsearch.action.ActionListener;
|
||||
//import org.elasticsearch.action.delete.DeleteResponse;
|
||||
//import org.elasticsearch.action.get.GetResponse;
|
||||
//import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
//import org.elasticsearch.action.get.MultiGetResponse;
|
||||
//import org.elasticsearch.action.index.IndexRequest;
|
||||
//import org.elasticsearch.action.search.MultiSearchResponse;
|
||||
//import org.elasticsearch.action.search.SearchRequest;
|
||||
//import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
//import org.elasticsearch.action.search.SearchResponse;
|
||||
//import org.elasticsearch.action.update.UpdateRequest;
|
||||
//import org.elasticsearch.client.transport.TransportClient;
|
||||
//import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
//import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
//import org.elasticsearch.index.query.QueryBuilder;
|
||||
//import org.elasticsearch.index.query.QueryBuilders;
|
||||
//import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
||||
//import org.elasticsearch.index.reindex.DeleteByQueryAction;
|
||||
//import org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder;
|
||||
//import org.elasticsearch.script.ScriptType;
|
||||
//import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
|
||||
//import org.elasticsearch.search.SearchHit;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.web.bind.annotation.*;
|
||||
//
|
||||
//import java.util.*;
|
||||
//
|
||||
///**
|
||||
// * 测试es的控制器,依据官方文档7.2.0版本做的一些测试方法
|
||||
// * 不建议使用了,使用 rest 的客户端
|
||||
// * @author 暮光:城中城
|
||||
// * @since 2019年7月14日
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/zyplayer-doc-test/es")
|
||||
//public class TestEsController {
|
||||
// private static Logger logger = LoggerFactory.getLogger(TestEsController.class);
|
||||
//
|
||||
// @Autowired(required = false)
|
||||
// private TransportClient transportClient;
|
||||
//
|
||||
// private static final String ES_INDEX = "zyplayer_doc_test";
|
||||
// private static final String ES_INDEX_2 = "zyplayer_doc_test2";
|
||||
// private static final String ES_TYPE = "_doc";
|
||||
//
|
||||
// @GetMapping("/list")
|
||||
// public ResponseJson<Object> list() {
|
||||
// QueryBuilder queryBuilder = QueryBuilders.boolQuery();
|
||||
//
|
||||
// SearchRequestBuilder requestBuilder = transportClient.prepareSearch(ES_INDEX).setTypes(ES_TYPE)
|
||||
// .setQuery(queryBuilder)
|
||||
// .setFrom(0).setSize(100).setExplain(true);
|
||||
// SearchResponse response = requestBuilder.execute().actionGet();
|
||||
// List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
// for (SearchHit searchHit : response.getHits().getHits()) {
|
||||
// resultList.add(searchHit.getSourceAsMap());
|
||||
// }
|
||||
// return DocResponseJson.ok(resultList);
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/execute")
|
||||
// @ApiImplicitParam(name = "sql", value = "执行的DSL表达式", example = "{\"query\":{\"match\":{\"content\":\"测试\"}}}")
|
||||
// public ResponseJson<Object> execute(@RequestBody String sql) {
|
||||
// SearchRequest searchRequest = new SearchRequest().indices(ES_INDEX).types(ES_TYPE);
|
||||
// // 执行查询,测试语句:{"query":{"match":{"content":"测试"}}}
|
||||
// SearchResponse searchResponse = new SearchTemplateRequestBuilder(transportClient)
|
||||
// .setScript(sql)
|
||||
// .setScriptType(ScriptType.INLINE)
|
||||
// .setScriptParams(new HashMap<>())
|
||||
// .setRequest(searchRequest)
|
||||
// .get()
|
||||
// .getResponse();
|
||||
// // 解析结果
|
||||
// List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
// for (SearchHit searchHit : searchResponse.getHits().getHits()) {
|
||||
// Map<String, Object> sourceMap = searchHit.getSourceAsMap();
|
||||
// if (sourceMap != null) {
|
||||
// resultList.add(sourceMap);
|
||||
// }
|
||||
// }
|
||||
// return DocResponseJson.ok(resultList);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/get/{id}")
|
||||
// public ResponseJson<Object> getById(@PathVariable String id) {
|
||||
// // 通过ID获取
|
||||
// GetResponse response = transportClient.prepareGet(ES_INDEX, ES_TYPE, id).get();
|
||||
// return DocResponseJson.ok(response.getSource());
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/delete/{id}")
|
||||
// public ResponseJson<Object> deleteById(@PathVariable String id) {
|
||||
// // 通过ID删除
|
||||
// DeleteResponse response = transportClient.prepareDelete(ES_INDEX, ES_TYPE, id).get();
|
||||
// return DocResponseJson.ok(response.status());
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/delete")
|
||||
// public ResponseJson<Object> delete() {
|
||||
// // 条件删除
|
||||
// BulkByScrollResponse response =
|
||||
// new DeleteByQueryRequestBuilder(transportClient, DeleteByQueryAction.INSTANCE)
|
||||
// .filter(QueryBuilders.matchQuery("content", "xx"))
|
||||
// .source(ES_INDEX)
|
||||
// .get();
|
||||
// long deleted = response.getDeleted();
|
||||
// return DocResponseJson.ok(deleted);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/deleteAsync")
|
||||
// public ResponseJson<Object> deleteAsync() {
|
||||
// // 异步删除
|
||||
// new DeleteByQueryRequestBuilder(transportClient, DeleteByQueryAction.INSTANCE)
|
||||
// .filter(QueryBuilders.matchQuery("content", "测试"))
|
||||
// .source(ES_INDEX)
|
||||
// .execute(new ActionListener<BulkByScrollResponse>() {
|
||||
// @Override
|
||||
// public void onResponse(BulkByScrollResponse response) {
|
||||
// long deleted = response.getDeleted();
|
||||
// logger.info("已删除:{}", deleted);
|
||||
// }
|
||||
// @Override
|
||||
// public void onFailure(Exception e) {
|
||||
// logger.error("删除失败", e);
|
||||
// }
|
||||
// });
|
||||
// return DocResponseJson.ok();
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/update/{id}")
|
||||
// public ResponseJson<Object> update(@PathVariable String id) {
|
||||
// // 使用UpdateRequest更新
|
||||
// UpdateRequest updateRequest = new UpdateRequest();
|
||||
// updateRequest.index(ES_INDEX);
|
||||
// updateRequest.type(ES_TYPE);
|
||||
// updateRequest.id(id);
|
||||
// try {
|
||||
// updateRequest.doc(XContentFactory.jsonBuilder()
|
||||
// .startObject()
|
||||
// .field("content", "测试更新")
|
||||
// .endObject());
|
||||
// transportClient.update(updateRequest).get();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// // 使用prepareUpdate更新
|
||||
// try {
|
||||
// transportClient.prepareUpdate(ES_INDEX, ES_TYPE, id)
|
||||
// .setDoc(XContentFactory.jsonBuilder()
|
||||
// .startObject()
|
||||
// .field("content", "测试更新")
|
||||
// .endObject())
|
||||
// .get();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return DocResponseJson.ok();
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/upsert/{id}")
|
||||
// @ApiImplicitParam(name = "index", value = "选择索引,可选:1、2")
|
||||
// public ResponseJson<Object> upsert(@PathVariable String id, String index) {
|
||||
// try {
|
||||
// String indexUp = Objects.equals(index, "1") ? ES_INDEX : ES_INDEX_2;
|
||||
// IndexRequest indexRequest = new IndexRequest(indexUp, ES_TYPE, id)
|
||||
// .source(XContentFactory.jsonBuilder()
|
||||
// .startObject()
|
||||
// .field("content", "测试更新或新增-新增")
|
||||
// .field("createTime", DateTime.now().toString())
|
||||
// .endObject());
|
||||
// UpdateRequest updateRequest = new UpdateRequest(indexUp, ES_TYPE, id)
|
||||
// .doc(XContentFactory.jsonBuilder()
|
||||
// .startObject()
|
||||
// .field("content", "测试更新或新增-更新")
|
||||
// .endObject())
|
||||
// .upsert(indexRequest);
|
||||
// transportClient.update(updateRequest).get();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return DocResponseJson.ok();
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/multiGet")
|
||||
// public ResponseJson<Object> multiGet() {
|
||||
// MultiGetResponse multiGetItemResponses = transportClient.prepareMultiGet()
|
||||
// .add(ES_INDEX, ES_TYPE, "1") // 通过单ID查
|
||||
// .add(ES_INDEX, ES_TYPE, "2", "3", "4") // 通过多ID查
|
||||
// .add(ES_INDEX_2, ES_TYPE, "1") // 同时查询另一个索引
|
||||
// .get();
|
||||
// List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
// for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
|
||||
// GetResponse response = itemResponse.getResponse();
|
||||
// if (response.isExists()) {
|
||||
// resultList.add(response.getSource());
|
||||
// }
|
||||
// }
|
||||
// return DocResponseJson.ok(resultList);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/multiSearch")
|
||||
// public ResponseJson<Object> multiSearch(String content) {
|
||||
// SearchRequestBuilder srb1 = transportClient.prepareSearch(ES_INDEX).setQuery(QueryBuilders.matchQuery("content", content)).setSize(10);
|
||||
// SearchRequestBuilder srb2 = transportClient.prepareSearch(ES_INDEX_2).setQuery(QueryBuilders.matchQuery("content", content)).setSize(10);
|
||||
// MultiSearchResponse multiSearchResponse = transportClient.prepareMultiSearch().add(srb1).add(srb2).get();
|
||||
// List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
// for (MultiSearchResponse.Item item : multiSearchResponse) {
|
||||
// for (SearchHit searchHit : item.getResponse().getHits().getHits()) {
|
||||
// Map<String, Object> sourceMap = searchHit.getSourceAsMap();
|
||||
// if (sourceMap != null) {
|
||||
// resultList.add(sourceMap);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return DocResponseJson.ok(resultList);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/terminateAfter")
|
||||
// public ResponseJson<Object> terminateAfter() {
|
||||
// SearchResponse sr = transportClient.prepareSearch(ES_INDEX)
|
||||
// .setTerminateAfter(1000)
|
||||
// .get();
|
||||
// if (sr.isTerminatedEarly()) {
|
||||
// // We finished early
|
||||
// return DocResponseJson.ok("成功");
|
||||
// }
|
||||
// return DocResponseJson.warn("失败");
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/other")
|
||||
// public ResponseJson<Object> other() {
|
||||
// // 都是些官网上的一些例子,不细跑了
|
||||
// BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
|
||||
// queryBuilder.must(QueryBuilders.matchQuery("content","xxx"));
|
||||
// queryBuilder.must(QueryBuilders.multiMatchQuery("xxx", "content", "name"));
|
||||
// queryBuilder.must(QueryBuilders.commonTermsQuery("content","xxx"));
|
||||
// queryBuilder.must(QueryBuilders.queryStringQuery("+xxx -aa"));
|
||||
// queryBuilder.must(QueryBuilders.simpleQueryStringQuery("+xxx -aa"));
|
||||
// // term query
|
||||
// queryBuilder.must(QueryBuilders.termQuery("content","xxx"));
|
||||
// queryBuilder.must(QueryBuilders.termsQuery("xxx", "content", "name"));
|
||||
// queryBuilder.must(QueryBuilders.rangeQuery("age")
|
||||
// .from(5)
|
||||
// .to(10)
|
||||
// .includeLower(true) // 相当于 >=
|
||||
// .includeUpper(false)); // 相当于 < ,true 表示 <=
|
||||
// queryBuilder.must(QueryBuilders.rangeQuery("age")
|
||||
// .gte("10")
|
||||
// .lt("20"));
|
||||
// queryBuilder.must(QueryBuilders.existsQuery("name"));
|
||||
// queryBuilder.must(QueryBuilders.prefixQuery("content", "xxx"));
|
||||
// queryBuilder.must(QueryBuilders.wildcardQuery("content", "k?mch*"));
|
||||
// queryBuilder.must(QueryBuilders.regexpQuery("content", "s.*y"));
|
||||
// queryBuilder.must(QueryBuilders.fuzzyQuery("content", "xxx"));
|
||||
// queryBuilder.must(QueryBuilders.idsQuery().addIds("1", "2", "3"));
|
||||
// queryBuilder.must(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("content","xxx")).boost(2.0f));
|
||||
// // 如果满足了这些条件的,则降低分数,原分数*0.5 则表示下降了一半
|
||||
// queryBuilder.must(QueryBuilders.boostingQuery(
|
||||
// QueryBuilders.termQuery("name","kimchy"),
|
||||
// QueryBuilders.termQuery("name","dadoonet"))
|
||||
// .negativeBoost(0.5f));
|
||||
// // 7.x 才支持
|
||||
//// queryBuilder.must(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("content","xxx")).boost(2.0f).tieBreaker(0.7f));
|
||||
// // filter:匹配内容必须出现在文档中,但不参与评分
|
||||
// queryBuilder.filter(QueryBuilders.matchQuery("content","xxx"));
|
||||
//
|
||||
//
|
||||
// SearchRequestBuilder requestBuilder = transportClient.prepareSearch(ES_INDEX).setTypes(ES_TYPE)
|
||||
// .setQuery(queryBuilder).setFrom(0).setSize(100).setExplain(true);
|
||||
// SearchResponse response = requestBuilder.execute().actionGet();
|
||||
// List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
// for (SearchHit searchHit : response.getHits().getHits()) {
|
||||
// resultList.add(searchHit.getSourceAsMap());
|
||||
// }
|
||||
// return DocResponseJson.ok(resultList);
|
||||
// }
|
||||
//}
|
||||
//
|
||||
@@ -0,0 +1,195 @@
|
||||
package com.zyplayer.doc.test.elasticsearch;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.data.service.elasticsearch.entity.EsWikiPage;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.get.*;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
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.Strings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.script.mustache.SearchTemplateRequest;
|
||||
import org.elasticsearch.script.mustache.SearchTemplateResponse;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 测试es的控制器,依据官方文档7.2.0版本做的一些测试方法
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年7月14日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/zyplayer-doc-test/es-rest")
|
||||
public class TestEsRestController {
|
||||
private static Logger logger = LoggerFactory.getLogger(TestEsRestController.class);
|
||||
|
||||
private static final String ES_INDEX = "zyplayer_doc_test";
|
||||
private static final String ES_INDEX_2 = "zyplayer_doc_test2";
|
||||
private static final String ES_TYPE = "_doc";
|
||||
|
||||
// rest请求客户端
|
||||
private RestHighLevelClient client = new RestHighLevelClient(
|
||||
RestClient.builder(
|
||||
new HttpHost("127.0.0.1", 9200, "http")
|
||||
));
|
||||
|
||||
@GetMapping("/list")
|
||||
public ResponseJson<Object> list(String keywords) throws IOException {
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||
sourceBuilder.query(QueryBuilders.termQuery("content", keywords));
|
||||
sourceBuilder.from(0);
|
||||
sourceBuilder.size(10);
|
||||
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
||||
|
||||
String[] includeFields = new String[] {"content", "*Time"};
|
||||
String[] excludeFields = new String[] {"user"};
|
||||
sourceBuilder.fetchSource(includeFields, excludeFields);
|
||||
|
||||
SearchRequest searchRequest = new SearchRequest();
|
||||
searchRequest.indices(ES_INDEX);
|
||||
searchRequest.source(sourceBuilder);
|
||||
|
||||
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||
|
||||
List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
|
||||
resultList.add(searchHit.getSourceAsMap());
|
||||
}
|
||||
return DocResponseJson.ok(resultList);
|
||||
}
|
||||
|
||||
@PostMapping("/execute")
|
||||
@ApiImplicitParam(name = "sql", value = "执行的DSL表达式", example = "{\"query\":{\"match\":{\"content\":\"测试\"}}}")
|
||||
public ResponseJson<Object> execute(@RequestBody String sql) throws IOException {
|
||||
SearchTemplateRequest request = new SearchTemplateRequest();
|
||||
request.setRequest(new SearchRequest(ES_INDEX).types(ES_TYPE));
|
||||
|
||||
request.setScriptType(ScriptType.INLINE);
|
||||
request.setScript(sql);
|
||||
// 可使用动态参数,{{field}},然后map为值
|
||||
// Map<String, Object> scriptParams = new HashMap<>();
|
||||
// scriptParams.put("field", "title");
|
||||
// scriptParams.put("value", "elasticsearch");
|
||||
// scriptParams.put("size", 5);
|
||||
// request.setScriptParams(scriptParams);
|
||||
|
||||
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
|
||||
|
||||
List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
for (SearchHit searchHit : response.getResponse().getHits()) {
|
||||
resultList.add(searchHit.getSourceAsMap());
|
||||
}
|
||||
return DocResponseJson.ok(resultList);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public ResponseJson<Object> getById(@PathVariable String id) throws Exception {
|
||||
GetRequest getRequest = new GetRequest(ES_INDEX, id);
|
||||
String[] includes = new String[]{"content", "*Time"};
|
||||
String[] excludes = Strings.EMPTY_ARRAY;
|
||||
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
|
||||
getRequest.fetchSourceContext(fetchSourceContext);
|
||||
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
|
||||
return DocResponseJson.ok(getResponse.getSource());
|
||||
}
|
||||
|
||||
@GetMapping("/delete/{id}")
|
||||
public ResponseJson<Object> deleteById(@PathVariable String id) throws IOException {
|
||||
DeleteRequest request = new DeleteRequest(ES_INDEX, id);
|
||||
request.timeout(TimeValue.timeValueMinutes(2));
|
||||
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
|
||||
return DocResponseJson.ok(deleteResponse.getResult().name());
|
||||
}
|
||||
|
||||
@GetMapping("/delete")
|
||||
public ResponseJson<Object> delete() {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@GetMapping("/deleteAsync")
|
||||
public ResponseJson<Object> deleteAsync() {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@GetMapping("/update/{id}")
|
||||
public ResponseJson<Object> update(@PathVariable String id, EsWikiPage esWikiPage) throws IOException {
|
||||
UpdateRequest request = new UpdateRequest(ES_INDEX, id);
|
||||
request.timeout(TimeValue.timeValueMinutes(2));
|
||||
// 需要更新的内容
|
||||
esWikiPage.setUpdateTime(new Date());
|
||||
request.doc(JSON.toJSONString(esWikiPage), XContentType.JSON);
|
||||
UpdateResponse deleteResponse = client.update(request, RequestOptions.DEFAULT);
|
||||
return DocResponseJson.ok(deleteResponse.getResult().name());
|
||||
}
|
||||
|
||||
@GetMapping("/upsert/{id}")
|
||||
@ApiImplicitParam(name = "esIndex", value = "选择索引,可选:1、2")
|
||||
public ResponseJson<Object> upsert(@PathVariable String id, String esIndex, EsWikiPage esWikiPage) throws IOException {
|
||||
String indexUp = Objects.equals(esIndex, "1") ? ES_INDEX : ES_INDEX_2;
|
||||
UpdateRequest request = new UpdateRequest(indexUp, id);
|
||||
request.timeout(TimeValue.timeValueMinutes(2));
|
||||
// 需要更新的内容
|
||||
esWikiPage.setUpdateTime(new Date());
|
||||
request.upsert(JSON.toJSONString(esWikiPage), XContentType.JSON);
|
||||
request.docAsUpsert(true);
|
||||
UpdateResponse deleteResponse = client.update(request, RequestOptions.DEFAULT);
|
||||
return DocResponseJson.ok(deleteResponse.getResult().name());
|
||||
}
|
||||
|
||||
@GetMapping("/multiGet")
|
||||
public ResponseJson<Object> multiGet() throws IOException {
|
||||
String[] includes = new String[] {"content", "*Time"};
|
||||
String[] excludes = Strings.EMPTY_ARRAY;
|
||||
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
|
||||
|
||||
MultiGetRequest request = new MultiGetRequest();
|
||||
request.add(new MultiGetRequest.Item(ES_INDEX, "id").fetchSourceContext(fetchSourceContext));
|
||||
request.add(new MultiGetRequest.Item(ES_INDEX_2, "id").fetchSourceContext(fetchSourceContext));
|
||||
|
||||
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
|
||||
|
||||
List<Map<String, Object>> resultList = new LinkedList<>();
|
||||
for (MultiGetItemResponse itemResponse : response.getResponses()) {
|
||||
resultList.add(itemResponse.getResponse().getSource());
|
||||
}
|
||||
return DocResponseJson.ok(resultList);
|
||||
}
|
||||
|
||||
@GetMapping("/multiSearch")
|
||||
public ResponseJson<Object> multiSearch(String content) {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@GetMapping("/terminateAfter")
|
||||
public ResponseJson<Object> terminateAfter() {
|
||||
return DocResponseJson.warn("失败");
|
||||
}
|
||||
|
||||
@GetMapping("/other")
|
||||
public ResponseJson<Object> other() {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user