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

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

Introduction

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

Prototype

public ModifiableSolrParams set(String name, String... val) 

Source Link

Document

Replace any existing parameter with the given name.

Usage

From source file:org.apache.metron.solr.integration.components.SolrComponent.java

License:Apache License

public List<Map<String, Object>> getAllIndexedDocs(String collection) {
    List<Map<String, Object>> docs = new ArrayList<>();
    CloudSolrClient solr = miniSolrCloudCluster.getSolrClient();
    solr.setDefaultCollection(collection);
    SolrQuery parameters = new SolrQuery();
    parameters.set("q", "*:*");
    try {/*from www  . j  a v a  2  s .  co m*/
        solr.commit();
        QueryResponse response = solr.query(parameters);
        for (SolrDocument solrDocument : response.getResults()) {
            docs.add(solrDocument);
        }
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }
    return docs;
}

From source file:org.apache.nutch.searcher.SolrBean.java

License:Apache License

public Hits search(Query query) throws IOException {
    // filter query string
    //    final BooleanQuery bQuery = filters.filter(query);

    //    final SolrQuery solrQuery = new SolrQuery(stringify(bQuery));
    final SolrQuery solrQuery = new SolrQuery();

    solrQuery.set("q", query.getQuery());
    String a = query.getParams().getSortField();
    solrQuery.setRows(query.getParams().getNumHits());

    if (query.getParams().getSortField() == null) {
        //      solrQuery.setFields(query.getParams().getDedupField(), "score", searchUID);
        query.getParams().setSortField("score");
    } else {//from  www  . j a v  a 2  s . c  o  m
        //      solrQuery.setFields(query.getParams().getDedupField(), query
        //          .getParams().getSortField(), searchUID);
        //      solrQuery.setSortField(query.getParams().getSortField(), query
        //          .getParams().isReverse() ? ORDER.asc : ORDER.desc);

        solrQuery.setSort(query.getParams().getSortField(),
                query.getParams().isReverse() ? ORDER.asc : ORDER.desc);
    }

    solrQuery.set("fl", "id,url,title,tstamp,type,content,segment,score");
    solrQuery.setHighlight(true);
    solrQuery.set("hl.fl", "title,content");
    solrQuery.set("hl.simple.pre", "<span class=highlight>");
    solrQuery.set("hl.simple.post", "</span>");
    solrQuery.set("defType", "edismax");
    solrQuery.set("qf", "title^4 content");

    QueryResponse response;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw makeIOException(e);
    }

    final SolrDocumentList docList = response.getResults();

    Map<String, Map<String, List<String>>> highlights = response.getHighlighting();

    int qtime = response.getQTime();

    final Hit[] hitArr = new Hit[docList.size()];
    for (int i = 0; i < hitArr.length; i++) {
        final SolrDocument solrDoc = docList.get(i);

        String url = (String) solrDoc.getFieldValue("url");
        String title = (String) solrDoc.getFieldValue("title");
        String content = (String) solrDoc.getFieldValue("content");

        final Object raw = solrDoc.getFirstValue(query.getParams().getSortField());
        WritableComparable sortValue;

        if (raw instanceof Integer) {
            sortValue = new IntWritable(((Integer) raw).intValue());
        } else if (raw instanceof Float) {
            sortValue = new FloatWritable(((Float) raw).floatValue());
        } else if (raw instanceof String) {
            sortValue = new Text((String) raw);
        } else if (raw instanceof Long) {
            sortValue = new LongWritable(((Long) raw).longValue());
        } else {
            throw new RuntimeException("Unknown sort value type!");
        }

        final String dedupValue = (String) solrDoc.getFirstValue(query.getParams().getDedupField());

        final String uniqueKey = (String) solrDoc.getFirstValue(searchUID);

        //    hitArr[i] = new Hit(uniqueKey, sortValue, dedupValue);
        SolrHit hit = new SolrHit(uniqueKey, sortValue, dedupValue);
        SolrHitDetails details = buildDetails(solrDoc);
        details.setHit(hit);
        hit.setHitDetails(details);

        hit.setTitleHighlighted(title);
        int len = (content.length() > 100 ? 100 : content.length());
        Summary.Fragment f = new Summary.Fragment(content.substring(0, len));
        Summary summary = new Summary();
        summary.add(f);
        hit.setSummary(summary);

        String titleHighlighted = "";
        if (highlights.containsKey(url)) {
            Map<String, List<String>> snippets = highlights.get(url);
            if (snippets.containsKey("title")) {
                titleHighlighted = snippets.get("title").get(0);
                hit.setTitleHighlighted(titleHighlighted);
            }

            if (snippets.containsKey("content")) {
                f = new Summary.Fragment(snippets.get("content").get(0));
                summary = new Summary();
                summary.add(f);
                hit.setSummary(summary);
            }
        }

        hitArr[i] = hit;
    }

    return new Hits(docList.getNumFound(), hitArr);
}

