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:com.buddycloud.channeldirectory.search.handler.nearby.NearbyQueryHandler.java

License:Apache License

private List<ChannelData> findNearbyObjects(double lat, double lng, RSM rsm)
        throws MalformedURLException, SolrServerException {
    SolrServer solrServer = SolrServerFactory.createChannelCore(getProperties());
    SolrQuery solrQuery = new SolrQuery("*:*");
    solrQuery.set("fq", "{!geofilt}");
    solrQuery.set("sfield", "geoloc");
    solrQuery.set("pt", lat + "," + lng);
    solrQuery.set("d", RADIUS_IN_KM);

    solrQuery.addSortField("geodist()", ORDER.asc);

    SolrRSMUtils.preprocess(solrQuery, rsm);
    QueryResponse queryResponse = solrServer.query(solrQuery);
    SolrRSMUtils.postprocess(queryResponse, rsm);

    return convertResponse(queryResponse);
}

From source file:com.digitalpebble.storm.crawler.solr.persistence.SolrSpout.java

License:Apache License

private void populateBuffer() {
    // TODO Sames as the ElasticSearchSpout?
    // TODO Use the cursor feature?
    // https://cwiki.apache.org/confluence/display/solr/Pagination+of+Results
    SolrQuery query = new SolrQuery();

    query.setQuery("*:*").addFilterQuery("nextFetchDate:[* TO NOW]").setStart(lastStartOffset)
            .setRows(this.bufferSize);

    if (StringUtils.isNotBlank(diversityField)) {
        query.addFilterQuery(String.format("{!collapse field=%s}", diversityField));
        query.set("expand", "true").set("expand.rows", diversityBucketSize);
    }/*from  w ww .j av  a2 s .c  o  m*/

    try {
        QueryResponse response = connection.getClient().query(query);
        SolrDocumentList docs = new SolrDocumentList();

        if (StringUtils.isNotBlank(diversityField)) {
            // Add the main documents collapsed by the CollapsingQParser
            // plugin
            docs.addAll(response.getResults());

            Map<String, SolrDocumentList> expandedResults = response.getExpandedResults();

            for (String key : expandedResults.keySet()) {
                docs.addAll(expandedResults.get(key));
            }

        } else {
            docs = response.getResults();
        }

        int numhits = response.getResults().size();

        // no more results?
        if (numhits == 0)
            lastStartOffset = 0;
        else
            lastStartOffset += numhits;

        for (SolrDocument doc : docs) {
            String url = (String) doc.get("url");

            // is already being processed - skip it!
            if (beingProcessed.containsKey(url))
                continue;

            Metadata metadata = new Metadata();

            String mdAsString = (String) doc.get("metadata");
            // get the serialized metadata information
            if (mdAsString != null) {
                // parse the string and generate the MD accordingly
                // url.path: http://www.lemonde.fr/
                // depth: 1
                String[] kvs = mdAsString.split("\n");
                for (String pair : kvs) {
                    String[] kv = pair.split(": ");
                    if (kv.length != 2) {
                        LOG.info("Invalid key value pair {}", pair);
                        continue;
                    }
                    metadata.addValue(kv[0], kv[1]);
                }
            }

            buffer.add(new Values(url, metadata));
        }

    } catch (Exception e) {
        LOG.error("Can't query Solr: {}", e);
    }
}

From source file:com.digitalpebble.stormcrawler.solr.persistence.SolrSpout.java

License:Apache License

