我正在构建一个 Java 应用程序,用于搜索来自 Elasticsearch 的数据(数据来自 kafka 到 logstash,然后是 json 格式的 elasticsearch)。当我使用时,QueryBuilders.queryStringQuery(reqId)
我得到所有结果都没有问题,但是当我使用时,QueryBuilders.termQuery("routingRequestID", reqId);
即使 reqId 存在于 ES 数据中,我也得到 0 次点击。
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
@GetMapping("/q/{reqId}")
public String searchByReqId(@PathVariable("reqId") final String reqId) throws IOException {
String[] indexes = {"devglan-log-test"};
QueryBuilder queryBuilder = QueryBuilders.termQuery("routingRequestID", reqId);
// QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(reqId);
SearchSourceBuilder searchSource = SearchSourceBuilder.searchSource().query(queryBuilder).from(0).size(1000);
System.out.println(searchSource.query());
SearchRequest searchRequest = new SearchRequest(indexes, searchSource);
System.out.println(searchRequest.source().toString());
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.toString());
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
System.out.println(hit.toString());
}
return "success";
}
{
took: 633,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0
},
hits: {
total: {
value: 1,
relation: "eq"
},
max_score: 1.6739764,
hits: [
{
_index: "devglan-log-test",
_type: "_doc",
_id: "k4qAPXEBCzyTR4XVXPb2",
_score: 1.6739764,
_source: {
@version: "1",
message: "
{"requestorRole":"role3", "requestorGivenName":"doe", "requestorSurName":"male",
"requestorOrganizationName":"dob", "reqd":"address",
"requestorC":"city", "routingRequestID":"7778787898778879"}",
@timestamp: "2020-04-03T00:45:53.917Z"
}
}
]
}
}
查询生成searchSource.query()
:
{
"term" : {
"routingRequestID" : {
"value" : "2421",
"boost" : 1.0
}
}
}
查询生成于searchRequest.source().toString()
:
{"from":0,"size":1000,"query":{"term":{"routingRequestID":{"value":"2421","boost":1.0}}}}
结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":null,"hits":[]}}
非常感谢所有帮助,如果您知道如何提供帮助,请不要跳过该帖子。*高五表情符号*