Example usage for org.apache.solr.client.solrj SolrQuery addSort

List of usage examples for org.apache.solr.client.solrj SolrQuery addSort

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery addSort.

Prototype

public SolrQuery addSort(String field, ORDER order) 

Source Link

Document

Adds a single sort clause to the end of the current sort information.

Usage

From source file:ddf.catalog.source.solr.SolrFilterDelegate.java

License:Open Source License

private SolrQuery getSolrQueryWithSort(String givenSpatialString) {

    if (sortBy != null && sortBy.getPropertyName() != null
            && Result.DISTANCE.equals(sortBy.getPropertyName().getPropertyName())) {

        String spatialQueryWithDistance = SCORE_DISTANCE + givenSpatialString;

        SolrQuery solrQuery = new SolrQuery(spatialQueryWithDistance);

        solrQuery.setFields("*", "score");

        ORDER sortOrder = ORDER.asc;//  w w w  .java  2 s . c  o m

        if (SortOrder.DESCENDING.equals(sortBy.getSortOrder())) {
            sortOrder = ORDER.desc;
        }

        solrQuery.addSort("score", sortOrder);

        return new SolrQuery(spatialQueryWithDistance);

    } else {
        return new SolrQuery(givenSpatialString);
    }
}

From source file:ddf.catalog.source.solr.SolrMetacardClient.java

License:Open Source License

protected String getSortProperty(QueryRequest request, SolrQuery query) {
    SortBy sortBy = request.getQuery().getSortBy();
    String sortProperty = "";

    if (sortBy != null && sortBy.getPropertyName() != null) {
        sortProperty = sortBy.getPropertyName().getPropertyName();
        SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (sortBy.getSortOrder() == SortOrder.ASCENDING) {
            order = SolrQuery.ORDER.asc;
        }//from ww  w.  j  ava2 s .c  o m

        if (Result.RELEVANCE.equals(sortProperty) || Result.DISTANCE.equals(sortProperty)) {
            query.setFields("*", RELEVANCE_SORT_FIELD);
            query.addSort(RELEVANCE_SORT_FIELD, order);
        } else if (sortProperty.equals(Result.TEMPORAL)) {
            query.addSort(resolver.getField(Metacard.EFFECTIVE, AttributeType.AttributeFormat.DATE, false),
                    order);
        } else {
            List<String> resolvedProperties = resolver.getAnonymousField(sortProperty);

            if (!resolvedProperties.isEmpty()) {
                for (String sortField : resolvedProperties) {
                    query.addSort(sortField, order);
                }

                query.add("fl", "*," + RELEVANCE_SORT_FIELD);
            } else {
                LOGGER.info(
                        "No schema field was found for sort property [{}]. No sort field was added to the query.",
                        sortProperty);
            }

        }

    }
    return sortProperty;
}

From source file:ddf.catalog.source.solr.SolrMetacardClientImpl.java

License:Open Source License

private void addDistanceSort(SolrQuery query, String sortField, SolrQuery.ORDER order,
        SolrFilterDelegate delegate) {//w  w w  . ja  va 2 s.c  om
    if (delegate.isSortedByDistance()) {
        query.addSort(DISTANCE_SORT_FUNCTION, order);
        query.setFields("*", RELEVANCE_SORT_FIELD, DISTANCE_SORT_FIELD + ":" + DISTANCE_SORT_FUNCTION);
        query.add(SORT_FIELD_KEY, sortField);
        query.add(POINT_KEY, delegate.getSortedDistancePoint());
    }
}

From source file:ddf.catalog.source.solr.SolrMetacardClientImpl.java

License:Open Source License

