Search in sources :

Example 1 with ExtensionQuery

use of org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery 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)

Example 2 with ExtensionQuery

use of org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery in project xwiki-platform by xwiki.

the class SearchRESTResource method searchGet.

/**
 * @since 3.3M2
 */
@GET
public ExtensionsSearchResult searchGet(@QueryParam(Resources.QPARAM_SEARCH_QUERY) @DefaultValue("") String pattern, @QueryParam(Resources.QPARAM_LIST_START) @DefaultValue("0") int offset, @QueryParam(Resources.QPARAM_LIST_NUMBER) @DefaultValue("-1") int number, @QueryParam(Resources.QPARAM_LIST_REQUIRETOTALHITS) @DefaultValue("true") boolean requireTotalHits) throws QueryException {
    ExtensionQuery query = this.extensionObjectFactory.createExtensionQuery();
    query.setQuery(pattern);
    query.setOffset(offset);
    query.setLimit(number);
    return searchPost(query);
}
Also used : ExtensionQuery(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery) GET(javax.ws.rs.GET)

Aggregations

ExtensionQuery (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionQuery)2 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)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 Query (org.xwiki.query.Query)1 SecureQuery (org.xwiki.query.SecureQuery)1