boolean facet = request.paramAsBoolean("facet", false);
boolean qDsl = request.paramAsBoolean("q.dsl", false);
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);
}
// handle highlighting
if (hl) {
// get supported highlighting parameters if they exist
String hlfl = request.param("hl.fl");
int hlsnippets = request.paramAsInt("hl.snippets", 1);
int hlfragsize = request.paramAsInt("hl.fragsize", 100);
String hlsimplepre = request.param("hl.simple.pre");
String hlsimplepost = request.param("hl.simple.post");
HighlightBuilder highlightBuilder = new HighlightBuilder();
if (hlfl == null) {
// run against default _all field
highlightBuilder.field("_all", hlfragsize, hlsnippets);
} else {
String[] hlfls = hlfl.split("\\s|,");
for (String hlField : hlfls) {
// skip wildcarded fields
if (!hlField.contains("*")) {
highlightBuilder.field(hlField, hlfragsize, hlsnippets);
}
}
}
// pre tags
if (hlsimplepre != null) {
highlightBuilder.preTags(hlsimplepre);
}
// post tags
if (hlsimplepost != null) {
highlightBuilder.postTags(hlsimplepost);
}
searchSourceBuilder.highlight(highlightBuilder);
}
// handle faceting
if (facet) {
// get supported facet parameters if they exist
List<String> facetFields = params.get("facet.field");
String facetSort = request.param("facet.sort");
int facetLimit = request.paramAsInt("facet.limit", 100);
List<String> facetQueries = params.get("facet.query");
if (facetFields != null && !facetFields.isEmpty()) {
for (String facetField : facetFields) {
TermsFacetBuilder termsFacetBuilder = new TermsFacetBuilder(facetField);
termsFacetBuilder.size(facetLimit);
termsFacetBuilder.field(facetField);
if (facetSort != null && facetSort.equals("index")) {
termsFacetBuilder.order(TermsFacet.ComparatorType.TERM);
} else {
termsFacetBuilder.order(TermsFacet.ComparatorType.COUNT);
}
searchSourceBuilder.facet(termsFacetBuilder);
}
}
if (facetQueries != null && !facetQueries.isEmpty()) {
for (String facetQuery : facetQueries) {
QueryFacetBuilder queryFacetBuilder = new QueryFacetBuilder(facetQuery);
queryFacetBuilder.query(QueryBuilders.queryString(facetQuery));
searchSourceBuilder.facet(queryFacetBuilder);
}
}
}
// get index and type we want to search against