Search in sources :

Example 6 with SecureQuery

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;
}
Also used : ExtensionsSearchResult(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionsSearchResult) SolrDocument(org.apache.solr.common.SolrDocument) Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) ExtensionQuery(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery) SortClause(org.xwiki.extension.repository.xwiki.model.jaxb.SortClause) Filter(org.xwiki.extension.repository.xwiki.model.jaxb.Filter) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SecureQuery(org.xwiki.query.SecureQuery) POST(javax.ws.rs.POST)

Aggregations

SecureQuery (org.xwiki.query.SecureQuery)6 Query (org.xwiki.query.Query)4 ArrayList (java.util.ArrayList)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrDocumentList (org.apache.solr.common.SolrDocumentList)3 QueryException (org.xwiki.query.QueryException)3 SolrDocument (org.apache.solr.common.SolrDocument)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiException (com.xpn.xwiki.XWikiException)1 Calendar (java.util.Calendar)1 List (java.util.List)1 Locale (java.util.Locale)1 POST (javax.ws.rs.POST)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ExtensionQuery (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery)1 ExtensionsSearchResult (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionsSearchResult)1 Filter (org.xwiki.extension.repository.xwiki.model.jaxb.Filter)1 SortClause (org.xwiki.extension.repository.xwiki.model.jaxb.SortClause)1 EntityReference (org.xwiki.model.reference.EntityReference)1