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:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java

License:Apache License

private SolrQueryResponse query(String query, String df, String[] fields, String[] fqs) {

    SolrQueryResponse rsp = new SolrQueryResponse();

    //append *// ww w.  java2s. c o m
    if (!query.endsWith("*")) {
        query = query.trim() + "*";
    }

    //Prepare query
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrQueryRequest req = new LocalSolrQueryRequest(solrCore, params);
    params.add(CommonParams.Q, query.toLowerCase());
    params.add(CommonParams.DF, df);
    params.add("q.op", "AND");
    params.add(FacetParams.FACET, "true");
    params.add(FacetParams.FACET_LIMIT, internalFacetLimit);
    params.add(FacetParams.FACET_MINCOUNT, "1");
    for (String field : fields) {
        params.add(FacetParams.FACET_FIELD, field);
    }
    if (fqs != null) {
        for (String fq : fqs) {
            params.add(CommonParams.FQ, fq);
        }
    }

    if (spellcheck_enabled) {
        params.add("spellcheck", "true");
        params.add("spellcheck.collate", "true");
    }

    try {
        //execute query and return
        searchHandler.handleRequestBody(req, rsp);
        return rsp;
    } catch (SolrException se) {
        throw se;
    } catch (Exception e) {
        e.printStackTrace();
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "internal server error");
    } finally {
        req.close();
    }
}

From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {

    SolrParams params = req.getParams();

    if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) {

        String q = params.get(CommonParams.Q);
        if (q == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter"));
            return;
        }//from  w  ww .j  ava2  s.  c om

        String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null
                ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD)
                : FIELDS;
        if (fields == null) {
            rsp.add("error",
                    error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter"));
            return;
        }

        String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF);
        if (df == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter"));
            return;
        }

        int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT);
        if (limit < 1) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0"));
            return;
        }

        String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS;

        Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE);

        //TODO replace
        if (multivalue) {
            rsp.add("error", error(500, "Multivalue suggestions are not yet supported!"));
            return;
        }

        suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue);

    } else {
        super.handleRequestBody(req, rsp);
    }
}

From source file:at.pagu.soldockr.core.QueryParser.java

License:Apache License

/**
 * Convert given Query into a SolrQuery executable via {@link SolrServer}
 * /*from   w ww  . j  a v a  2  s.  c o  m*/
 * @param query
 * @return
 */
public final SolrQuery constructSolrQuery(SolDockRQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");
    Assert.notNull(query.getCriteria(), "Query has to have a criteria.");

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.Q, getQueryString(query));
    if (query instanceof Query) {
        processQueryOptions(solrQuery, (Query) query);
    }
    if (query instanceof FacetQuery) {
        processFacetOptions(solrQuery, (FacetQuery) query);
    }
    return solrQuery;
}

From source file:at.pagu.soldockr.core.QueryParserTest.java

License:Apache License

private void assertQueryStringPresent(SolrQuery solrQuery) {
    Assert.assertNotNull(solrQuery.get(CommonParams.Q));
}

From source file:com.browseengine.bobo.servlet.BrowseServlet.java

License:Open Source License

@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    SolrParams params = new BoboHttpRequestParam(req);
    String qstring = params.get(CommonParams.Q);
    String df = params.get(CommonParams.DF);
    String sortString = params.get(CommonParams.SORT);
    BoboDefaultQueryBuilder qbuilder = new BoboDefaultQueryBuilder();
    Query query = qbuilder.parseQuery(qstring, df);
    Sort sort = qbuilder.parseSort(sortString);
    BrowseRequest br = null;//from   w  ww.j ava  2s .c  o m
    try {
        br = BoboRequestBuilder.buildRequest(params, query, sort);
        BrowseResult result = _svc.browse(br);
        res.setCharacterEncoding("UTF-8");
        Writer writer = res.getWriter();

        try {
            String val = BrowseJSONSerializer.serialize(result);
            writer.write(val);
        } catch (JSONException je) {
            throw new IOException(je.getMessage());
        }
    } catch (BrowseException e) {
        throw new ServletException(e.getMessage(), e);
    }
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