protected String setSortProperty(QueryRequest request, SolrQuery query, SolrFilterDelegate solrFilterDelegate) {
    SortBy sortBy = request.getQuery().getSortBy();
    String sortProperty = "";

    if (sortBy != null && sortBy.getPropertyName() != null) {
        sortProperty = sortBy.getPropertyName().getPropertyName();
        SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (sortBy.getSortOrder() == SortOrder.ASCENDING) {
            order = SolrQuery.ORDER.asc;
        }/*from   w  w w .  j a v a  2s . c  o  m*/

        query.setFields("*", RELEVANCE_SORT_FIELD);

        if (Result.RELEVANCE.equals(sortProperty)) {
            query.addSort(RELEVANCE_SORT_FIELD, order);
        } else if (Result.DISTANCE.equals(sortProperty)) {
            addDistanceSort(query, GEOMETRY_SORT_FIELD, order, solrFilterDelegate);
        } else if (sortProperty.equals(Result.TEMPORAL)) {
            query.addSort(
                    resolver.getSortKey(
                            resolver.getField(Metacard.EFFECTIVE, AttributeType.AttributeFormat.DATE, false)),
                    order);
        } else {
            List<String> resolvedProperties = resolver.getAnonymousField(sortProperty);

            if (!resolvedProperties.isEmpty()) {
                for (String sortField : resolvedProperties) {
                    if (sortField.endsWith(SchemaFields.GEO_SUFFIX)) {
                        addDistanceSort(query, resolver.getSortKey(sortField), order, solrFilterDelegate);
                    } else if (!(sortField.endsWith(SchemaFields.BINARY_SUFFIX)
                            || sortField.endsWith(SchemaFields.OBJECT_SUFFIX))) {
                        query.addSort(resolver.getSortKey(sortField), order);
                    }
                }
            } else {
                LOGGER.debug(
                        "No schema field was found for sort property [{}]. No sort field was added to the query.",
                        sortProperty);
            }

        }

    }
    return resolver.getSortKey(sortProperty);
}

From source file:de.qaware.javaone.solrspark.SolrSpark.java

License:Apache License

/**
 * This method queries SOLR and processes all found documents. For each
 * document the length of the field 'logRawEntry' is returned (The count of
 * unicode chars).//from www .  j a v  a2s. c  om
 *
 * @param query a query string.
 * @param solrRDD the solrRDD Spark abstraction.
 * @param jsc the Spark context.
 * @return the sum of the stringlength of all log messages.
 * @throws SolrServerException When the Solr processing gets into trouble.
 */
private static int queryAndProcess(String query, SolrRDD solrRDD, SparkContext jsc) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery(query);
    solrQuery.addSort("id", SolrQuery.ORDER.asc);

    // 1. Smart filtering by searching
    JavaRDD<SolrDocument> result = solrRDD.query(jsc, solrQuery);

    // 2. Parallel processing of the search results
    // parallel map in the cluster - calculate the sum of bytes from all log messages
    int totalLength = 0;
    if (!result.isEmpty()) {
        totalLength = result.map((SolrDocument d) -> {
            if (d == null) {
                return 0;
            }
            String logEntry = (String) d.getFieldValue("logRawEntry");
            return logEntry != null ? logEntry.length() : 0;
        }).reduce((Integer i, Integer j) -> {
            return i + j;
        });
    }
    return totalLength;
}

From source file:edu.unc.lib.dl.cdr.services.rest.ContainerDataFileChecksumsController.java

License:Apache License

@RequestMapping(value = "/edit/fileinfo/{pid}")
public void get(@PathVariable("pid") String pid, HttpServletResponse response)
        throws SolrServerException, IOException {
    String fid = pid.replace(":", "_");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fid + "-fileinfo.csv\"");
    try (ServletOutputStream out = response.getOutputStream()) {
        out.print("title");
        out.print(',');
        out.print("pid");
        out.print(',');
        out.print("mimetype");
        out.print(',');
        out.print("length");
        out.print(',');
        out.println("checksum");

        if (server == null)
            initializeSolrServer();/*from   w w w.  java2  s . c  om*/
        SolrQuery parameters = new SolrQuery();
        parameters.setQuery("contentModel:" + ClientUtils.escapeQueryChars("info:fedora/cdr-model:Simple")
                + " ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*");
        parameters.addSort("filesizeTotal", ORDER.desc);
        parameters.addField("title");
        parameters.addField("id");
        parameters.addField("datastream");
        QueryResponse solrResponse = server.query(parameters);

        for (SolrDocument doc : solrResponse.getResults()) {
            Map<String, String> line = new HashMap<String, String>();
            line.put("pid", (String) doc.getFieldValue("id"));
            line.put("title", (String) doc.getFieldValue("title"));
            String[] dsValues = new String[5];
            for (Object ds : doc.getFieldValues("datastream")) {
                String dstr = (String) ds;
                if (dstr.startsWith("DATA_FILE|")) {
                    dsValues = dstr.split(Pattern.quote("|"));
                    break;
                }
            }
            line.put("md5sum", dsValues[4]);
            line.put("length", dsValues[3]);
            line.put("mimetype", dsValues[1]);
            outputCSV(line, out);
        }
    }
}

