Example usage for org.apache.solr.common.params SolrParams toString

List of usage examples for org.apache.solr.common.params SolrParams toString

Introduction

In this page you can find the example usage for org.apache.solr.common.params SolrParams toString.

Prototype

@Override
public String toString() 

Source Link

Document

Like #toQueryString() , but only replacing enough chars so that the URL may be unambiguously pasted back into a browser.

Usage

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

private QueryResponse query(SolrParams query, SolrRequest.METHOD queryMethod) throws SolrServerException {
    int retry = 0;

    QueryResponse qr = null;/* w w  w.j a va  2  s.  c  o m*/
    while (retry < maxRetries && qr == null) {
        retry++;
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("SOLR query:" + query.toString());
            }

            qr = getServer().query(query, queryMethod == null ? this.queryMethod : queryMethod); // can throw exception
        } catch (SolrServerException e) {
            //want to retry IOException and Proxy Error
            if (retry < maxRetries
                    && (e.getMessage().contains("IOException") || e.getMessage().contains("Proxy Error"))) {
                if (retryWait > 0) {
                    try {
                        Thread.sleep(retryWait);
                    } catch (InterruptedException ex) {
                        // If the Thread sleep is interrupted, we shouldn't attempt to continue
                        Thread.currentThread().interrupt();
                        throw e;
                    }
                }
            } else {
                //throw all other errors
                throw e;
            }
        } catch (HttpSolrServer.RemoteSolrException e) {
            //report failed query
            logger.error("query failed: " + query.toString() + " : " + e.getMessage());
            throw e;
        }
    }

    return qr;
}

From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    // Date startTime =new Date();

    String event = params.get("event");
    String ids = params.get("ids");
    String shardUrl = params.get("shard.url");

    // filter the query warming clause and di request
    if (!((event != null && event.equals("firstSearcher")) || (ids != null))) {
        String[] shardUrlStrs = shardUrl.split("\\\\|")[0].split("/");
        String collectionName = shardUrlStrs[shardUrlStrs.length - 1];
        String queryText = rb.getQueryString();
        String queryTextAnalyzed = queryText;
        // String queryTextAnalyzed =
        // AnalyzerUtils.analyze(queryText,collection);
        int searchPage = params.getInt("page", 0);
        String simpleSearchStr = getSimpleSearchStr(params);
        String simpleSearchId = getSimpleSearchId(simpleSearchStr);
        String searchLogDocId = generateSearchLogDocId(simpleSearchId);

        String simpleSearchQueryAnalyzedStr = params.toString();
        // String simpleSearchQueryAnalyzedStr
        // =getSimpleSearchStr(simpleSearch, true);
        long timeCost = rb.rsp.getEndTime() - rb.req.getStartTime();

        SolrInputDocument doc = new SolrInputDocument();
        doc.setField(StatsConstants.INDEX_FIELD_ID, searchLogDocId);
        doc.setField(StatsConstants.INDEX_FIELD_COLLECTION_NAME, collectionName);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_ID, simpleSearchId);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH, simpleSearchStr);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_QUERY_ANALYZED, simpleSearchQueryAnalyzedStr);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT, queryText);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT_ANALYZED, queryTextAnalyzed);
        doc.setField(StatsConstants.INDEX_FIELD_NUM_FOUND, rb.getNumberDocumentsFound());
        doc.setField(StatsConstants.INDEX_FIELD_RESPONSE_TIME, timeCost);
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_DATE, new Date());
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_PAGE, searchPage);

        try {/*from  w  ww .ja v  a  2s  . c  o m*/
            //            searchLogCache.add(doc);
            //            if (searchLogCache.size() >= commitThreshold) {
            int port = Integer.parseInt(shardUrl.substring(shardUrl.indexOf(":") + 1, shardUrl.indexOf("/")));
            if (searchLogServer == null || localPort != port) {
                localPort = port;
                searchLogServer = new HttpSolrServer(
                        "http://localhost:" + localPort + "/solr/" + searchLogCoreName);
            }
            //               searchLogServer.add(searchLogCache);
            searchLogServer.add(doc);
            searchLogServer.commit();
            //               searchLogCache.clear();
            //            }
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // System.out.println("premier phase:"+timeCost);
    }
    // Date endTime =new Date();
    // System.out.println("total time:"+(endTime.getTime()-startTime.getTime()));
}

