我正在學習 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);
}
}
也歡迎任何改進代碼的指標。我對(現已棄用的)高休息客戶端如何作業沒有先驗知識,所以我實際上不知道構建這種應用程式的最佳方法是什么。
uj5u.com熱心網友回復:
您可以使用SearchResponse<>該類來獲取它,您正在使用product該類來映射hitsJSON api 的搜索回應部分,但它具有您在 Kibana 上獲得的所有元資料(Kibana 還提供了搜索 API 的 JSON 輸出)。
下面是SearchResponse類中的示例欄位,您可以注意到它類似于搜索 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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/471151.html
標籤:爪哇 弹性搜索 elasticsearch-java-api
上一篇:ElasticSearch-在多個欄位中搜索單詞中間
下一篇:如何使用引導程式添加6行段落?