From source file:edu.unc.lib.dl.cdr.services.rest.ContainerManifestController.java

License:Apache License

@SuppressWarnings("unchecked")
@RequestMapping(value = "/edit/manifest-json/{pid}")
public @ResponseBody Map<String, Object> getJSON(@PathVariable("pid") String pid) throws SolrServerException {
    if (server == null)
        initializeSolrServer();/*ww  w  . j  a  va 2  s.  co  m*/
    SolrQuery parameters = new SolrQuery();
    parameters.setQuery("ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*");
    parameters.addSort("ancestorNames", ORDER.asc);
    parameters.addSort("isPart", ORDER.asc);
    parameters.addSort("displayOrder", ORDER.asc);
    parameters.addField("ancestorNames");
    parameters.addField("ancestorPath");
    parameters.addField("id");
    parameters.addField("filesizeTotal");
    parameters.addField("title");
    QueryResponse solrResponse = server.query(parameters);

    // objects map is a local index of pid to map
    Map<String, Map<String, Object>> objects = new HashMap<String, Map<String, Object>>();
    Map<String, Object> coll = new HashMap<String, Object>();
    coll.put("pid", pid);
    String title = tripleStoreQueryService.lookupLabel(pid);
    coll.put("title", title);
    objects.put(pid, coll);
    for (SolrDocument doc : solrResponse.getResults()) {
        Map<String, Object> line = new HashMap<String, Object>();
        objects.put((String) doc.getFieldValue("id"), line);

        Collection<Object> ancestorPaths = doc.getFieldValues("ancestorPath");
        line.put("depth", String.valueOf(ancestorPaths.size()));

        // get parent id
        String[] ancestorPathsArray = ancestorPaths.toArray(new String[] {});
        String lastAncestor = ancestorPathsArray[ancestorPathsArray.length - 1];
        int start = lastAncestor.indexOf(",") + 1;
        int end = lastAncestor.indexOf(",", start);
        String parent = lastAncestor.substring(start, end);

        // file object record
        line.put("pid", doc.getFieldValue("id"));
        line.put("parentPid", parent);
        line.put("title", doc.getFieldValue("title"));
        line.put("filesizeTotal", doc.getFieldValue("filesizeTotal"));
        // TODO get checksum of data file
    }

    for (Map<String, Object> record : objects.values()) {

        if (pid.equals(record.get("pid")))
            continue;
        String parentPid = (String) record.get("parentPid");
        // file object as child
        Map<String, Object> parentHash = objects.get(parentPid);
        if (parentHash == null) {
            LOG.warn("Cannot find expected pid in index: {}", parentPid);
            continue;
        }
        List<Map<String, Object>> children = null;
        if (!parentHash.containsKey("children")) {
            children = new ArrayList<Map<String, Object>>();
            parentHash.put("children", children);
        } else {
            children = (List<Map<String, Object>>) parentHash.get("children");
        }
        children.add(record);
    }
    return coll;
}

From source file:edu.unc.lib.dl.cdr.services.rest.ContainerManifestController.java

License:Apache License

