@Override public void writeResult(Object obj, long searchTime, boolean isSuccess) throws ResultWriterException, IOException { if (!isSuccess) { String errorMsg = null; if (obj == null) { errorMsg = "null"; } else { errorMsg = obj.toString(); } resultWriter .object() .key("status") .value(1) .key("time") .value(Formatter.getFormatTime(searchTime)) .key("total_count") .value(0) .key("error_msg") .value(errorMsg) .endObject(); } else { Result result = (Result) obj; // fieldNames = result.getFieldNameList(); resultWriter .object() .key("status") .value(0) .key("time") .value(Formatter.getFormatTime(searchTime)) .key("start") .value(result.getStart()) .key("total_count") .value(result.getTotalCount()) .key("count") .value(result.getCount()); // .key("field_count").value(fieldNames.length) // .key("fieldname_list") // .array("name"); // // for (int i = 0; i < fieldNames.length; i++) { // resultWriter.value(fieldNames[i]); // } // resultWriter.endArray(); writeBody(result, resultWriter, searchTime); resultWriter.endObject(); resultWriter.done(); } }
public void writeBody(Result result, ResponseWriter resultWriter, long searchTime) throws ResultWriterException { String[] fieldNames = result.getFieldNameList(); String thumbnailField = setting.getThumbnailField(); String titleField = setting.getTitleField(); String bodyField = setting.getBodyField(); Set<String> thumbnailIdSet = findIdList(thumbnailField); Set<String> titleIdSet = findIdList(titleField); Set<String> bodyIdSet = findIdList(bodyField); resultWriter.key("result"); // data Row[] rows = result.getData(); if (rows.length == 0) { resultWriter.array("item").endArray(); } else { resultWriter.array("item"); for (int i = 0; i < rows.length; i++) { Row row = rows[i]; resultWriter.object(); /* * 1. title * */ resultWriter.key("thumbnail"); Iterator<String> iter = thumbnailIdSet.iterator(); String thumbnailData = thumbnailField; while (iter.hasNext()) { String fieldId = iter.next(); String fieldData = getFieldData(fieldId, row, fieldNames); thumbnailData = thumbnailData.replaceAll("\\$" + fieldId, fieldData); } resultWriter.value(thumbnailData); /* * 2. title * */ resultWriter.key("title"); iter = titleIdSet.iterator(); String titleData = titleField; while (iter.hasNext()) { String fieldId = iter.next(); String fieldData = getFieldData(fieldId, row, fieldNames); titleData = titleData.replaceAll("\\$" + fieldId, fieldData); } resultWriter.value(titleData); /* * 3. body * */ String bodyData = bodyField; resultWriter.key("body"); iter = bodyIdSet.iterator(); while (iter.hasNext()) { String fieldId = iter.next(); String fieldData = getFieldData(fieldId, row, fieldNames); bodyData = bodyData.replaceAll("\\$" + fieldId, fieldData); } resultWriter.value(bodyData); resultWriter.endObject(); } resultWriter.endArray(); // GroupResults groupResult = result.getGroupResult(); // // new GroupResultWriter(null).writeBody(groupResult, resultWriter); } }