查询接口开发完成
This commit is contained in:
@@ -1,19 +1,52 @@
|
|||||||
package top.fjy8018.elasticsearch;
|
package top.fjy8018.elasticsearch;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
|
@Slf4j
|
||||||
public class ElasticsearchApplication {
|
public class ElasticsearchApplication {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TransportClient client;
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String index(){
|
public String index(){
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID查询结果
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/get/book/novel")
|
||||||
|
public ResponseEntity get(@RequestParam(name = "id") String id){
|
||||||
|
if (id==null){
|
||||||
|
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
GetResponse response = this.client.prepareGet("book","novel",id).get();
|
||||||
|
|
||||||
|
if (!response.isExists()){
|
||||||
|
log.error("【查询为空】ID="+id);
|
||||||
|
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity(response.getSource(), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ElasticsearchApplication.class, args);
|
SpringApplication.run(ElasticsearchApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package top.fjy8018.elasticsearch.configeration;
|
package top.fjy8018.elasticsearch.configeration;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.elasticsearch.client.transport.TransportClient;
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
|
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.transport.TransportAddress;
|
import org.elasticsearch.common.transport.TransportAddress;
|
||||||
import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
||||||
@@ -9,12 +11,14 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author F嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-05-19 12:22
|
* @date 2018-05-19 12:22
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
public class ElasticSearchConfiger {
|
public class ElasticSearchConfiger {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,6 +39,12 @@ public class ElasticSearchConfiger {
|
|||||||
|
|
||||||
client.addTransportAddress(node);
|
client.addTransportAddress(node);
|
||||||
|
|
||||||
|
List<DiscoveryNode> nodes = client.listedNodes();
|
||||||
|
log.info("【当前节点】");
|
||||||
|
for(int i = 0 ; i < nodes.size() ; i++) {
|
||||||
|
log.info(nodes.get(i).toString());
|
||||||
|
}
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/main/resources/application.yml
Normal file
5
src/main/resources/application.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
server:
|
||||||
|
servlet:
|
||||||
|
context-path: /es
|
||||||
|
port: 8080
|
||||||
|
debug: true
|
||||||
Reference in New Issue
Block a user