@RequestMapping(value = "/edit/manifest-csv/{pid}")
public void downloadCSV(@PathVariable("pid") String pid, HttpServletResponse response)
        throws SolrServerException, IOException {
    if (server == null)
        initializeSolrServer();/*  w w w .  j  a  v  a2s .c om*/
    SolrQuery parameters = new SolrQuery();
    parameters.setQuery("ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*");
    parameters.addSort("ancestorNames", ORDER.asc);
    parameters.addSort("isPart", ORDER.asc);
    parameters.addSort("displayOrder", ORDER.asc);
    parameters.addField("ancestorPath");
    parameters.addField("ancestorNames");
    parameters.addField("id");
    parameters.addField("title");
    QueryResponse solrResponse = server.query(parameters);

    String id = pid.replace(":", "_");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + id + "-manifest.csv\"");
    try (ServletOutputStream out = response.getOutputStream()) {
        out.print("depth");
        out.print(',');
        out.print("pid");
        out.print(',');
        out.println("title");
        for (SolrDocument doc : solrResponse.getResults()) {
            String title = (String) doc.getFieldValue("title");
            String p = (String) doc.getFieldValue("id");
            String anc = (String) doc.getFieldValue("ancestorNames");
            int depth = doc.getFieldValues("ancestorPath").size();
            outputCSV(p, title, depth, anc, out);
        }
    }
}

From source file:edu.unc.lib.dl.search.solr.service.SolrSearchService.java

License:Apache License

/**
 * Constructs a SolrQuery object from the search state specified within a SearchRequest object. The request may
 * optionally request to retrieve facet results in addition to search results.
 * //from   w ww  .j  a v a2  s.  c o  m
 * @param searchRequest
 * @param isRetrieveFacetsRequest
 * @return
 */
