Example usage for org.apache.solr.common.params CommonParams Q

List of usage examples for org.apache.solr.common.params CommonParams Q

Introduction

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

Prototype

String Q

To view the source code for org.apache.solr.common.params CommonParams Q.

Click Source Link

Document

query string

Usage

From source file:edu.toronto.cs.phenotips.solr.AbstractSolrScriptService.java

License:Open Source License

/**
 * Convert a Lucene query string into a map of Solr parameters. More specifically, places the input query under the
 * "q" parameter, and adds parameters for requesting a spellcheck result.
 * /*from   w  w w  . j  a  v a2 s.c  om*/
 * @param query the lucene query string to use
 * @param sort the sort criteria ("fiel_name order')
 * @param rows the number of items to return, or -1 to use the default number of results
 * @param start the number of items to skip, i.e. the index of the first hit to return, 0-based
 * @return a map of Solr query parameter ready to be used for constructing a {@link MapSolrParams} object
 */
private Map<String, String> getSolrQuery(String query, String sort, int rows, int start) {
    Map<String, String> result = new HashMap<String, String>();
    result.put(CommonParams.START, start + "");
    if (rows > 0) {
        result.put(CommonParams.ROWS, rows + "");
    }
    result.put(CommonParams.Q, query);
    if (StringUtils.isNotBlank(sort)) {
        result.put(CommonParams.SORT, sort);
    }
    result.put("spellcheck", Boolean.toString(true));
    result.put("spellcheck.collate", Boolean.toString(true));
    return result;
}

From source file:edu.toronto.cs.phenotips.solr.OmimScriptService.java

License:Open Source License

/**
 * Prepare the map of parameters that can be passed to a Solr query, in order to get a list of diseases matching the
 * selected positive and negative phenotypes.
 *
 * @param phenotypes the list of already selected phenotypes
 * @param nphenotypes phenotypes that are not observed in the patient
 * @return the computed Solr query parameters
 *///  w  w  w  . j av  a 2  s.  com
private MapSolrParams prepareParams(Collection<String> phenotypes, Collection<String> nphenotypes) {
    Map<String, String> params = new HashMap<String, String>();
    String q = "symptom:" + StringUtils.join(phenotypes, " symptom:");
    if (nphenotypes.size() > 0) {
        q += "  not_symptom:" + StringUtils.join(nphenotypes, " not_symptom:");
    }
    params.put(CommonParams.Q, q.replaceAll("HP:", "HP\\\\:"));
    params.put(CommonParams.ROWS, "100");
    params.put(CommonParams.START, "0");
    params.put(CommonParams.DEBUG_QUERY, Boolean.toString(true));
    params.put(CommonParams.EXPLAIN_STRUCT, Boolean.toString(true));

    return new MapSolrParams(params);
}

From source file:jp.co.atware.solr.geta.GETAssocComponent.java

License:Apache License

/**
 * {@inheritDoc}//from w w w  .  j ava  2  s  . co  m
 * 
 * @see SearchComponent#init(NamedList)
 */
@Override
public void init(@SuppressWarnings("rawtypes") NamedList args) {
    super.init(args);

    GETAssocConfig config = new GETAssocConfig();
    if (args != null) {
        NamedList<?> geta = (NamedList<?>) args.get(CONFIG_GETA);
        if (geta != null) {

            // GETA???
            NamedList<?> settings = (NamedList<?>) geta.get("settings");
            if (settings != null) {
                config.settings.gss3url = (String) settings.get(CONFIG_GSS3URL);
                NamedList<?> process = (NamedList<?>) settings.get("process");
                @SuppressWarnings("unchecked")
                List<NamedList<?>> req = (List<NamedList<?>>) process.get("req");
                if (req != null) {
                    for (NamedList<?> list : req) {
                        String param = (String) list.get("param");
                        String type = (String) list.get("type");
                        QueryType queryType = GETAssocConfig.QueryType.valueOf(type);
                        if (param != null && !param.isEmpty()) {
                            config.settings.req.put(param, queryType);
                        }
                    }
                }
                @SuppressWarnings("unchecked")
                List<NamedList<?>> doc = (List<NamedList<?>>) process.get("doc");
                if (doc != null) {
                    for (NamedList<?> list : doc) {
                        String param = (String) list.get("field");
                        String type = (String) list.get("type");
                        QueryType queryType = GETAssocConfig.QueryType.valueOf(type);
                        if (param != null && !param.isEmpty()) {
                            config.settings.doc.put(param, queryType);
                        }
                    }
                }
            }

            // ????
            NamedList<?> defaults = (NamedList<?>) geta.get("defaults");
            if (defaults != null) {

                // stage1
                config.defaults.target = (String) defaults.get(CONFIG_TARGET);
                config.defaults.niwords = (String) defaults.get(CONFIG_NIWORDS);
                config.defaults.cutoff_df = (String) defaults.get(CONFIG_CUTOFF_DF);
                config.defaults.stage1_sim = (String) defaults.get(CONFIG_STAGE1_SIM);

                // stage2
                config.defaults.narticles = (String) defaults.get(CONFIG_NARTICLES);
                config.defaults.nkeywords = (String) defaults.get(CONFIG_NKEYWORDS);
                config.defaults.yykn = (String) defaults.get(CONFIG_YYKN);
                config.defaults.nacls = (String) defaults.get(CONFIG_NACLS);
                config.defaults.nkcls = (String) defaults.get(CONFIG_NKCLS);
                config.defaults.a_offset = (String) defaults.get(CONFIG_A_OFFSET);
                config.defaults.a_props = (String) defaults.get(CONFIG_A_PROPS);
                config.defaults.cross_ref = (String) defaults.get(CONFIG_CROSS_REF);
                config.defaults.stage2_sim = (String) defaults.get(CONFIG_STAGE2_SIM);

                // freetext
                config.defaults.stemmer = (String) defaults.get(CONFIG_STEMMER);
                config.defaults.freetext_cutoff_df = (String) defaults.get(CONFIG_FREETEXT_CUTOFF_DF);

                // article
                config.defaults.source = (String) defaults.get(CONFIG_SOURCE);
                config.defaults.article_cutoff_df = (String) defaults.get(CONFIG_ARTICLE_CUTOFF_DF);
            }
        }
    }
    if (config.settings.gss3url == null) {
        throw new IllegalStateException(
                "Config '" + CONFIG_GETA + "/" + "settings" + "/" + CONFIG_GSS3URL + "' is requierd.");
    }
    if (config.settings.req.isEmpty() && config.settings.doc.isEmpty()) {
        config.settings.req.put(CommonParams.Q, QueryType.fulltext);
    }

    this.config = config;

    HashMap<String, ValueOf> map = new HashMap<String, ValueOf>();
    map.put("score", ValueOf.DOUBLE);
    map.put("total", ValueOf.INT);
    map.put("user-time", ValueOf.FLOAT);

    this.valueTransMap = map;
}

From source file:learning.SolrJTest.java

License:Mozilla Public License

@Test
@Ignore("it used to work because segments were there. It shouldnt depend on that.")
public void can_get_auto_complete_suggestions() throws Exception {
    add2Documents();//from   ww w .  ja  v  a  2 s. c o  m
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.QT, "/suggest");
    solrQuery.setParam(CommonParams.Q, "do");
    List<String> suggestions = suggestionsOfQuery(solrQuery);
    assertThat(suggestions.size(), is(2));
}

