提交 656581f9 编写于 作者: 武汉红喜's avatar 武汉红喜

RestClient使用

上级 e334a254
......@@ -23,6 +23,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.elasticsearch;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
/**
......@@ -21,11 +30,25 @@ public class SimpleController {
@Autowired
private CustomerRepository repository;
@Autowired
private RestClient restClient;
@RequestMapping("/indexExists/{indexName}")
public Boolean indexExists(@PathVariable String indexName) {
return elasticsearchTemplate.indexExists(indexName);
}
@RequestMapping("/search/{indices}")
public List<Customer> query(@PathVariable String indices) {
QueryBuilder queryBuilder= QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("firstName", "Alice"));
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indices)
.withQuery(queryBuilder)
.build();
return elasticsearchTemplate.queryForList(searchQuery, Customer.class);
}
@RequestMapping("/save")
public String testEsRepo() {
saveCustomers();
......@@ -47,6 +70,30 @@ public class SimpleController {
return this.repository.findByLastName("Smith");
}
@PostMapping("/{indices}/_search")
public Object search(@PathVariable String indices,
@RequestBody(required = false) JSONObject query) {
StringBuilder endpoint = new StringBuilder("/").append(indices).append("/_search");
Request request = new Request("POST", endpoint.toString());
request.addParameter("rest_total_hits_as_int", "true");
request.addParameter("ignore_throttled", "true");
try {
if (query != null) {
request.setJsonEntity(query.toString());
}
Response response = restClient.performRequest(request);
return responseToJSONObject(response);
} catch (IOException e) {
e.printStackTrace();
return "error";
}
}
private JSONObject responseToJSONObject(Response response) throws IOException {
String body = EntityUtils.toString(response.getEntity());
return JSON.parseObject(body);
}
private void saveCustomers() {
this.repository.deleteAll();
this.repository.save(new Customer("Alice", "Smith"));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册