protected SolrQuery generateSearch(SearchRequest searchRequest) {
    SearchState searchState = (SearchState) searchRequest.getSearchState();
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder termQuery = new StringBuilder();

    // Generate search term query string
    addSearchFields(searchState, termQuery);

    // Add range Fields to the query
    addRangeFields(searchState, termQuery);

    // No query terms given, make it an everything query
    StringBuilder query = new StringBuilder();
    if (termQuery.length() == 0) {
        query.append("*:* ");
    } else {
        query.append('(').append(termQuery).append(')');
    }

    // Add access restrictions to query
    try {
        addAccessRestrictions(query, searchRequest.getAccessGroups());
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.debug("User had no access groups", e);
        return null;
    }

    // Add query
    solrQuery.setQuery(query.toString());

    if (searchState.getResultFields() != null) {
        for (String field : searchState.getResultFields()) {
            String solrFieldName = solrSettings.getFieldName(field);
            if (solrFieldName != null)
                solrQuery.addField(solrFieldName);
        }
    }

    if (searchState.getRollup() != null && searchState.getRollup()) {
        solrQuery.set(GroupParams.GROUP, true);
        if (searchState.getRollupField() == null)
            solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(SearchFieldKeys.ROLLUP_ID.name()));
        else
            solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(searchState.getRollupField()));

        solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true);
        if (searchState.getFacetsToRetrieve() != null && searchState.getFacetsToRetrieve().size() > 0) {
            solrQuery.set(GroupParams.GROUP_FACET, true);
        }
    }

    // Add sort parameters
    List<SearchSettings.SortField> sortFields = searchSettings.sortTypes.get(searchState.getSortType());
    if (sortFields != null) {
        for (int i = 0; i < sortFields.size(); i++) {
            SearchSettings.SortField sortField = sortFields.get(i);
            SolrQuery.ORDER sortOrder = SolrQuery.ORDER.valueOf(sortField.getSortOrder());
            if (!searchState.getSortNormalOrder())
                sortOrder = sortOrder.reverse();
            solrQuery.addSort(solrSettings.getFieldName(sortField.getFieldName()), sortOrder);
        }
    }

    // Set requested resource types
    String resourceTypeFilter = this.getResourceTypeFilter(searchState.getResourceTypes());
    if (resourceTypeFilter != null) {
        solrQuery.addFilterQuery(resourceTypeFilter);
    }

    // Turn on faceting
    if (searchRequest.isRetrieveFacets()) {
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        if (searchState.getBaseFacetLimit() != null)
            solrQuery.setFacetLimit(searchState.getBaseFacetLimit());

        if (searchState.getFacetsToRetrieve() != null) {
            // Add facet fields
            for (String facetName : searchState.getFacetsToRetrieve()) {
                String facetField = solrSettings.getFieldName(facetName);
                if (facetField != null)
                    solrQuery.addFacetField(solrSettings.getFieldName(facetName));
            }
        }
    }

    // Override the base facet limit if overrides are given.
    if (searchState.getFacetLimits() != null) {
        for (Entry<String, Integer> facetLimit : searchState.getFacetLimits().entrySet()) {
            solrQuery.add("f." + solrSettings.getFieldName(facetLimit.getKey()) + ".facet.limit",
                    facetLimit.getValue().toString());
        }
    }

    // Add facet limits
    Map<String, Object> facets = searchState.getFacets();
    if (facets != null) {
        Iterator<Entry<String, Object>> facetIt = facets.entrySet().iterator();
        while (facetIt.hasNext()) {
            Entry<String, Object> facetEntry = facetIt.next();

            if (facetEntry.getValue() instanceof String) {
                LOG.debug("Adding facet " + facetEntry.getKey() + " as a String");
                // Add Normal facets
                solrQuery.addFilterQuery(solrSettings.getFieldName(facetEntry.getKey()) + ":\""
                        + SolrSettings.sanitize((String) facetEntry.getValue()) + "\"");
            } else {
                LOG.debug("Adding facet " + facetEntry.getKey() + " as a "
                        + facetEntry.getValue().getClass().getName());
                facetFieldUtil.addToSolrQuery(facetEntry.getValue(), solrQuery);
            }
        }
    }

    // Scope hierarchical facet results to the highest tier selected within the facet tree
    if (searchRequest.isRetrieveFacets() && searchRequest.isApplyCutoffs()
            && searchState.getFacetsToRetrieve() != null) {
        Set<String> facetsQueried = searchState.getFacets().keySet();
        // Apply closing cutoff to all cutoff facets that are being retrieved but not being queried for
        for (String fieldKey : searchState.getFacetsToRetrieve()) {
            if (!facetsQueried.contains(fieldKey)) {
                facetFieldUtil.addDefaultFacetPivot(fieldKey, solrQuery);
            }
        }

        // Add individual facet field sorts if they are present.
        if (searchState.getFacetSorts() != null) {
            for (Entry<String, String> facetSort : searchState.getFacetSorts().entrySet()) {
                solrQuery.add("f." + solrSettings.getFieldName(facetSort.getKey()) + ".facet.sort",
                        facetSort.getValue());
            }
        }
    }

    // Set Navigation options
    if (searchState.getStartRow() != null)
        solrQuery.setStart(searchState.getStartRow());
    if (searchState.getRowsPerPage() != null)
        solrQuery.setRows(searchState.getRowsPerPage());

    return solrQuery;
}

From source file:edu.usu.sdl.openstorefront.service.SearchServiceImpl.java

License:Apache License

