use of org.xwiki.query.SecureQuery in project xwiki-platform by xwiki.
the class SearchRESTResource method searchPost.
@POST
public ExtensionsSearchResult searchPost(ExtensionQuery query) throws QueryException {
ExtensionsSearchResult result = this.extensionObjectFactory.createExtensionsSearchResult();
Query solrQuery = this.queryManager.createQuery(toSolrStatement(query.getQuery()), "solr");
// /////////////////
// Search only in the current wiki
// /////////////////
solrQuery.setWiki(this.xcontextProvider.get().getWikiId());
// /////////////////
// Limit and offset
// /////////////////
solrQuery.setLimit(query.getLimit());
solrQuery.setOffset(query.getOffset());
if (query instanceof SecureQuery) {
// Show only what the current user has the right to see
((SecureQuery) query).checkCurrentUser(true);
}
// /////////////////
// Boost
// /////////////////
solrQuery.bindValue("qf", DEFAULT_BOOST);
// /////////////////
// Fields
// /////////////////
solrQuery.bindValue("fl", DEFAULT_FL);
// /////////////////
// Ordering
// /////////////////
// Convert extension ordering into solr ordering
List<String> sortClauses = new ArrayList<>(query.getSortClauses().size() + 1);
for (SortClause sortClause : query.getSortClauses()) {
String solrField = XWikiRepositoryModel.toSolrField(sortClause.getField());
if (solrField != null) {
sortClauses.add(solrField + ' ' + sortClause.getOrder().name().toLowerCase());
}
}
// Set default ordering
if (StringUtils.isEmpty(query.getQuery())) {
// Sort by rating by default when search query is empty
sortClauses.add(XWikiRepositoryModel.toSolrOrderField(RatingExtension.FIELD_AVERAGE_VOTE) + " desc");
sortClauses.add(XWikiRepositoryModel.toSolrOrderField(RatingExtension.FIELD_TOTAL_VOTES) + " desc");
} else {
// Sort by score by default when search query is not empty
sortClauses.add("score desc");
}
solrQuery.bindValue("sort", sortClauses);
// /////////////////
// Filtering
// /////////////////
List<String> fq = new ArrayList<>(query.getFilters().size() + 1);
// TODO: should be filter only on current wiki ?
// We want only valid extensions documents
fq.add(XWikiRepositoryModel.SOLRPROP_EXTENSION_VALIDEXTENSION + ":true");
// Request filters
for (Filter fiter : query.getFilters()) {
String solrField = XWikiRepositoryModel.toSolrField(fiter.getField());
if (solrField != null) {
StringBuilder builder = new StringBuilder();
builder.append(solrField);
builder.append(':');
if (fiter.getComparison() == COMPARISON.EQUAL) {
builder.append(fiter.getValueString());
} else {
builder.append('*' + fiter.getValueString() + '*');
}
fq.add(builder.toString());
}
}
solrQuery.bindValue("fq", fq);
// /////////////////
// Execute
// /////////////////
QueryResponse response = (QueryResponse) solrQuery.execute().get(0);
SolrDocumentList documents = response.getResults();
result.setOffset((int) documents.getStart());
result.setTotalHits((int) documents.getNumFound());
// other searches behavior
if (query.getLimit() != 0) {
for (SolrDocument document : documents) {
result.getExtensions().add(createExtensionVersionFromSolrDocument(document));
}
}
return result;
}
Aggregations