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

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

Introduction

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

Prototype

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

Source Link

Document

Add the given values to any existing name

Usage

From source file:org.vootoo.client.netty.NettySolrClientTest.java

License:Apache License

@Test
public void test_netty_client_solr_exception() {
    SolrQuery solrQuery = new SolrQuery("*:*");
    solrQuery.add(RequestParams.ErrorCode, "503");
    solrQuery.add(RequestParams.ErrorMsg, "test 503 solr exception");
    solrQuery.add(RequestParams.ErrorMeta, "myKey:otherV");

    try {//w  ww . jav a2 s  .  c o  m
        solrClient.query(solrQuery);

        Assert.fail("has Exception, but not throw");
    } catch (Exception e) {
        assertVootooException(e, 503, solrQuery, false);
    }

    solrQuery = new SolrQuery("*:*");
    solrQuery.add(RequestParams.ErrorCode, "429");
    solrQuery.add(RequestParams.ErrorMsg, "test 429 VootooException too many request");

    try {
        solrClient.query(solrQuery);
        Assert.fail("has Exception, but not throw");
    } catch (Exception e) {
        assertVootooException(e, 429, solrQuery, false);
    }

    //unknow
    int unknownCode = 1000000002;
    solrQuery = new SolrQuery("*:*");
    solrQuery.add(RequestParams.ErrorCode, String.valueOf(unknownCode));
    solrQuery.add(RequestParams.ErrorMsg, "test VootooException code=" + unknownCode);

    try {
        solrClient.query(solrQuery);
        Assert.fail("has Exception, but not throw");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SolrServerException);
        Throwable throwable = ((SolrServerException) e).getCause();
        assertVootooException(throwable, VootooException.VootooErrorCode.UNKNOWN.code, solrQuery, true);

        VootooException ve = (VootooException) throwable;
        Assert.assertEquals(ve.getUnknownCode(), unknownCode);
        //System.out.println(ve.getRemoteServer());
        //System.out.println(ve.getRemoteTrace());
    }
}

From source file:org.vootoo.client.netty.NettySolrClientTest.java

License:Apache License

@Test
public void test_in_query_other_exception() {
    SolrQuery solrQuery = new SolrQuery("*:*");
    solrQuery.add(RequestParams.ExceptionClass, RejectedExecutionException.class.getName());
    //solrQuery.add(RequestParams.ExceptionClass, SolrException.class.getName());

    try {/*from   w w  w  .  j  ava 2  s  .c om*/
        solrClient.query(solrQuery);
        Assert.fail("has Exception, but not throw");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof VootooException);

        VootooException ve = (VootooException) e;
        Assert.assertEquals(ve.code(), SolrException.ErrorCode.SERVER_ERROR.code);
        Assert.assertEquals(ve.getMessage(), "mock test ExceptionClass for junit");
        Assert.assertTrue(ve.getRemoteTrace() != null);
    }
}

From source file:org.xwiki.platform.search.internal.SolrjSearch.java

License:Open Source License

/**
 * @param queryPostProcessing query string
 * @param request Search request./*  w ww. jav a  2 s  .co m*/
 * @return solr query.
 */
private SolrQuery getSolrQuery(String queryPostProcessing, SearchRequest request) {
    SolrQuery solrQuery = new SolrQuery(queryPostProcessing);
    Map<String, String> searchParametersMap = request.getSearchParametersMap();

    // If query doesn't have language, Add a language filter query.
    if (!queryPostProcessing.contains(DocumentField.LANGUAGE)) {
        solrQuery.add("fq", "lang:" + getXWikiContext().getLanguage());
    }

    if (searchParametersMap != null && searchParametersMap.size() > 0) {
        for (Entry<String, String> entry : searchParametersMap.entrySet()) {
            if (entry.getKey().equals("qf") && !StringUtils.isEmpty(entry.getValue())) {
                String value = request.processQueryFrequency(entry.getValue());
                solrQuery.add(entry.getKey(), value);
            } else {
                if (!StringUtils.isEmpty(entry.getValue())) {
                    solrQuery.add(entry.getKey(), entry.getValue());
                }
            }

        }

    }

    return solrQuery;
}

From source file:ru.org.linux.search.SearchViewer.java

License:Apache License