From source file:lux.solr.XQueryComponent.java

License:Mozilla Public License

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    if (searchHandler == null) {
        // bleah -- we need a link to the search handler to pass down in to the bowels of
        // XQuery evaluation so we can recurse when we come to a search call.  To get that,
        // we can only traverse the core registry, but due to order of initialization, the
        // handler won't have been linked to this component until after all the inform() calls 
        // are done.
        //  A possible alternative here would be to write our own search handler that extends
        // the Solr one and adds itself to the ResponseBuilder...
        findSearchHandler();//from   w  w w.  j  a v  a 2s .  co  m
    }
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    if (rb.getQueryString() == null) {
        rb.setQueryString(params.get(CommonParams.Q));
    }
    String contentType = params.get("lux.contentType");
    // TODO: make this a local variable in or near #addResult, not an instance variable: it's not threadsafe
    serializer = solrIndexConfig.checkoutSerializer();
    if (contentType != null) {
        if (contentType.equals("text/html")) {
            serializer.setOutputProperty(Serializer.Property.METHOD, "html");
        } else if (contentType.equals("text/xml")) {
            serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
        }
    } else {
        serializer.setOutputProperty(Serializer.Property.METHOD, getDefaultSerialization());
    }
    if (queryPath == null) {
        // allow subclasses to override...
        queryPath = rb.req.getParams().get(LUX_XQUERY);
    }
    resultByteSize = 0;
}