private void populateBuffer() {
    // TODO Sames as the ElasticSearchSpout?
    // TODO Use the cursor feature?
    // https://cwiki.apache.org/confluence/display/solr/Pagination+of+Results
    SolrQuery query = new SolrQuery();

    query.setQuery("*:*").addFilterQuery("nextFetchDate:[* TO NOW]").setStart(lastStartOffset)
            .setRows(this.bufferSize);

    if (StringUtils.isNotBlank(diversityField)) {
        query.addFilterQuery(String.format("{!collapse field=%s}", diversityField));
        query.set("expand", "true").set("expand.rows", diversityBucketSize);
    }//from  w w  w  .ja  v  a 2s.  c  o m

    try {
        QueryResponse response = connection.getClient().query(query);
        SolrDocumentList docs = new SolrDocumentList();

        if (StringUtils.isNotBlank(diversityField)) {
            // Add the main documents collapsed by the CollapsingQParser
            // plugin
            docs.addAll(response.getResults());

            Map<String, SolrDocumentList> expandedResults = response.getExpandedResults();

            for (String key : expandedResults.keySet()) {
                docs.addAll(expandedResults.get(key));
            }

        } else {
            docs = response.getResults();
        }

        int numhits = response.getResults().size();

        // no more results?
        if (numhits == 0)
            lastStartOffset = 0;
        else
            lastStartOffset += numhits;

        String prefix = mdPrefix.concat(".");

        for (SolrDocument doc : docs) {
            String url = (String) doc.get("url");

            // is already being processed - skip it!
            if (beingProcessed.containsKey(url))
                continue;

            Metadata metadata = new Metadata();

            Iterator<String> keyIterators = doc.getFieldNames().iterator();
            while (keyIterators.hasNext()) {
                String key = keyIterators.next();

                if (key.startsWith(prefix)) {
                    Collection<Object> values = doc.getFieldValues(key);

                    key = StringUtils.replace(key, prefix, "", 1);
                    Iterator<Object> valueIterator = values.iterator();
                    while (valueIterator.hasNext()) {
                        String value = (String) valueIterator.next();

                        metadata.addValue(key, value);
                    }
                }
            }

            buffer.add(new Values(url, metadata));
        }

    } catch (Exception e) {
        LOG.error("Can't query Solr: {}", e);
    }
}

From source file:com.doculibre.constellio.services.FacetServicesImpl.java

License:Open Source License