From source file:fr.cnes.sitools.metacatalogue.resources.mdweb.MdWebSearchResource.java

License:Open Source License

/**
 * Actions on PUT//from   ww w .  j  a va  2 s  .com
 * 
 * @param variant
 *          MediaType of response
 * @return Representation response
 */
@Get
public Representation get(Variant variant) {
    NamedList<String> list = new NamedList<String>();

    Form query = getRequest().getResourceRef().getQueryAsForm();
    for (Parameter parameter : query) {
        list.add(parameter.getName(), parameter.getValue());
    }

    SolrServer server = getSolrServer(getContext());

    ThesaurusSearcher searcher = null;

    try {
        searcher = getThesaurusSearcher();
    } catch (IOException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Cannot read thesaurus", e);
    }
    //
    // repr = opensearchQuery(repr, query, server, searcher);

    SolrParams solrQuery = SolrQuery.toSolrParams(list);

    try {
        getLogger().info("Query : " + solrQuery.toString());
        QueryResponse rsp = server.query(solrQuery);

        boolean isAuthenticated = getClientInfo().isAuthenticated();
        SitoolsSettings settings = getSettings();
        String applicationBaseUrl = settings.getPublicHostDomain() + application.getAttachementRef();

        //Reference ref = new Reference(getRequest().getResourceRef().getBaseRef());
        Reference ref = new Reference(applicationBaseUrl + "/" + getReference().getLastSegment());

        ref.setQuery(query.getQueryString());

        return new GeoJsonMDEORepresentation(rsp, isAuthenticated, applicationBaseUrl, ref, 0, "start", "rows",
                searcher.getAllConceptsAsMap(getLanguage()), thesaurusFacetFields);
    } catch (SolrServerException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Error while querying solr index", e);
    }
}

From source file:net.yacy.cora.federate.solr.instance.ServerShard.java

License:Open Source License

/**
 * Performs a query to the Solr server//  w ww. j  a  v a 2  s  . c o  m
 * @param params  an object holding all key/value parameters to send along the request
 * @throws IOException 
 */
@Override
public QueryResponse query(final SolrParams params) throws SolrServerException, IOException {
    List<SolrClient> qs = this.shards.server4read();
    if (qs.size() == 1) {
        return qs.get(0).query(params);
    }

    // concurrently call all shards
    final Collection<QueryResponse> qrl = new ConcurrentLinkedQueue<QueryResponse>();
    List<Thread> t = new ArrayList<Thread>();
    for (final SolrClient s : qs) {
        Thread t0 = new Thread() {
            @Override
            public void run() {
                this.setName("ServerShard.query/1(" + params.toString() + ")");
                QueryResponse rsp;
                try {
                    rsp = s.query(params);
                } catch (final Throwable e) {
                    return;
                }
                qrl.add(rsp);
            }
        };
        t0.start();
        t.add(t0);
    }
    for (Thread t0 : t) {
        try {
            t0.join();
        } catch (final InterruptedException e) {
        }
    }

    // prepare combined response
    return ResponseAccumulator.combineResponses(qrl);
}

From source file:net.yacy.cora.federate.solr.instance.ServerShard.java

License:Open Source License

/**
 * Performs a query to the Solr server/*from  w w w .  j  av  a2 s  .c  o  m*/
 * @param params  an object holding all key/value parameters to send along the request
 * @param method  specifies the HTTP method to use for the request, such as GET or POST
 * @throws IOException 
 */