From source file:net.sr_sl.solr.ssq.SsqQueryComponent.java

License:Apache License

/**
 * Modify the query parameters/*from  w w  w.  j  a  v  a 2 s.c  o m*/
 */
private boolean modifyQueryRequest(ResponseBuilder rb) throws IOException {

    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();

    // check whether server side queries is active for this request
    if (!params.getBool(SSQ_PREFIX, false))
        return false;

    // get parameters to use
    String ssqQuery = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY));
    String ssqParam = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_PARAM), SSQ_PARAM_DFT);

    // when ssqQuery or ssqParam is not set, don't modify
    if (ssqQuery == null || ssqQuery.isEmpty() || ssqParam.isEmpty())
        return false;

    // Get original value for ssqParam and return when already set
    String ssqParamVal = params.get(ssqParam);
    if (ssqParamVal != null && !ssqParamVal.isEmpty())
        return false;

    // Get original query string value
    String origQueryString = rb.getQueryString();
    String origQVal = req.getOriginalParams().get(CommonParams.Q);

    // Retrieve value to use as query-term; when empty, use q.alt
    String qVal = origQVal;
    if (qVal == null || qVal.isEmpty()) {
        String alt_q = params.get(DisMaxParams.ALTQ);
        if (alt_q != null && !alt_q.isEmpty()) {
            qVal = alt_q;
        }
    }

    // Get value for ssqQuery
    String ssqQueryVal = params
            .get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_DELIM).concat(ssqQuery));
    // When value not found, assume that ssqQuery is the query to execute
    // per default
    if (ssqQueryVal == null || ssqQueryVal.isEmpty())
        ssqQueryVal = ssqQuery;

    // Perform replacement
    ModifiableSolrParams mparams = new ModifiableSolrParams();

    // Set flag to indicate that replacement is performed
    mparams.set(SSQ_PREFIX.concat(SSQ_APPLIED_SUFFIX), Boolean.toString(true));

    // Store original querystring when <> q
    if (origQVal != null && !origQVal.equals(origQueryString))
        mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERYSTRING).concat(SSQ_APPLIED_SUFFIX),
                origQueryString);

    // Perform the switch (qVal --> ssqParam)
    mparams.set(ssqParam, qVal);
    mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_APPLIED_SUFFIX), ssqQueryVal);

    // set the extra parameters
    req.setParams(SolrParams.wrapAppended(req.getParams(), mparams));

    // set queryString to query
    rb.setQueryString(ssqQueryVal);

    return true;
}

From source file:net.sr_sl.solr.ssq.SsqQueryComponent.java

License:Apache License

/**
 * Restore the query parameters Restore queryString and reset original q (to
 * ensure further components will be processed correctly)
 *//*w ww  . ja va2 s  . co m*/
private boolean restoreQueryRequest(ResponseBuilder rb) {

    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();

    // check whether server side queries is active for this request
    if (!params.getBool(SSQ_PREFIX, false))
        return false;

    // check whether values where replaced
    if (!params.getBool(SSQ_PREFIX.concat(SSQ_APPLIED_SUFFIX), false))
        return false;

    // retrieve original queryString and q
    String queryString = params
            .get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERYSTRING).concat(SSQ_APPLIED_SUFFIX));
    boolean useStoredQueryString = (queryString != null && queryString.isEmpty());

    // remove flag that was possibly set to store original queryString
    if (!useStoredQueryString) {
        ModifiableSolrParams mparams = new ModifiableSolrParams(params);
        mparams.remove(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERYSTRING).concat(SSQ_APPLIED_SUFFIX));
        req.setParams(mparams);
    } else {
        queryString = req.getOriginalParams().get(CommonParams.Q);
    }

    // set queryString to original query
    rb.setQueryString(queryString);

    return true;
}