public static SolrQuery toSolrQuery(SimpleSearch simpleSearch, int start, int row,
        boolean includeSingleValueFacets, boolean notIncludedOnly, List<String> customFieldFacets,
        List<String> customQueryFacets, ConstellioUser user) {
    String solrServerName = simpleSearch.getCollectionName();
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    RecordCollection collection = collectionServices.get(solrServerName);
    SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
    Boolean usesDisMax = solrServices.usesDisMax(collection);
    SolrQuery query;
    if (!collection.isOpenSearch()) {
        query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, true, includeSingleValueFacets,
                notIncludedOnly);//  w ww  .  ja v a2 s  .c o m
    } else {
        query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, false, true, false);
    }

    query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName());
    query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY,
            simpleSearch.getLuceneQuery(includeSingleValueFacets, true));
    query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
    if (user != null) {
        query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
    }

    if (StringUtils.isEmpty(query.getQuery())) {
        query.setQuery(SimpleSearch.SEARCH_ALL);
        query.setRequestHandler("/elevate");
    }

    query.set("shards.qt", "/elevate");
    query.setRequestHandler("/elevate");

    query.setRows(row);
    query.setStart(start);
    query.setHighlight(true);
    query.setHighlightFragsize(100);
    query.setHighlightSnippets(2);

    query.setFacet(true);
    query.setFacetLimit(400);
    query.setFacetMinCount(1);

    if (collection.isOpenSearch()) {
        query.setParam("openSearchURL", collection.getOpenSearchURL());
        Locale locale = simpleSearch.getSingleSearchLocale();
        if (locale != null) {
            query.setParam("lang", locale.getLanguage());
        }
    } else {
        for (CollectionFacet collectionFacet : collection.getCollectionFacets()) {
            if (customFieldFacets == null && collectionFacet.isFieldFacet()) {
                IndexField indexField = collectionFacet.getFacetField();
                String indexFieldName = indexField.getName();
                if (!notIncludedOnly) {
                    query.addFacetField(indexFieldName);
                } else {
                    SearchedFacet searchedFacet = simpleSearch.getSearchedFacet(indexFieldName);
                    if (searchedFacet != null) {
                        if (!searchedFacet.getIncludedValues().isEmpty()) {
                            StringBuffer sbEx = new StringBuffer();
                            sbEx.append("{!ex=dt}");
                            //                                sbEx.append("{!ex=");
                            //                                boolean first = true;
                            //                                for (String includedValue : searchedFacet.getIncludedValues()) {
                            //                                    if (first) {
                            //                                        first = false;
                            //                                    } else {
                            //                                        sbEx.append(",");
                            //                                    }
                            //                                    sbEx.append(includedValue); 
                            //                                }
                            //                                sbEx.append("}");
                            //                                query.setParam("facet.field", sbEx.toString() + indexFieldName);
                            query.addFacetField(sbEx.toString() + indexFieldName);
                        } else {
                            query.addFacetField(indexFieldName);
                        }
                    }
                }
            } else if (customQueryFacets == null && collectionFacet.isQueryFacet()) {
                // Modification Rida, remplacement de collectionFacet.getLabels() par
                // collectionFacet.getLabelledValues()
                // for (I18NLabel valueLabels : collectionFacet.getLabels()) {
                for (I18NLabel valueLabels : collectionFacet.getLabelledValues()) {
                    String facetQuery = valueLabels.getKey();
                    query.addFacetQuery(facetQuery);
                }
            }
        }
        if (customFieldFacets != null) {
            for (String facetField : customFieldFacets) {
                if (!notIncludedOnly) {
                    query.addFacetField(facetField);
                } else {
                    StringBuffer sbEx = new StringBuffer();
                    sbEx.append("{!ex=dt}");
                    //                        sbEx.append("{!ex=");
                    //                        boolean first = true;
                    //                        for (String includedValue : searchedFacet.getIncludedValues()) {
                    //                            if (first) {
                    //                                first = false;
                    //                            } else {
                    //                                sbEx.append(",");
                    //                            }
                    //                            sbEx.append(includedValue); 
                    //                        }
                    //                        sbEx.append("}");
                    query.setParam("facet.field", sbEx.toString() + facetField);
                }
            }
        }

        if (customQueryFacets != null) {
            for (String facetQuery : customQueryFacets) {
                query.addFacetQuery(facetQuery);
            }
        }
    }

    return query;
}

From source file:com.doculibre.constellio.services.SearchServicesImpl.java

License:Open Source License