From source file:org.apache.nutch.searcher.SolrBean.java

License:Apache License

public HitDetails getDetails(Hit hit) throws IOException {
    //    SolrHit h = (SolrHit) hit;
    //    return h.getHitDetails();
    QueryResponse response;//from  w w  w  . j  av a 2 s .  c o  m
    try {
        String url = hit.getUniqueKey();
        url.replace(":", "\\:");
        SolrQuery solrQuery = new SolrQuery(searchUID + ":\"" + url + "\"");
        solrQuery.set("fl", "id,url,title,tstamp,type,content,segment,score");
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw makeIOException(e);
    }

    final SolrDocumentList docList = response.getResults();
    if (docList.getNumFound() == 0) {
        return null;
    }

    return buildDetails(docList.get(0));
}

From source file:org.bigsolr.hadoop.SolrInputFormat.java

License:Apache License

@Override
public List<InputSplit> getSplits(JobContext context) throws IOException, InterruptedException {
    log.info("SolrInputFormat -> getSplits");

    Configuration conf = context.getConfiguration();
    String collectionName = conf.get(COLLECTION_NAME);
    int numSplits = context.getNumReduceTasks();
    SolrServer solr = SolrOperations.getSolrServer(conf);

    final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY));
    solrQuery.setFields(ID_FIELD);/*  ww w .j a v  a 2  s .co m*/
    solrQuery.setRows(50);
    solrQuery.set("collection", collectionName);
    solrQuery.setStart(0);

    QueryResponse response;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw new IOException(e);
    }

    int numResults = (int) response.getResults().getNumFound();
    int numDocsPerSplit = (numResults / numSplits);
    int currentDoc = 0;

    List<InputSplit> splits = new ArrayList<InputSplit>();
    for (int i = 0; i < numSplits - 1; i++) {
        splits.add(new SolrInputSplit(currentDoc, numDocsPerSplit));
        currentDoc += numDocsPerSplit;
    }
    splits.add(new SolrInputSplit(currentDoc, numResults - currentDoc));

    return splits;
}

From source file:org.bigsolr.hadoop.SolrInputFormat.java

License:Apache License

@Override
public org.apache.hadoop.mapred.InputSplit[] getSplits(org.apache.hadoop.mapred.JobConf conf, int numSplits)
        throws IOException {
    log.info("SolrInputFormat -> getSplits");
    String collectionName = conf.get(COLLECTION_NAME);
    SolrServer solr = SolrOperations.getSolrServer(conf);

    final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY));
    solrQuery.setFields(ID_FIELD);// w  w  w  . j  a  va  2 s . c o  m
    solrQuery.setRows(50);
    solrQuery.set("collection", collectionName);
    solrQuery.setStart(0);

    QueryResponse response;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw new IOException(e);
    }

    int numResults = (int) response.getResults().getNumFound();
    int numDocsPerSplit = (numResults / numSplits);
    int currentDoc = 0;

    List<InputSplit> splits = new ArrayList<InputSplit>();
    for (int i = 0; i < numSplits - 1; i++) {
        splits.add(new SolrInputSplit(currentDoc, numDocsPerSplit));
        currentDoc += numDocsPerSplit;
    }
    splits.add(new SolrInputSplit(currentDoc, numResults - currentDoc));

    return splits.toArray(new SolrInputSplit[splits.size()]);
}

From source file:org.bigsolr.hadoop.SolrInputFormat.java

License:Apache License