public QueryResponse performSearch(SolrServer search) throws SolrServerException {
    SolrQuery params = new SolrQuery();
    // set search query params
    params.set("q", query.getQ());
    params.set("rows", SEARCH_ROWS);
    params.set("start", query.getOffset());

    params.set("qt", "edismax");

    if (query.getRange().getParam() != null) {
        params.add("fq", query.getRange().getParam());
    }/*from  ww  w . j av  a  2s.  c o  m*/

    if (query.getInterval().getRange() != null) {
        params.add("fq", query.getInterval().getRange());
    }

    params.setFacetMinCount(1);
    params.setFacet(true);

    String section = query.getSection();

    if (section != null && !section.isEmpty() && !"0".equals(section)) {
        params.add("fq", "{!tag=dt}section:" + query.getSection());
        params.addFacetField("{!ex=dt}section");

        params.addFacetField("{!ex=dt}group_id");
    } else {
        params.addFacetField("section");
        params.addFacetField("group_id");
    }

    if (query.getUser() != null) {
        User user = query.getUser();

        if (query.isUsertopic()) {
            params.add("fq", "topic_user_id:" + user.getId());
        } else {
            params.add("fq", "user_id:" + user.getId());
        }
    }

    if (query.getGroup() != 0) {
        params.add("fq", "{!tag=dt}group_id:" + query.getGroup());
    }

    params.set("sort", query.getSort().getParam());

    return search.query(params);
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

License:Apache License

public List<String> getGenesWithMoreProcedures(int n, ArrayList<String> resourceName)
        throws SolrServerException, InterruptedException, ExecutionException {
    List<String> genes = new ArrayList<>();
    SolrQuery q = new SolrQuery();

    if (resourceName != null) {
        q.setQuery(ObservationDTO.DATASOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + ObservationDTO.DATASOURCE_NAME + ":"));
    } else {/*w  w w  .  ja va 2s  . c om*/
        q.setQuery("*:*");
    }

    String geneProcedurePivot = ObservationDTO.GENE_SYMBOL + "," + ObservationDTO.PROCEDURE_NAME;

    q.add("facet.pivot", geneProcedurePivot);

    q.setFacet(true);
    q.setRows(1);
    q.setFacetMinCount(1);
    q.set("facet.limit", -1);

    System.out.println(
            "Solr url for getOverviewGenesWithMoreProceduresThan " + solr.getBaseURL() + "/select?" + q);
    QueryResponse response = solr.query(q);

    for (PivotField pivot : response.getFacetPivot().get(geneProcedurePivot)) {
        if (pivot.getPivot().size() >= n) {
            genes.add(pivot.getValue().toString());
        }
    }

    return genes;
}

From source file:uk.ac.ebi.phenotype.service.StatisticalResultService.java

License:Apache License

public GeneRowForHeatMap getResultsForGeneHeatMap(String accession, GenomicFeature gene,
        Map<String, Set<String>> map, String resourceName) {

    GeneRowForHeatMap row = new GeneRowForHeatMap(accession);
    Map<String, HeatMapCell> paramPValueMap = new HashMap<>();

    if (gene != null) {
        row.setSymbol(gene.getSymbol());
    } else {/*w w  w . j  a va  2  s .  c o  m*/
        System.err.println("error no symbol for gene " + accession);
    }

    for (String procedure : map.get(accession)) {
        paramPValueMap.put(procedure, null);
    }

    SolrQuery q = new SolrQuery().setQuery(StatisticalResultDTO.MARKER_ACCESSION_ID + ":\"" + accession + "\"")
            .addFilterQuery(StatisticalResultDTO.RESOURCE_NAME + ":\"" + resourceName + "\"")
            .setSort(StatisticalResultDTO.P_VALUE, SolrQuery.ORDER.asc)
            .addField(StatisticalResultDTO.PROCEDURE_STABLE_ID).addField(StatisticalResultDTO.STATUS)
            .addField(StatisticalResultDTO.P_VALUE).setRows(10000000);
    q.add("group", "true");
    q.add("group.field", StatisticalResultDTO.PROCEDURE_STABLE_ID);
    q.add("group.sort", StatisticalResultDTO.P_VALUE + " asc");

    try {
        GroupCommand groups = solr.query(q).getGroupResponse().getValues().get(0);
        for (Group group : groups.getValues()) {
            HeatMapCell cell = new HeatMapCell();
            SolrDocument doc = group.getResult().get(0);
            cell.setxAxisKey(doc.get(StatisticalResultDTO.PROCEDURE_STABLE_ID).toString());
            if (Double.valueOf(doc.getFieldValue(StatisticalResultDTO.P_VALUE).toString()) < 0.0001) {
                cell.setStatus("Significant call");
            } else if (doc.getFieldValue(StatisticalResultDTO.STATUS).toString().equals("Success")) {
                cell.setStatus("Data analysed, no significant call");
            } else {
                cell.setStatus("Could not analyse");
            }
            paramPValueMap.put(doc.getFieldValue(StatisticalResultDTO.PROCEDURE_STABLE_ID).toString(), cell);
        }
        row.setXAxisToCellMap(paramPValueMap);
    } catch (SolrServerException ex) {
        LOG.error(ex.getMessage());
    }
    return row;
}

From source file:uk.ac.ebi.phenotype.service.StatisticalResultService.java

License:Apache License