@Override
public QueryResponse query(final SolrParams params, final METHOD method)
        throws SolrServerException, IOException {
    List<SolrClient> qs = this.shards.server4read();
    if (qs.size() == 1) {
        return qs.get(0).query(params, method);
    }

    final Collection<QueryResponse> qrl = new ConcurrentLinkedQueue<QueryResponse>();
    // concurrently call all shards
    List<Thread> t = new ArrayList<Thread>();
    for (final SolrClient s : qs) {
        Thread t0 = new Thread() {
            @Override
            public void run() {
                this.setName("ServerShard.query/2(" + params.toString() + ")");
                QueryResponse rsp;
                try {
                    rsp = s.query(params, method);
                } catch (final Throwable e) {
                    return;
                }
                qrl.add(rsp);
            }
        };
        t0.start();
        t.add(t0);
    }
    for (Thread t0 : t) {
        try {
            t0.join();
        } catch (final InterruptedException e) {
        }
    }

    // prepare combined response
    return ResponseAccumulator.combineResponses(qrl);
}

From source file:org.codelibs.solr.lib.SolrGroup.java

License:Apache License

/**
 * Query solr, and stream the results.  Unlike the standard query, this will
 * send events for each Document rather then add them to the QueryResponse.
 *
 * Although this function returns a 'QueryResponse' it should be used with care
 * since it excludes anything that was passed to callback.  Also note that
 * future version may pass even more info to the callback and may not return
 * the results in the QueryResponse.//from  ww  w. j  a  v  a 2s .co m
 *
 */
public QueryResponse queryAndStreamResponse(final SolrParams params, final StreamingResponseCallback callback) {
    // check this group status
    checkStatus(QueryType.QUERY);

    SolrLibServiceException fsqe = null;
    final int maxRetryCount = statusPolicy.getMaxRetryCount(QueryType.QUERY);
    for (int i = 0; i < maxRetryCount; i++) {
        try {
            return queryInternal(QueryType.QUERY, params, new UpdateProcessCallback<QueryResponse>() {
                @Override
                public QueryResponse callback(final SolrServer solrServer)
                        throws SolrServerException, IOException {
                    return solrServer.queryAndStreamResponse(params, callback);
                }
            });
        } catch (final SolrLibQueryException | SolrLibServerNotAvailableException e) {
            throw e;
        } catch (final Exception e) {
            if (fsqe == null) {
                fsqe = new SolrLibServiceException("ESL0011", new Object[] { params.toString() });
            }
            fsqe.addException(e);
        }
        statusPolicy.sleep(QueryType.QUERY);
    }

    throw fsqe;
}

From source file:org.codelibs.solr.lib.SolrGroup.java

License:Apache License

/**
 * Performs a query to the Solr server/*from   w  w w. java  2s .  co  m*/
 * @param params  an object holding all key/value parameters to send along the request
 * @param method  specifies the HTTP method to use for the request, such as GET or POST
 */
public QueryResponse query(final SolrParams params, final METHOD method) {
    // check this group status
    checkStatus(QueryType.QUERY);

    SolrLibServiceException fsqe = null;
    final int maxRetryCount = statusPolicy.getMaxRetryCount(QueryType.QUERY);
    for (int i = 0; i < maxRetryCount; i++) {
        try {
            return queryInternal(QueryType.QUERY, params, new UpdateProcessCallback<QueryResponse>() {
                @Override
                public QueryResponse callback(final SolrServer solrServer)
                        throws SolrServerException, IOException {
                    return solrServer.query(params, method);
                }
            });
        } catch (final SolrLibQueryException | SolrLibServerNotAvailableException e) {
            throw e;
        } catch (final Exception e) {
            if (fsqe == null) {
                fsqe = new SolrLibServiceException("ESL0011", new Object[] { params.toString() });
            }
            fsqe.addException(e);
        }
        statusPolicy.sleep(QueryType.QUERY);
    }

    throw fsqe;
}