赞
踩
当前内容主要用于本人学习和复习使用RestTemplate操作Elasticsearch,涉及到简单的查询操作
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
@SpringBootApplication
public class ElasticsearchRestTemplateApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ElasticsearchRestTemplateApplication.class, args);
}
}
这里本人使用restTemplate.getForObject(URL, String.class);,所以后面需要分析结果
创建实体类:Person(对应查询后的返回结果的实体类)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String user;
private String title;
private String desc;
}
使用_search方式返回的结果:
hits->hits->_source集合
使用主键查询,/1方式的返回结果:
直接获取_source即可
public abstract class ElasticsearchJsonFormatUtils { static ObjectMapper mapper = new ObjectMapper(); static JsonFactory factory = mapper.getFactory(); public static <T> List<T> getTargetsFromElasticsearch(String jsonStr, Class<T> clazz) { List<T> ts = new LinkedList<T>(); try { TreeNode hits = findHits(jsonStr); for (int i = 0; i < hits.size(); i++) { TreeNode node = hits.get(i); T t = convertTreeNode(node, clazz); ts.add(t); } return ts; } catch (Exception e) { e.printStackTrace(); } return ts; } private static TreeNode findHits(String jsonStr) { try { JsonParser createParser = factory.createParser(jsonStr); TreeNode readTree = mapper.readTree(createParser); TreeNode hits = readTree.get("hits").get("hits"); return hits; } catch (Exception e) { e.printStackTrace(); return null; } } private static <T> T convertTreeNode(TreeNode node, Class<T> clazz) { T t = null; Iterator<String> fieldNames = node.fieldNames(); while (fieldNames.hasNext()) { String next = fieldNames.next(); if (next.equals("_source")) { TreeNode source = node.get(next); return mapper.convertValue(source, clazz); } } return t; } public static <T> T getTargetFromElasticsearch(String jsonString, Class<T> clazz) { try { JsonParser createParser = factory.createParser(jsonString); TreeNode readTree = mapper.readTree(createParser); TreeNode treeNode = readTree.get("_source"); T value = mapper.convertValue(treeNode, clazz); return value; } catch (IOException e) { e.printStackTrace(); return null; } } }
@RestController public class PersonController { @Autowired RestTemplate restTemplate; private static final String URL = "http://192.168.126.128:9200"; @RequestMapping("/selectAll") public List<?> selectAll() { String resultStr = restTemplate.getForObject(URL + "/accounts/person/_search", String.class); System.out.println(resultStr); List<Person> persons = ElasticsearchJsonFormatUtils.getTargetsFromElasticsearch(resultStr, Person.class); return persons; } @RequestMapping("/selectById/{id}") public Person selectById(@PathVariable("id") Integer id) { String resultStr = restTemplate.getForObject(URL + "/accounts/person/" + id, String.class); System.out.println(resultStr); return ElasticsearchJsonFormatUtils.getTargetFromElasticsearch(resultStr, Person.class); } }
操作成功,其他的删除修改操作可以看前面的文章!
1.使用restTemplate操作Elasticsearch的时候,需要指定当前访问的url,还需要将需要的操作拼接,麻烦
2.获取返回数据的时候,默认结果是不同的,查询所有的时候需要解析两个hits,查询一个的时候只需要一个_source,解析工作复杂
==3.虽然上面的操作实现了基本的操作,但是过程过于复杂,需要分析返回结果=
=
以上纯属个人见解,如有问题请联系本人!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。