@Override
public QueryResponse search(SimpleSearch simpleSearch, int start, int rows, SearchParams searchParams,
        ConstellioUser user) {//from w ww.  jav a 2 s . co m
    QueryResponse queryResponse;

    String collectionName = simpleSearch.getCollectionName();
    if (collectionName != null) {
        RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
        RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
        RecordCollection collection = collectionServices.get(collectionName);

        SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
        Boolean usesDisMax = solrServices.usesDisMax(collection);

        SolrQuery query;
        if (!collection.isOpenSearch()) {
            query = toSolrQuery(simpleSearch, usesDisMax, true, true);
        } else {
            query = toSolrQuery(simpleSearch, usesDisMax, false, true);
        }
        // displayQuery(query);

        String luceneQuery = simpleSearch.getLuceneQuery();
        query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, luceneQuery);
        query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
        query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, collectionName);
        if (user != null) {
            query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
        }

        String queryString = query.getQuery();
        if (StringUtils.isEmpty(queryString)) {
            queryString = SimpleSearch.SEARCH_ALL;
        }

        List<Record> pendingExclusions = recordServices.getPendingExclusions(collection);
        while (!pendingExclusions.isEmpty()) {
            IndexingManager indexingManager = IndexingManager.get(collection);
            if (indexingManager.isActive()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                pendingExclusions = recordServices.getPendingExclusions(collection);
            } else {
                return null;
            }
        }

        // SolrQuery query = new SolrQuery();
        query.set("collectionName", simpleSearch.getCollectionName());
        // query.setQuery(luceneQuery);

        query.set("shards.qt", "/elevate");
        query.setRequestHandler("/elevate");

        // nb rsultats par page
        query.setRows(rows);

        // page de dbut
        query.setStart(start);
        query.setHighlight(searchParams.isHighlightingEnabled());
        if (searchParams.isHighlightingEnabled()) {
            query.setHighlightFragsize(searchParams.getFragsize());
            query.setHighlightSnippets(searchParams.getSnippets());
        }

        if (simpleSearch.getSortField() != null) {
            ORDER order = SimpleSearch.SORT_DESCENDING.equals(simpleSearch.getSortOrder()) ? ORDER.desc
                    : ORDER.asc;
            IndexFieldServices indexFieldServices = ConstellioSpringUtils.getIndexFieldServices();
            IndexField indexField = indexFieldServices.get(simpleSearch.getSortField(), collection);
            if (indexField != null) {
                IndexField sortIndexField = indexFieldServices.getSortFieldOf(indexField);
                if (sortIndexField != null) {
                    query.setSort(sortIndexField.getName(), order);
                }
            }
        }

        if (collection.isOpenSearch()) {
            query.setParam("openSearchURL", collection.getOpenSearchURL());
            Locale locale = simpleSearch.getSingleSearchLocale();
            if (locale != null) {
                query.setParam("lang", locale.getLanguage());
            }
        }

        if (searchParams.getHighlightedFields() == null) {
            IndexField defaultSearchField = collection.getDefaultSearchIndexField();
            query.addHighlightField(defaultSearchField.getName());
            for (CopyField copyFieldDest : defaultSearchField.getCopyFieldsDest()) {
                IndexField copyIndexFieldSource = copyFieldDest.getIndexFieldSource();
                if (copyIndexFieldSource != null && !copyIndexFieldSource.isTitleField()
                        && copyIndexFieldSource.isHighlighted()) {
                    query.addHighlightField(copyIndexFieldSource.getName());
                }
            }
            IndexField titleField = collection.getTitleIndexField();
            if (titleField != null && titleField.isHighlighted()) {
                query.addHighlightField(titleField.getName());
            }
        } else {
            for (String highlightedField : searchParams.getHighlightedFields()) {
                IndexField field = collection.getIndexField(highlightedField);
                if (field != null) {
                    query.addHighlightField(highlightedField);
                }
            }
        }
        SolrServer server = SolrCoreContext.getSolrServer(collectionName);
        if (server != null) {
            try {
                // displayQuery(query);
                queryResponse = server.query(query);
            } catch (SolrServerException e) {
                queryResponse = null;
            }
        } else {
            queryResponse = null;
        }

        //            if (queryResponse != null && !collection.isOpenSearch()) {
        //                StatsCompiler statCompiler = StatsCompiler.getInstance();
        //                try {
        //                    statCompiler.saveStats(collectionName, SolrLogContext.getStatsSolrServer(),
        //                        SolrLogContext.getStatsCompileSolrServer(), queryResponse, luceneQuery);
        //                } catch (SolrServerException e) {
        //                    throw new RuntimeException(e);
        //                } catch (IOException e) {
        //                    throw new RuntimeException(e);
        //                }
        //            }

    } else {
        queryResponse = null;
    }

    // improveQueryResponse(collectionName, queryResponse);
    // System.out.println("Response size" + queryResponse.getResults().getNumFound());
    return queryResponse;
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Do the query using a SolrQuery/*from   www.j a v a 2s  .  c  o  m*/
 */
public static QueryResponse doThirdQuery(SolrServer server) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(query);
    solrQuery.set("collectionName", myCollection);
    solrQuery.set("facet", facet);
    solrQuery.setStart(start);
    solrQuery.setRows(nbDocuments);
    return server.query(solrQuery);
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Do the query using a SolrQuery/*w  w w. j  av  a2  s .c om*/
 */
