I wrote JAVA code that queries an Elastic Search index (served by Elastic Cloud - although I don't think that's relevant to this question).
With no query terms, the function returns all documents in the index as expected.
When I add a search query with Elastic's QueryBuilder syntax (this is part of Elastic's High Level REST Client for JAVA), no matches are found.
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();
hits is empty even when query = "RICARDO" and I do in fact have a document in this index with the value "ricardo" in the firstname field. Casing does not matter... setting query to "ricardo" also does not bring back any matches.
Why is this?
It is an issue with my implementation of the high level REST client library because the query works as expected in POSTMAN...
GET https://elastic:hWWVNZEk<hidden>7a6620ba18623.us-east-1.aws.found.io:9243/contacts_6/_search
{
"query":
{
"term":
{
"firstname":
{
"value": "ricardo"
}
}
}
}
Does indeed return...
{
"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"
}
}
]
}
}