在Spring Boot REST API中从Java API Client返回整个Elasticsearch响应(作为



我正在学习Elasticsearch和Java客户端API(目前只是遵循文档),它工作得很好,但我想知道是否有可能从查询返回整个搜索响应?到目前为止,我只得到了"_source"部分来自包含三个类变量的点击。这很好,但我想尝试一个完整的响应,就像我在Kibana控制台看到的那样。以下是我的类和REST点:

客户机配置:

@Configuration
public class ClientConf {

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
}

查询发生的类:

@Component
public class ElasticSearcher {

@Autowired
ClientConf conf;
public List<Product> searchByType(String type) throws Exception{
List<Product> returnProduct = new ArrayList<Product>();

//searches EXACT term
SearchResponse<Product> search = conf.client.search(s -> s
.index("test")
.query(q -> q
.term(t -> t
.field("type")
.value(v -> v.stringValue(type))
)),
Product.class);

List<Hit<Product>> hits = search.hits().hits();


for (Hit<Product> hit: hits) {
returnProduct.add(hit.source());
}

return returnProduct;
}

public void indexProduct(String type, String name, double price) throws ElasticsearchException, IOException {
Product product = new Product(type, name, price);

// I was just trying both variants here
//      IndexResponse response=conf.client.index(i->i
//              .index("test")
//              .document(product)
//              );

IndexRequest<Product> request = IndexRequest.of(i -> i
.index("test")
.document(product)
);

IndexResponse response = conf.client.index(request);
}

public Product getById(String id) throws ElasticsearchException, IOException {

Product product=null;

GetResponse<Product> response = conf.client.get(g -> g
.index("test") 
.id(id),
Product.class      
);

if (response.found()) {
product = response.source();
} 

return product;
}

//this is the one I'm trying to return entire responses with:
public SearchResponse matchNameSearch(String text) throws ElasticsearchException, IOException{

SearchResponse response = conf.client.search(s -> s
.index("test") 
.query(q -> q      
.match(t -> t   
.field("name")  
.query(text)
)
),
Product.class      
);
return response;
}

}

编辑:如果我调用matchNameSearch(我用Postman测试),它返回一个空对象。我假设通过返回response,这是一个SearchResponse类会给我整个响应?

我正在使用的对象类:

//I have lombok
@Data
public class Product {

private String type;
private String name;
private double price;

public Product() {

}

public Product(String type, String name, double price) {
this.type=type;
this.name=name;
this.price=price;
}
}

最后,其余端点:

@RestController
public class SearchController {

@Autowired
ElasticSearcher searcher;

@GetMapping("/search/{type}")
public List<Product> searchByType(@PathVariable("type") String type) throws Exception {
return searcher.searchByType(type);
}

@PostMapping("/index/{type}/{name}/{price}")
public void indexProduct(@PathVariable("type") String type,
@PathVariable("name") String name,
@PathVariable("price") String price) throws ElasticsearchException, IOException {
searcher.indexProduct(type, name, Double.parseDouble(price));
}

@GetMapping("/read/{id}")
public Product searchById(@PathVariable("id") String id) throws ElasticsearchException, IOException {
return searcher.getById(id);
}

//this is the one:
@GetMapping("/match/{text}")
public SearchResponse matchText(@PathVariable("text") String text) throws ElasticsearchException, IOException{
return searcher.matchNameSearch(text);
}
}

也欢迎任何改进代码的建议。我对high rest客户端是如何工作的(现在已经弃用了)一无所知,所以我实际上不知道构建这种应用程序的最佳方法是什么。

您可以使用SearchResponse<>类获得,您正在使用product类来映射JSON api的搜索响应的hits部分,但它具有您在Kibana上获得的所有元数据(Kibana还提供搜索api的JSON输出)。

下面是SearchResponse类中的示例字段,您可以注意到它类似于Search JSON输出。

@JsonpDeserializable
public class SearchResponse<TDocument> implements JsonpSerializable {
private final long took;
private final boolean timedOut;
private final ShardStatistics shards;
private final HitsMetadata<TDocument> hits;
private final Map<String, Aggregate> aggregations;
@Nullable
private final ClusterStatistics clusters;
private final Map<String, JsonData> fields;
@Nullable
private final Double maxScore;
@Nullable
private final Long numReducePhases;
@Nullable
private final Profile profile;
@Nullable
private final String pitId;
@Nullable
private final String scrollId;
private final Map<String, List<Suggestion<TDocument>>> suggest;
@Nullable
private final Boolean terminatedEarly;

最新更新