boolean fqDsl = request.paramAsBoolean("fq.dsl", false);
// build the query
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (q != null) {
QueryBuilder queryBuilder;
if (qDsl) {
queryBuilder = QueryBuilders.wrapperQuery(q);
} else {
queryBuilder = QueryBuilders.queryString(q);
}
searchSourceBuilder.query(queryBuilder);
}
searchSourceBuilder.from(start);
searchSourceBuilder.size(rows);
// parse fl into individual fields
// solr supports separating by comma or spaces
if (fl != null) {
if (!Strings.hasText(fl)) {
searchSourceBuilder.noFields();
} else {
searchSourceBuilder.fields(fl.split("\\s|,"));
}
}
// handle sorting
if (sort != null) {
String[] sorts = Strings.splitStringByCommaToArray(sort);
for (int i = 0; i < sorts.length; i++) {
String sortStr = sorts[i].trim();
int delimiter = sortStr.lastIndexOf(" ");
if (delimiter != -1) {
String sortField = sortStr.substring(0, delimiter);
if ("score".equals(sortField)) {
sortField = "_score";
}
String reverse = sortStr.substring(delimiter + 1);
if ("asc".equals(reverse)) {
searchSourceBuilder.sort(sortField, SortOrder.ASC);
} else if ("desc".equals(reverse)) {
searchSourceBuilder.sort(sortField, SortOrder.DESC);
}
} else {
searchSourceBuilder.sort(sortStr);
}
}
} else {
// default sort by descending score
searchSourceBuilder.sort("_score", SortOrder.DESC);
}
// handler filters
if (fqs != null && !fqs.isEmpty()) {
FilterBuilder filterBuilder = null;
// if there is more than one filter specified build
// an and filter of query filters, otherwise just
// build a single query filter.
if (fqs.size() > 1) {
AndFilterBuilder fqAnd = andFilter();
for (String fq : fqs) {
QueryBuilder queryBuilder = fqDsl ? QueryBuilders.wrapperQuery(fq) : QueryBuilders.queryString(fq);
fqAnd.add(queryFilter(queryBuilder));
}
filterBuilder = fqAnd;
} else {
QueryBuilder queryBuilder = fqDsl ? QueryBuilders.wrapperQuery(fqs.get(0)) : QueryBuilders.queryString(fqs.get(0));
filterBuilder = queryFilter(queryBuilder);
}
searchSourceBuilder.filter(filterBuilder);
}