我一直在使用Elastic Search版本5.6.16和Spring Boot。我想基于字符串进行搜索。为此,我编写了以下代码。
Controller.java
public @ResponseBody String getItem(@RequestParam("data") String data) {
QueryBuilder qb = QueryBuilders.queryStringQuery(data);
SearchResponse searchResponse = client.prepareSearch("index")
.setTypes("default").setQuery(qb).setSize(10).execute().actionGet();
return searchResponse;
}
此方法返回String。但是我想得到结果为List <Student>
。其中Student
类为:
Student.java
@Getter
@Setter
@AllArgsConstructor
@ToString
public class Student {
private int id;
private int name;
}
我正在上传ES索引上的“学生”数据。请让我知道如何从ES获得List的结果
也许此更改将帮助您取得进展:
public @ResponseBody String getItem(@RequestParam("data") String data) {
List<Student> records = new ArrayList<Student>();
QueryBuilder qb = QueryBuilders.queryStringQuery(data);
SearchResponse searchResponse = client.prepareSearch("index")
.setTypes("default").setQuery(qb).setSize(10).execute().actionGet();
SearchHit[] results = response.getHits().getHits();
for(SearchHit hit : results){
String source = hit.getSourceAsString();
Student s = objectMapper.readValue(source,Student.class);
records.add(s);
}
return records;
}