From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java

License:Open Source License

public SolrQueryResponse query(SolrQueryRequest req) throws SolrException {
    final long startTime = System.currentTimeMillis();

    // during the solr query we set the thread name to the query string to get more debugging info in thread dumps
    String q = req.getParams().get(CommonParams.Q);
    String fq = req.getParams().get(CommonParams.FQ);
    String sort = req.getParams().get(CommonParams.SORT);
    String threadname = Thread.currentThread().getName();
    if (q != null)
        Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq)
                + (sort == null ? "" : ", sort = " + sort)); // for debugging in Threaddump

    SolrQueryResponse rsp = new SolrQueryResponse();
    NamedList<Object> responseHeader = new SimpleOrderedMap<Object>();
    responseHeader.add("params", req.getOriginalParams().toNamedList());
    rsp.add("responseHeader", responseHeader);
    //SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));

    // send request to solr and create a result
    this.requestHandler.handleRequest(req, rsp);

    // get statistics and add a header with that
    Exception exception = rsp.getException();
    int status = exception == null ? 0
            : exception instanceof SolrException ? ((SolrException) exception).code() : 500;
    responseHeader.add("status", status);
    responseHeader.add("QTime", (int) (System.currentTimeMillis() - startTime));

    if (q != null)
        Thread.currentThread().setName(threadname);
    // return result
    return rsp;//from   w w w  .  j  a v  a2 s .c o m
}

From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java

License:Open Source License

/**
 * the usage of getResponseByParams is disencouraged for the embedded Solr connector. Please use request(SolrParams) instead.
 * Reason: Solr makes a very complex folding/unfolding including data compression for SolrQueryResponses.
 *//*  w  w  w . j  ava  2  s  . co m*/
@Override
public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException {
    if (this.server == null)
        throw new IOException("server disconnected");
    // during the solr query we set the thread name to the query string to get more debugging info in thread dumps
    String q = params.get(CommonParams.Q);
    String fl = params.get(CommonParams.FL);
    String[] fq = params.getParams(CommonParams.FQ);
    String threadname = Thread.currentThread().getName();
    if (q != null) {
        StringBuilder fqa = new StringBuilder();
        if (fq != null)
            for (String f : fq)
                fqa.append("fq=").append(f).append(' ');
        Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", " + fqa.toString())
                + (fl == null ? "" : ", fl=" + fl));
        //System.out.println("solr query: q = " + q + (fq == null ? "" : ", " + fqa.toString()) + (fl == null ? "" : ", fl=" + fl));
    }
    QueryResponse rsp;
    try {
        rsp = this.server.query(params);
        if (q != null)
            Thread.currentThread().setName(threadname);
        if (rsp != null)
            if (log.isFine())
                log.fine(rsp.getResults().getNumFound() + " results for q=" + q);
        return rsp;
    } catch (final SolrServerException e) {
        throw new IOException(e);
    } catch (final Throwable e) {
        throw new IOException("Error executing query", e);
    }
}

From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java

License:Open Source License

/**
 * get the solr document list from a query response
 * This differs from getResponseByParams in such a way that it does only create the fields of the response but
 * never search snippets and there are also no facets generated.
 * @param params//from   w ww  .j a  v a2  s.c o m
 * @return
 * @throws IOException
 * @throws SolrException
 */
@Override
public SolrDocumentList getDocumentListByParams(ModifiableSolrParams params) throws IOException, SolrException {
    SolrQueryRequest req = this.request(params);
    SolrQueryResponse response = null;
    String q = params.get(CommonParams.Q);
    String fq = params.get(CommonParams.FQ);
    String sort = params.get(CommonParams.SORT);
    String threadname = Thread.currentThread().getName();
    try {
        if (q != null)
            Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq)
                    + (sort == null ? "" : ", sort = " + sort)); // for debugging in Threaddump
        response = this.query(req);
        if (q != null)
            Thread.currentThread().setName(threadname);
        if (response == null)
            throw new IOException("response == null");
        return SolrQueryResponse2SolrDocumentList(req, response);
    } finally {
        req.close();
        SolrRequestInfo.clearRequestInfo();
    }
}