我编写了查询 Elastic Search 索引的 JAVA 代码(由 Elastic Cloud 提供服务——尽管我认为这与这个问题无关)。
如果没有查询词,该函数会按预期返回索引中的所有文档。
当我使用 Elastic 的 QueryBuilder 语法(这是 Elastic 的 Java 高级 REST 客户端的一部分)添加搜索查询时,找不到匹配项。
RestHighLevelClient client = createHighLevelRestClient();
int numberOfSearchHitsToReturn = 100; // defaults to 10
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.size(numberOfSearchHitsToReturn);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
String[] includeFields = colNames.toArray(new String[colNames.size()]);
String[] excludeFields = new String[] {}; // just need an exclude field in order to call
// fetchSource
sourceBuilder.fetchSource(includeFields, excludeFields);
sourceBuilder.from(offset);
sourceBuilder.size(limitParam);
sourceBuilder.query(QueryBuilders.termQuery("firstname", query));
SearchRequest searchRequest = new SearchRequest("contacts_" + list_id).source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
即使 query = "RICARDO" 并且我确实在此索引中有一个文档,其 firstname 字段中的值为 "ricardo",hits 也是空的。大小写无关紧要......将查询设置为“ricardo”也不会带回任何匹配项。
为什么是这样?
这是我实现高级 REST 客户端库的问题,因为查询在 POSTMAN 中按预期工作......
GET https://elastic:hWWVNZEk<hidden>7a6620ba18623.us-east-1.aws.found.io:9243/contacts_6/_search
{
"query":
{
"term":
{
"firstname":
{
"value": "ricardo"
}
}
}
}
确实回来了……
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.5263605,
"hits": [
{
"_index": "contacts_6_twtoatx8yv",
"_type": "_doc",
"_id": "2098",
"_score": 3.5263605,
"_source": {
"list_id": "6",
"contact_id": "2098",
"firstname": "RICARDO",
"middlename": "",
"lastname": "SMITH"
}
}
]
}
}