* @param request the ES RestRequest
* @param response the ES SearchResponse
* @return search results as a SolrDocumentList
*/
private SolrDocumentList convertToSolrDocumentList(RestRequest request, SearchResponse response) {
SolrDocumentList results = new SolrDocumentList();
// get the ES hits
SearchHits hits = response.getHits();
// set the result information on the SolrDocumentList
results.setMaxScore(hits.getMaxScore());
results.setNumFound(hits.getTotalHits());
results.setStart(request.paramAsInt("start", 0));
// loop though the results and convert each
// one to a SolrDocument
for (SearchHit hit : hits.getHits()) {
SolrDocument doc = new SolrDocument();
// always add score to document
doc.addField("score", hit.score());
// attempt to get the returned fields
// if none returned, use the source fields
Map<String, SearchHitField> fields = hit.getFields();
Map<String, Object> source = hit.sourceAsMap();
if (fields.isEmpty()) {
if (source != null) {
for (String sourceField : source.keySet()) {
Object fieldValue = source.get(sourceField);
// ES does not return date fields as Date Objects
// detect if the string is a date, and if so
// convert it to a Date object
if (fieldValue.getClass() == String.class) {
if (datePattern.matcher(fieldValue.toString()).matches()) {
fieldValue = dateFormat.parseDateTime(fieldValue.toString()).toDate();
}
}
doc.addField(sourceField, fieldValue);
}
}
} else {
for (String fieldName : fields.keySet()) {
SearchHitField field = fields.get(fieldName);
Object fieldValue = field.getValue();
// ES does not return date fields as Date Objects
// detect if the string is a date, and if so
// convert it to a Date object
if (fieldValue.getClass() == String.class) {
if (datePattern.matcher(fieldValue.toString()).matches()) {
fieldValue = dateFormat.parseDateTime(fieldValue.toString()).toDate();
}
}
doc.addField(fieldName, fieldValue);
}
}
// add the SolrDocument to the SolrDocumentList
results.add(doc);
}
return results;
}