@Override
public RecordReader<NullWritable, SolrRecord> createRecordReader(InputSplit split, TaskAttemptContext context)
        throws IOException, InterruptedException {

    log.info("SolrInputFormat -> createRecordReader");

    Configuration conf = context.getConfiguration();
    org.apache.hadoop.mapred.Reporter reporter = null; // Need to implement with heartbeat

    String collectionName = conf.get(COLLECTION_NAME);
    String fields = conf.get(FIELDS);
    SolrServer solr = SolrOperations.getSolrServer(conf);

    SolrInputSplit solrSplit = (SolrInputSplit) split;
    final int numDocs = (int) solrSplit.getLength();

    final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY));

    solrQuery.setFields(fields);/*from  ww w .ja  v a  2  s. c  om*/
    solrQuery.set("collection", collectionName);
    solrQuery.setStart(solrSplit.getDocBegin());
    solrQuery.setRows(numDocs);

    QueryResponse response;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw new IOException(e);
    }

    final SolrDocumentList solrDocs = response.getResults();
    return new SolrRecordReader(solrDocs, numDocs);
}

From source file:org.bigsolr.hadoop.SolrInputFormat.java

License:Apache License

@Override
public org.apache.hadoop.mapred.RecordReader<NullWritable, SolrRecord> getRecordReader(
        org.apache.hadoop.mapred.InputSplit split, org.apache.hadoop.mapred.JobConf conf,
        org.apache.hadoop.mapred.Reporter reporter) throws IOException {

    log.info("SolrInputFormat -> getRecordReader");

    String collectionName = conf.get(COLLECTION_NAME);
    String fields = conf.get(FIELDS);
    SolrServer solr = SolrOperations.getSolrServer(conf);
    int numDocs = 0;

    SolrInputSplit solrSplit = (SolrInputSplit) split;
    try {/*  w  w  w.j ava 2 s  .c om*/
        numDocs = (int) solrSplit.getLength();
    } catch (final IOException e) {
        throw new IOException(e);
    }

    final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY));
    solrQuery.setFields(fields);
    solrQuery.set("collection", collectionName); // Added
    solrQuery.setStart(solrSplit.getDocBegin());
    solrQuery.setRows(numDocs);

    QueryResponse response = null;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw new IOException(e);
    }

    final SolrDocumentList solrDocs = response.getResults();
    return new SolrRecordReader(solrDocs, numDocs);
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl.java

License:Apache License

/**
 * Given a qualified solr query string (such as "category:2002"), actually performs a solr search. It will
 * take into considering the search criteria to build out facets / pagination / sorting.
 *
 * @param qualifiedSolrQuery/*  w ww  . j  a  va 2  s.  c om*/
 * @param facets
 * @param searchCriteria
 * @return the ProductSearchResult of the search
 * @throws ServiceException
 */
protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets,
        SearchCriteria searchCriteria, String defaultSort, String... filterQueries) throws ServiceException {
    Map<String, SearchFacetDTO> namedFacetMap = getNamedFacetMap(facets, searchCriteria);

    // Build the basic query
    // Solr queries with a 'start' parameter cannot be a negative number
    int start = (searchCriteria.getPage() <= 0) ? 0 : (searchCriteria.getPage() - 1);
    SolrQuery solrQuery = new SolrQuery().setQuery(qualifiedSolrQuery).setRows(searchCriteria.getPageSize())
            .setStart((start) * searchCriteria.getPageSize());

    //This is for SolrCloud.  We assume that we are always searching against a collection aliased as "PRIMARY"
    solrQuery.setParam("collection", SolrContext.PRIMARY); //This should be ignored if not using SolrCloud

    if (useSku) {
        solrQuery.setFields(shs.getSkuIdFieldName());
    } else {
        solrQuery.setFields(shs.getProductIdFieldName());
    }
    if (filterQueries != null) {
        solrQuery.setFilterQueries(filterQueries);
    }
    solrQuery.addFilterQuery(shs.getNamespaceFieldName() + ":(\"" + shs.getCurrentNamespace() + "\")");
    solrQuery.set("defType", "edismax");
    solrQuery.set("qf", buildQueryFieldsString());

    // Attach additional restrictions
    attachSortClause(solrQuery, searchCriteria, defaultSort);
    attachActiveFacetFilters(solrQuery, namedFacetMap, searchCriteria);
    attachFacets(solrQuery, namedFacetMap);

    modifySolrQuery(solrQuery, qualifiedSolrQuery, facets, searchCriteria, defaultSort);

    extensionManager.getProxy().modifySolrQuery(solrQuery, qualifiedSolrQuery, facets, searchCriteria,
            defaultSort);

    if (LOG.isTraceEnabled()) {
        try {
            LOG.trace(URLDecoder.decode(solrQuery.toString(), "UTF-8"));
        } catch (Exception e) {
            LOG.trace("Couldn't UTF-8 URL Decode: " + solrQuery.toString());
        }
    }

    // Query solr
    QueryResponse response;
    List<SolrDocument> responseDocuments;
    int numResults = 0;
    try {
        response = SolrContext.getServer().query(solrQuery, getSolrQueryMethod());
        responseDocuments = getResponseDocuments(response);
        numResults = (int) response.getResults().getNumFound();

        if (LOG.isTraceEnabled()) {
            LOG.trace(response.toString());

            for (SolrDocument doc : responseDocuments) {
                LOG.trace(doc);
            }
        }
    } catch (SolrServerException e) {
        throw new ServiceException("Could not perform search", e);
    }

    // Get the facets
    setFacetResults(namedFacetMap, response);
    sortFacetResults(namedFacetMap);

    SearchResult result = new SearchResult();
    result.setFacets(facets);
    setPagingAttributes(result, numResults, searchCriteria);

    if (useSku) {
        List<Sku> skus = getSkus(responseDocuments);
        result.setSkus(skus);
    } else {
        // Get the products
        List<Product> products = getProducts(responseDocuments);
        result.setProducts(products);
    }

    return result;
}