@Override
public List<ComponentSearchView> getSearchItems(SearchQuery query, FilterQueryParams filter) {
    // use for advanced search with And - Or combinations on separate fields
    String queryOperator = " " + SolrAndOr.OR + " ";
    String myQueryString;/*from w  w w .  j ava 2 s .co  m*/

    // If incoming query string is blank, default to solar *:* for the full query
    if (StringUtils.isNotBlank(query.getQuery())) {
        StringBuilder queryData = new StringBuilder();

        Field fields[] = SolrComponentModel.class.getDeclaredFields();
        for (Field field : fields) {
            org.apache.solr.client.solrj.beans.Field fieldAnnotation = field
                    .getAnnotation(org.apache.solr.client.solrj.beans.Field.class);
            if (fieldAnnotation != null && field.getType() == String.class) {
                String name = field.getName();
                if (StringUtils.isNotBlank(fieldAnnotation.value())
                        && org.apache.solr.client.solrj.beans.Field.DEFAULT
                                .equals(fieldAnnotation.value()) == false) {
                    name = fieldAnnotation.value();
                }

                queryData.append(SolrEquals.EQUAL.getSolrOperator()).append(name)
                        .append(SolrManager.SOLR_QUERY_SEPERATOR).append(query.getQuery())
                        .append(queryOperator);
            }
        }
        myQueryString = queryData.toString();
        if (myQueryString.endsWith(queryOperator)) {
            queryData.delete((myQueryString.length() - (queryOperator.length())), myQueryString.length());
            myQueryString = queryData.toString();
        }
    } else {
        myQueryString = SolrManager.SOLR_ALL_QUERY;
    }
    log.log(Level.FINER, myQueryString);

    // execute the searchComponent method and bring back from solr a list array
    List<SolrComponentModel> resultsList = new ArrayList<>();
    try {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(myQueryString);

        // fields to be returned back from solr
        solrQuery.setFields(SolrComponentModel.ID_FIELD, SolrComponentModel.ISCOMPONENT_FIELD);
        solrQuery.setStart(filter.getOffset());
        solrQuery.setRows(filter.getMax());

        Field sortField = ReflectionUtil.getField(new SolrComponentModel(), filter.getSortField());
        if (sortField != null) {
            String sortFieldText = filter.getSortField();
            org.apache.solr.client.solrj.beans.Field fieldAnnotation = sortField
                    .getAnnotation(org.apache.solr.client.solrj.beans.Field.class);
            if (fieldAnnotation != null) {
                sortFieldText = fieldAnnotation.value();
            }
            SolrQuery.ORDER order = SolrQuery.ORDER.desc;
            if (OpenStorefrontConstant.SORT_ASCENDING.equalsIgnoreCase(filter.getSortOrder())) {
                order = SolrQuery.ORDER.asc;
            }
            solrQuery.addSort(sortFieldText, order);
        }

        solrQuery.setIncludeScore(true);

        QueryResponse response = SolrManager.getServer().query(solrQuery);
        SolrDocumentList results = response.getResults();
        DocumentObjectBinder binder = new DocumentObjectBinder();
        resultsList = binder.getBeans(SolrComponentModel.class, results);
    } catch (SolrServerException ex) {
        throw new OpenStorefrontRuntimeException("Search Failed",
                "Contact System Admin.  Seach server maybe Unavailable", ex);
    } catch (Exception ex) {
        log.log(Level.WARNING, "Solr query failed unexpectly; likely bad input.", ex);
    }

    //Pulling the full object on the return
    List<ComponentSearchView> views = new ArrayList<>();

    List<String> componentIds = new ArrayList<>();
    for (SolrComponentModel result : resultsList) {
        if (result.getIsComponent()) {
            componentIds.add(result.getId());
        }
    }

    //remove bad indexes, if any
    List<ComponentSearchView> componentSearchViews = getComponentService().getSearchComponentList(componentIds);
    Set<String> goodComponentIdSet = new HashSet<>();
    for (ComponentSearchView view : componentSearchViews) {
        goodComponentIdSet.add(view.getComponentId());
    }

    for (String componentId : componentIds) {
        if (goodComponentIdSet.contains(componentId) == false) {
            log.log(Level.FINE, MessageFormat.format("Removing bad index: {0}", componentId));
            deleteById(componentId);
        }
    }
    views.addAll(componentSearchViews);

    List<ComponentSearchView> articleViews = getAttributeService().getArticlesSearchView();
    Map<String, ComponentSearchView> allViews = new HashMap<>();
    for (ComponentSearchView componentSearchView : articleViews) {
        AttributeCodePk attributeCodePk = new AttributeCodePk();
        attributeCodePk.setAttributeType(componentSearchView.getArticleAttributeType());
        attributeCodePk.setAttributeCode(componentSearchView.getArticleAttributeCode());
        allViews.put(attributeCodePk.toKey(), componentSearchView);
    }
    for (SolrComponentModel result : resultsList) {

        if (result.getIsComponent() == false) {
            ComponentSearchView view = allViews.get(result.getId());
            if (view != null) {
                views.add(view);
            } else {
                log.log(Level.FINE, MessageFormat.format("Removing bad index: {0}", result.getId()));
                deleteById(result.getId());
            }
        }
    }

    //TODO: Get the score and sort by score
    return views;
}