public static QueryResponse spellCheck(SolrServer server, String badQuery) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(badQuery);
    solrQuery.set("collectionName", myCollection);

    // qt=spellcheck || qt=spellchecker
    solrQuery.setRequestHandler("spellcheck");
    return server.query(solrQuery);
}

From source file:com.eharmony.matching.seeking.executor.solr.SolrQueryExecutor.java

License:Apache License

protected <T, R> SolrQuery translate(Query<T, R> query) {
    SolrQuery solrQuery = new SolrQuery();
    // special geospatial query case
    SolrSpatialQuery spatialQuery = queryTranslator.getSpatialQuery(query);
    String translated = queryTranslator.translate(query);
    solrQuery.setQuery(translated == null || translated.isEmpty() ? "*:*" : translated);
    if (spatialQuery != null) {
        //solrQuery.addFilterQuery("{!func}geodist()"); 
        solrQuery.addFilterQuery("{!geofilt}");
        solrQuery.set("sfield", spatialQuery.getField());
        solrQuery.set("pt", spatialQuery.getX() + "," + spatialQuery.getY());
        solrQuery.set("d", spatialQuery.getDistance().toString());
    }/*from  w w  w  .  ja va  2 s. c om*/
    for (SolrOrdering ordering : queryTranslator.translateOrder(query).get()) {
        String field = ordering.getField();
        if (spatialQuery != null && field.equals(spatialQuery.getField())) {
            field = "geodist()";
        }
        solrQuery.addSortField(field, ordering.getOrder());
    }
    for (String field : queryTranslator.translateProjection(query)) {
        solrQuery.addField(field);
    }
    if (query.getMaxResults() != null) {
        solrQuery.setRows(query.getMaxResults());
    }
    return solrQuery;
}

From source file:com.frank.search.service.impl.CustomSolrRepositoryImpl.java

License:Apache License

public PageProductResponse findProductsByCustom(Product product) {
    SolrQuery sq = new SolrQuery();

    sq.set("q", "goods_name:");
    sq.set("fl", "stores_name", "id", "goods_name");

    QueryResponse queryResponse = null;// solrTemplate.querySolrByCustomDefine(sq);
    List<Product> productList = queryResponse.getBeans(Product.class);
    PageProductResponse pageModel = new PageProductResponse();
    pageModel.setTotalPages(queryResponse.getResults().getNumFound());
    return pageModel;
}

From source file:com.frank.search.solr.core.DefaultQueryParser.java

License:Apache License

private void processStatsOptions(SolrQuery solrQuery, Query query) {
    StatsOptions statsOptions = query.getStatsOptions();

    if (statsOptions == null || (CollectionUtils.isEmpty(statsOptions.getFields())
            && CollectionUtils.isEmpty(statsOptions.getFacets())
            && CollectionUtils.isEmpty(statsOptions.getSelectiveFacets()))) {
        return;//from  www  .  jav a 2 s .  c  o  m
    }

    solrQuery.set(StatsParams.STATS, true);

    for (Field field : statsOptions.getFields()) {
        solrQuery.add(StatsParams.STATS_FIELD, field.getName());

        String selectiveCalcDistinctParam = CommonParams.FIELD + "." + field.getName() + "."
                + StatsParams.STATS_CALC_DISTINCT;
        Boolean selectiveCountDistincts = statsOptions.isSelectiveCalcDistincts(field);

        if (selectiveCountDistincts != null) {
            solrQuery.add(selectiveCalcDistinctParam, String.valueOf(selectiveCountDistincts.booleanValue()));
        }

    }

    for (Field field : statsOptions.getFacets()) {
        solrQuery.add(StatsParams.STATS_FACET, field.getName());
    }

    for (Entry<Field, Collection<Field>> entry : statsOptions.getSelectiveFacets().entrySet()) {

        Field field = entry.getKey();
        String prefix = CommonParams.FIELD + "." + field.getName() + ".";

        String paramName = prefix + StatsParams.STATS_FACET;
        for (Field facetField : entry.getValue()) {
            solrQuery.add(paramName, facetField.getName());
        }

    }

}