public List<GeneRowForHeatMap> getSecondaryProjectMapForResource(String resourceName) {

    List<GeneRowForHeatMap> res = new ArrayList<>();
    HashMap<String, GeneRowForHeatMap> geneRowMap = new HashMap<>(); // <geneAcc, row>
    List<BasicBean> procedures = getProceduresForDataSource(resourceName);

    for (BasicBean procedure : procedures) {
        SolrQuery q = new SolrQuery().setQuery(StatisticalResultDTO.RESOURCE_NAME + ":\"" + resourceName + "\"")
                .addFilterQuery(StatisticalResultDTO.PROCEDURE_STABLE_ID + ":" + procedure.getId())
                .setSort(StatisticalResultDTO.P_VALUE, SolrQuery.ORDER.asc)
                .addField(StatisticalResultDTO.PROCEDURE_STABLE_ID)
                .addField(StatisticalResultDTO.MARKER_ACCESSION_ID).addField(StatisticalResultDTO.MARKER_SYMBOL)
                .addField(StatisticalResultDTO.STATUS).addField(StatisticalResultDTO.P_VALUE).setRows(10000000);
        q.add("group", "true");
        q.add("group.field", StatisticalResultDTO.MARKER_ACCESSION_ID);
        q.add("group.sort", StatisticalResultDTO.P_VALUE + " asc");

        try {// w ww  .j ava2 s  . c om
            GroupCommand groups = solr.query(q).getGroupResponse().getValues().get(0);

            for (Group group : groups.getValues()) {
                GeneRowForHeatMap row;
                HeatMapCell cell = new HeatMapCell();
                SolrDocument doc = group.getResult().get(0);
                String geneAcc = doc.get(StatisticalResultDTO.MARKER_ACCESSION_ID).toString();
                Map<String, HeatMapCell> xAxisToCellMap = new HashMap<>();

                if (geneRowMap.containsKey(geneAcc)) {
                    row = geneRowMap.get(geneAcc);
                    xAxisToCellMap = row.getXAxisToCellMap();
                } else {
                    row = new GeneRowForHeatMap(geneAcc);
                    row.setSymbol(doc.get(StatisticalResultDTO.MARKER_SYMBOL).toString());
                    xAxisToCellMap.put(procedure.getId(), null);
                }
                cell.setxAxisKey(doc.get(StatisticalResultDTO.PROCEDURE_STABLE_ID).toString());
                if (Double.valueOf(doc.getFieldValue(StatisticalResultDTO.P_VALUE).toString()) < 0.0001) {
                    cell.setStatus(HeatMapCell.THREE_I_DEVIANCE_SIGNIFICANT);
                } else if (doc.getFieldValue(StatisticalResultDTO.STATUS).toString().equals("Success")) {
                    cell.setStatus(HeatMapCell.THREE_I_DATA_ANALYSED_NOT_SIGNIFICANT);
                } else {
                    cell.setStatus(HeatMapCell.THREE_I_COULD_NOT_ANALYSE);
                }
                xAxisToCellMap.put(doc.getFieldValue(StatisticalResultDTO.PROCEDURE_STABLE_ID).toString(),
                        cell);
                row.setXAxisToCellMap(xAxisToCellMap);
                geneRowMap.put(geneAcc, row);
            }
        } catch (SolrServerException ex) {
            LOG.error(ex.getMessage());
        }
    }

    res = new ArrayList<>(geneRowMap.values());
    Collections.sort(res, new GeneRowForHeatMap3IComparator());

    return res;
}

From source file:uk.ac.ebi.phenotype.service.StatisticalResultService.java

License:Apache License

public List<BasicBean> getProceduresForDataSource(String resourceName) {

    List<BasicBean> res = new ArrayList();
    SolrQuery q = new SolrQuery().setQuery(StatisticalResultDTO.RESOURCE_NAME + ":\"" + resourceName + "\"")
            .setRows(10000);/*from   w w  w  .j  av a 2s .  c  o m*/
    q.add("group", "true");
    q.add("group.field", StatisticalResultDTO.PROCEDURE_NAME);
    q.add("group.rows", "1");
    q.add("fl", StatisticalResultDTO.PROCEDURE_NAME + "," + StatisticalResultDTO.PROCEDURE_STABLE_ID);

    System.out.println("Procedure query " + solr.getBaseURL() + "/select?" + q);

    try {
        GroupCommand groups = solr.query(q).getGroupResponse().getValues().get(0);
        for (Group group : groups.getValues()) {
            BasicBean bb = new BasicBean();
            SolrDocument doc = group.getResult().get(0);
            bb.setName(doc.getFieldValue(StatisticalResultDTO.PROCEDURE_NAME).toString());
            bb.setId(doc.getFieldValue(StatisticalResultDTO.PROCEDURE_STABLE_ID).toString());
            res.add(bb);
        }
    } catch (SolrServerException ex) {
        LOG.error(ex.getMessage());
    }
    return res;
}

From source file:uk.co.flax.biosolr.ontology.search.jena.TextIndexSolr5.java

License:Apache License

private SolrDocumentList solrQuery(String qs, int limit) {
    SolrQuery sq = new SolrQuery(qs);
    sq.setIncludeScore(true);//  w  w w .  j av  a2  s .  c  o m
    if (limit > 0)
        sq.setRows(limit);
    else
        sq.setRows(MAX_N); // The Solr default is 10.
    try {
        // Set default field.
        sq.add(CommonParams.DF, docDef.getPrimaryField());
        QueryResponse rsp = solrClient.query(sq);
        SolrDocumentList docs = rsp.getResults();
        return docs;
    } catch (SolrServerException | IOException e) {
        exception(e);
        return null;
    }
}