@Override
public NamedList<Object> request(SolrRequest request) throws SolrServerException, IOException {
    SolrParams params = request.getParams();
    if (params == null) {
        params = new ModifiableSolrParams();
    }/*from  w w w  .j a  v a  2 s  . c  o m*/

    String openSearchURL = params.get("openSearchURL");
    if (openSearchURL == null) {
        throw new SolrServerException("openSearchURL param is missing");
    }
    String query = params.get(CommonParams.Q);
    int start = params.getInt(CommonParams.START, 0);
    int hitsPerPage = params.getInt(CommonParams.ROWS, 10);
    String lang = params.get("lang");

    Map<String, String> paramsMap = new HashMap<String, String>();
    if (SimpleSearch.SEARCH_ALL.equals(query)) {
        query = "url:http";
    }
    paramsMap.put("query", query);
    if (StringUtils.isNotBlank(lang)) {
        paramsMap.put("lang", "" + lang);
    }
    paramsMap.put("start", "" + start);
    paramsMap.put("hitsPerPage", "" + hitsPerPage);
    // FIXME
    paramsMap.put("hitsPerDup", "" + Integer.MAX_VALUE);
    Element rootElement = sendGet(openSearchURL, paramsMap);

    SolrDocumentList solrDocumentList = parse(rootElement);
    SimpleOrderedMap<Object> result = new SimpleOrderedMap<Object>();
    result.add("response", solrDocumentList);
    return result;
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    SolrDocumentList solrDocumentList;// w w  w .  jav a  2  s .com
    //        
    // InputStream inputXML = OpenSearchSolrServer.class.getResourceAsStream("opensearch_example.xml");
    // SAXReader saxReader = new SAXReader();
    // Document doc = saxReader.read(inputXML);
    // IOUtils.closeQuietly(inputXML);
    //
    // System.out.println("Mock request");
    // solrDocumentList = parse(doc.getRootElement());
    // printResults(solrDocumentList);

    System.out.println("Real request");
    OpenSearchSolrServer solrServer = new OpenSearchSolrServer();
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("openSearchURL", "http://recherched.gouv.qc.ca/internet/opensearch");
    params.add(CommonParams.Q, "doculibre");
    params.add(CommonParams.START, "5");
    params.add(CommonParams.ROWS, "10");
    params.add("lang", "en");
    NamedList<Object> results = solrServer.request(new QueryRequest(params));
    solrDocumentList = (SolrDocumentList) results.get("response");
    printResults(solrDocumentList);

    QueryResponse queryResponse = solrServer.query(params);
    solrDocumentList = queryResponse.getResults();
    printResults(solrDocumentList);
}

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

License:Apache License

/**
 * Convert given Query into a SolrQuery executable via
 * {@link org.apache.solr.client.solrj.SolrClient}
 *
 * @param query//from ww  w  .j a va2  s  .  c  om
 * @return
 */
@Override
public final SolrQuery doConstructSolrQuery(SolrDataQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");
    Assert.notNull(query.getCriteria(), "Query has to have a criteria.");

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.Q, getQueryString(query));
    if (query instanceof Query) {
        processQueryOptions(solrQuery, (Query) query);
    }
    if (query instanceof FacetQuery) {
        processFacetOptions(solrQuery, (FacetQuery) query);
    }
    if (query instanceof HighlightQuery) {
        processHighlightOptions(solrQuery, (HighlightQuery) query);
    }
    return solrQuery;
}

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

License:Apache License

@Override
public SolrQuery doConstructSolrQuery(TermsQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");

    SolrQuery solrQuery = new SolrQuery();
    String queryString = getQueryString(query);
    if (StringUtils.hasText(queryString)) {
        solrQuery.setParam(CommonParams.Q, queryString);
    }/*  ww w  . j a  v a2  s  .co m*/
    appendTermsOptionsToSolrQuery(query.getTermsOptions(), solrQuery);
    processTermsFields(solrQuery, query);
    appendRequestHandler(solrQuery, query.getRequestHandler());
    return solrQuery;
}

From source file:com.memonews.solr.handler.component.HighlightQueryComponent.java

License:Apache License

@Override
public void process(final ResponseBuilder rb) throws IOException {

    if (rb.doHighlights) {
        final SolrQueryRequest req = rb.req;
        final SolrParams params = req.getParams();
        final String qstr = params.get(CommonParams.Q);
        if (LOG.isDebugEnabled()) {
            LOG.info("Original query: " + qstr);
        }//from  ww  w.  j  a v  a  2s  .co m

        try {
            Query parsedQuery = parseAndCleanQuery(qstr, req, NAME);
            if (parsedQuery != null) {
                rb.setHighlightQuery(parsedQuery);
            }
        } catch (ParseException e1) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e1);
        }
    }
}