From source file:org.codelibs.fess.service.SearchService.java

License:Apache License

public List<Map<String, Object>> getDocumentList(final String query, final int start, final int rows,
        final FacetInfo facetInfo, final GeoInfo geoInfo, final MoreLikeThisInfo mltInfo,
        final String[] responseFields, final String[] docValuesFields, final boolean forUser) {
    if (start > queryHelper.getMaxSearchResultOffset()) {
        throw new ResultOffsetExceededException("The number of result size is exceeded.");
    }// w  ww .  jav  a2  s .  co m

    final long startTime = System.currentTimeMillis();

    final SolrGroup solrGroup = solrGroupManager.getSolrGroup(QueryType.QUERY);

    QueryResponse queryResponse = null;
    final SolrQuery solrQuery = new SolrQuery();
    final SearchQuery searchQuery = queryHelper.build(query, forUser);
    final String q = searchQuery.getQuery();
    if (StringUtil.isNotBlank(q)) {
        // fields
        solrQuery.setFields(responseFields);
        // query
        solrQuery.setQuery(q);
        solrQuery.setStart(start);
        solrQuery.setRows(rows);
        solrQuery.set("mm", searchQuery.getMinimumShouldMatch());
        solrQuery.set("defType", searchQuery.getDefType());
        for (final Map.Entry<String, String[]> entry : queryHelper.getQueryParamMap().entrySet()) {
            solrQuery.set(entry.getKey(), entry.getValue());
        }
        // filter query
        if (searchQuery.hasFilterQueries()) {
            solrQuery.addFilterQuery(searchQuery.getFilterQueries());
        }
        // sort
        final SortField[] sortFields = searchQuery.getSortFields();
        if (sortFields.length != 0) {
            for (final SortField sortField : sortFields) {
                solrQuery.addSort(sortField.getField(),
                        Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc
                                : SolrQuery.ORDER.asc);
            }
        } else if (queryHelper.hasDefaultSortFields()) {
            for (final SortField sortField : queryHelper.getDefaultSortFields()) {
                solrQuery.addSort(sortField.getField(),
                        Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc
                                : SolrQuery.ORDER.asc);
            }
        }
        // highlighting
        if (queryHelper.getHighlightingFields() != null && queryHelper.getHighlightingFields().length != 0) {
            for (final String hf : queryHelper.getHighlightingFields()) {
                solrQuery.addHighlightField(hf);
            }
            solrQuery.setHighlightSnippets(queryHelper.getHighlightSnippetSize());
        }
        // shards
        if (queryHelper.getShards() != null) {
            solrQuery.setParam("shards", queryHelper.getShards());
        }
        // geo
        if (geoInfo != null && geoInfo.isAvailable()) {
            solrQuery.addFilterQuery(geoInfo.toGeoQueryString());
            final String additionalGeoQuery = queryHelper.getAdditionalGeoQuery();
            if (StringUtil.isNotBlank(additionalGeoQuery)) {
                solrQuery.addFilterQuery(additionalGeoQuery);
            }
        }
        // facets
        if (facetInfo != null) {
            solrQuery.setFacet(true);
            if (facetInfo.field != null) {
                for (final String f : facetInfo.field) {
                    if (queryHelper.isFacetField(f)) {
                        solrQuery.addFacetField(f);
                    } else {
                        throw new FessSolrQueryException("EFESS0002", new Object[] { f });
                    }
                }
            }
            if (facetInfo.query != null) {
                for (final String fq : facetInfo.query) {
                    final String facetQuery = queryHelper.buildFacetQuery(fq);
                    if (StringUtil.isNotBlank(facetQuery)) {
                        solrQuery.addFacetQuery(facetQuery);
                    } else {
                        throw new FessSolrQueryException("EFESS0003", new Object[] { fq, facetQuery });
                    }
                }
            }
            if (facetInfo.limit != null) {
                solrQuery.setFacetLimit(Integer.parseInt(facetInfo.limit));
            }
            if (facetInfo.minCount != null) {
                solrQuery.setFacetMinCount(Integer.parseInt(facetInfo.minCount));
            }
            if (facetInfo.missing != null) {
                solrQuery.setFacetMissing(Boolean.parseBoolean(facetInfo.missing));
            }
            if (facetInfo.prefix != null) {
                solrQuery.setFacetPrefix(facetInfo.prefix);
            }
            if (facetInfo.sort != null && queryHelper.isFacetSortValue(facetInfo.sort)) {
                solrQuery.setFacetSort(facetInfo.sort);
            }
        }
        // mlt
        if (mltInfo != null) {
            final String mltField = queryHelper.getMoreLikeThisField(mltInfo.field);
            if (mltField != null) {
                solrQuery.set("mlt", true);
                if (mltInfo.count != null) {
                    solrQuery.set("mlt.count", Integer.parseInt(mltInfo.count));
                }
                solrQuery.set("mlt.fl", mltField);
            }
        }

        if (queryHelper.getTimeAllowed() >= 0) {
            solrQuery.setTimeAllowed(queryHelper.getTimeAllowed());
        }
        final Set<Entry<String, String[]>> paramSet = queryHelper.getRequestParameterSet();
        if (!paramSet.isEmpty()) {
            for (final Map.Entry<String, String[]> entry : paramSet) {
                solrQuery.set(entry.getKey(), entry.getValue());
            }
        }

        if (docValuesFields != null) {
            for (final String docValuesField : docValuesFields) {
                solrQuery.add(Constants.DCF, docValuesField);
            }
        }

        queryResponse = solrGroup.query(solrQuery, SolrRequest.METHOD.POST);
    }
    final long execTime = System.currentTimeMillis() - startTime;

    final QueryResponseList queryResponseList = ComponentUtil.getQueryResponseList();
    queryResponseList.init(queryResponse, rows);
    queryResponseList.setSearchQuery(q);
    queryResponseList.setSolrQuery(solrQuery.toString());
    queryResponseList.setExecTime(execTime);
    return queryResponseList;
}

From source file:org.dspace.app.webui.cris.components.statistics.ASolrStatsConfigurerComponent.java

protected void _prepareBasicQuery(SolrQuery solrQuery, Integer yearsQuery) {
    _addBasicConfiguration(solrQuery, yearsQuery);
    solrQuery.addFacetField(_CONTINENT, _COUNTRY_CODE, _CITY, ID, _LOCATION, _FISCALYEAR, _SOLARYEAR);
    solrQuery.setFacetMissing(true);/*ww  w .j  ava2 s. c om*/
    solrQuery.set("f." + _LOCATION + ".facet.missing", false);
    solrQuery.set("f." + ID + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.missing", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.sort", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.sort", false);

    solrQuery.set("f." + _CONTINENT + ".facet.mincount", 1);
    solrQuery.set("f." + _COUNTRY_CODE + ".facet.mincount", 1);
    solrQuery.set("f." + _CITY + ".facet.mincount", 1);
    solrQuery.set("f." + ID + ".facet.mincount", 1);
    solrQuery.set("f." + _LOCATION + ".facet.mincount", 1);
    solrQuery.set("f." + _FISCALYEAR + ".facet.mincount", 1);
    solrQuery.set("f." + _SOLARYEAR + ".facet.mincount", 1);
}