String query = params.containsKey("q") ? params.getFirst("q") : "*";
String appid = app.isShared() ? ("_" + Config.SEPARATOR + app.getAppIdentifier()) : app.getAppIdentifier();
String type = (!StringUtils.isBlank(typeOverride) && !"search".equals(typeOverride)) ?
typeOverride : params.getFirst(Config._TYPE);
Pager pager = new Pager();
pager.setPage(NumberUtils.toLong(params.getFirst("page"), 0));
pager.setSortby(params.getFirst("sort"));
pager.setDesc(Boolean.parseBoolean(params.containsKey("desc") ? params.getFirst("desc") : "true"));
pager.setLimit(NumberUtils.toInt(params.getFirst("limit"), pager.getLimit()));
queryType = StringUtils.isBlank(queryType) ? params.getFirst("querytype") : queryType;
Map<String, Object> result = new HashMap<String, Object>();
List<P> items = new ArrayList<P>();
if ("id".equals(queryType)) {
String id = params.containsKey(Config._ID) ? params.getFirst(Config._ID) : null;
P obj = search.findById(appid, id);
if (obj != null) {
items = Collections.singletonList(obj);
pager.setCount(1);
}
} else if ("ids".equals(queryType)) {
List<String> ids = params.get("ids");
items = search.findByIds(appid, ids);
pager.setCount(items.size());
} else if ("nearby".equals(queryType)) {
String latlng = params.getFirst("latlng");
if (StringUtils.contains(latlng, ",")) {
String[] coords = latlng.split(",", 2);
String rad = params.containsKey("radius") ? params.getFirst("radius") : null;
int radius = NumberUtils.toInt(rad, 10);
double lat = NumberUtils.toDouble(coords[0], 0);
double lng = NumberUtils.toDouble(coords[1], 0);
items = search.findNearby(appid, type, query, radius, lat, lng, pager);
}
} else if ("prefix".equals(queryType)) {
items = search.findPrefix(appid, type, params.getFirst("field"), params.getFirst("prefix"), pager);
} else if ("similar".equals(queryType)) {
List<String> fields = params.get("fields");
if (fields != null) {
items = search.findSimilar(appid, type, params.getFirst("filterid"),
fields.toArray(new String[]{}), params.getFirst("like"), pager);
}
} else if ("tagged".equals(queryType)) {
List<String> tags = params.get("tags");
if (tags != null) {
items = search.findTagged(appid, type, tags.toArray(new String[]{}), pager);
}
} else if ("in".equals(queryType)) {
items = search.findTermInList(appid, type, params.getFirst("field"), params.get("terms"), pager);
} else if ("terms".equals(queryType)) {
String matchAll = params.containsKey("matchall") ? params.getFirst("matchall") : "true";
List<String> termsList = params.get("terms");
if (termsList != null) {
Map<String, String> terms = new HashMap<String, String>(termsList.size());
for (String termTuple : termsList) {
if (!StringUtils.isBlank(termTuple) && termTuple.contains(Config.SEPARATOR)) {
String[] split = termTuple.split(Config.SEPARATOR, 2);
terms.put(split[0], split[1]);
}
}
if (params.containsKey("count")) {
pager.setCount(search.getCount(appid, type, terms));
} else {
items = search.findTerms(appid, type, terms, Boolean.parseBoolean(matchAll), pager);
}
}
} else if ("wildcard".equals(queryType)) {
items = search.findWildcard(appid, type, params.getFirst("field"), query, pager);
} else if ("count".equals(queryType)) {
pager.setCount(search.getCount(appid, type));
} else {
items = search.findQuery(appid, type, query, pager);
}
result.put("items", items);
result.put("totalHits", pager.getCount());
return result;
}