List of usage examples for org.apache.solr.client.solrj SolrQuery set
public ModifiableSolrParams set(String name, String... val)
From source file:org.mousephenotype.cda.solr.service.StatisticalResultService.java
License:Apache License
/** * This map is needed for the summary on phenotype pages (the percentages & * pie chart). It takes a long time to load so it does it asynchronously. * * @param sex/*from w ww . j a v a 2 s . co m*/ * @return Map < String parameterStableId , List<String * geneMgiIdWithParameterXMeasured>> * @throws SolrServerException, IOException * @throws InterruptedException * @throws ExecutionException * @author tudose */ public Map<String, List<String>> getParameterToGeneMap(SexType sex) throws SolrServerException, IOException, InterruptedException, ExecutionException { Map<String, List<String>> res = new ConcurrentHashMap<>(); //<parameter, <genes>> String pivotFacet = StatisticalResultDTO.PARAMETER_STABLE_ID + "," + StatisticalResultDTO.MARKER_ACCESSION_ID; SolrQuery q = new SolrQuery().setQuery(ObservationDTO.SEX + ":" + sex.name()); q.setFilterQueries(StatisticalResultDTO.STRAIN_ACCESSION_ID + ":\"" + StringUtils.join( OverviewChartsConstants.B6N_STRAINS, "\" OR " + ObservationDTO.STRAIN_ACCESSION_ID + ":\"") + "\""); q.set("facet.pivot", pivotFacet); q.setFacet(true); q.setRows(1); q.set("facet.limit", -1); logger.info("Solr url for getParameterToGeneMap " + SolrUtils.getBaseURL(statisticalResultCore) + "/select?" + q); QueryResponse response = statisticalResultCore.query(q); for (PivotField pivot : response.getFacetPivot().get(pivotFacet)) { if (pivot.getPivot() != null) { List<String> genes = new ArrayList<>(); for (PivotField gene : pivot.getPivot()) { genes.add(gene.getValue().toString()); } res.put(pivot.getValue().toString(), new ArrayList<String>(genes)); } } return res; }
From source file:org.mousephenotype.cda.solr.service.StatisticalResultService.java
License:Apache License
public List<Group> getGenesBy(String mpId, SexType sex) throws SolrServerException, IOException { SolrQuery q = new SolrQuery() .setQuery("(" + StatisticalResultDTO.MP_TERM_ID + ":\"" + mpId + "\" OR " + StatisticalResultDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + mpId + "\" OR " + StatisticalResultDTO.MP_TERM_ID_OPTIONS + ":\"" + mpId + "\" OR " + StatisticalResultDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + mpId + "\")") .setRows(10000).setSort(StatisticalResultDTO.DOCUMENT_ID, SolrQuery.ORDER.asc); q.set("group.field", "" + StatisticalResultDTO.MARKER_SYMBOL); q.set("group", true); q.set("group.limit", 0); if (sex != null) { q.addFilterQuery("(" + StatisticalResultDTO.PHENOTYPE_SEX + ":" + sex.getName() + " OR " + StatisticalResultDTO.SEX + ":" + sex.getName() + ")"); }/* ww w . j av a2s. c o m*/ System.out.println("Query: " + q); QueryResponse results = statisticalResultCore.query(q); System.out.println("Results: " + results.getGroupResponse().getValues().get(0).getValues().size()); return results.getGroupResponse().getValues().get(0).getValues(); }
From source file:org.mousephenotype.cda.solr.service.StatisticalResultService.java
License:Apache License
/** * * @param mpId//from w w w . j a v a2 s .c o m * @return List of stable ids for parameters that led to at least one association to the * given parameter or some class in its subtree * @throws SolrServerException, IOException * @author tudose */ public List<String> getParametersForPhenotype(String mpId) throws SolrServerException, IOException { List<String> res = new ArrayList<>(); SolrQuery q = new SolrQuery() .setQuery( "(" + StatisticalResultDTO.MP_TERM_ID + ":\"" + mpId + "\" OR " + StatisticalResultDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + mpId + "\" OR " + StatisticalResultDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + mpId + "\") AND (" + StatisticalResultDTO.STRAIN_ACCESSION_ID + ":\"" + StringUtils.join(OverviewChartsConstants.B6N_STRAINS, "\" OR " + GenotypePhenotypeDTO.STRAIN_ACCESSION_ID + ":\"") + "\")") .setRows(0); q.set("facet.field", "" + StatisticalResultDTO.PARAMETER_STABLE_ID); q.set("facet", true); q.set("facet.limit", -1); q.set("facet.mincount", 1); QueryResponse response = statisticalResultCore.query(q); for (Count parameter : response.getFacetField(StatisticalResultDTO.PARAMETER_STABLE_ID).getValues()) { res.add(parameter.getName()); } return res; }
From source file:org.ofbiz.solr.SolrProductSearch.java
License:Apache License
public static Map<String, Object> runSolrSuggest(DispatchContext dctx, Map<String, Object> context) { // get Connection HttpSolrServer server = null;// w w w . j a v a 2 s .c o m Map<String, Object> result; Integer viewIndex = (Integer) context.get("viewIndex"); Integer viewSize = (Integer) context.get("viewSize"); if (viewIndex < 1) { viewIndex = 1; } try { server = new HttpSolrServer(SolrUtil.solrUrl); // create Query Object SolrQuery solrQuery = new SolrQuery(); solrQuery.set("qt", "/suggest"); solrQuery.setQuery((String) context.get("query")); QueryResponse rsp = server.query(solrQuery); result = ServiceUtil.returnSuccess(); Debug.log("suggest" + rsp.getResponse().get("suggest"), module); ; result.put("queryResult", rsp); } catch (Exception e) { Debug.logError(e, e.getMessage(), module); result = ServiceUtil.returnError(e.toString()); } return result; }
From source file:org.opencms.search.solr.TestSolrSearch.java
License:Open Source License
/** * @throws Throwable/*w w w .ja v a 2 s . c om*/ */ public void testAdvancedHighlighting() throws Throwable { // TODO: improve echo("Testing highlighting"); // creating the query: facet=true&facet.field=Title_exact&facet.mincount=1&facet.query=text:OpenCms&rows=0 SolrQuery query = new CmsSolrQuery(getCmsObject(), null); // hl=true query.setHighlight(true); // add df to the query - otherwise highlighting will fail query.set("df", "text"); CmsSolrIndex index = OpenCms.getSearchManager().getIndexSolr(AllTests.SOLR_ONLINE); CmsSolrResultList results = index.search(getCmsObject(), query); assertNotNull(results.getHighLighting()); echo("Highlighting works fine!"); CmsSolrQuery q = new CmsSolrQuery(getCmsObject(), null); q.setTextSearchFields("content_en"); q.setText("OpenCms"); q.setHighlight(true); q.setHighlightFragsize(200); q.setHighlightFields("content_en"); CmsSolrResultList res = OpenCms.getSearchManager().getIndexSolr(AllTests.SOLR_ONLINE).search(getCmsObject(), q); Map<String, Map<String, List<String>>> highlighting = res.getHighLighting(); assertTrue("There should be some highlighted documents", highlighting != null); if (highlighting != null) { for (Map<String, List<String>> map : highlighting.values()) { for (List<String> entry : map.values()) { for (String s : entry) { assertTrue("There must occure OpenCms in the highlighting", s.toLowerCase().contains("OpenCms".toLowerCase())); } } } } }
From source file:org.opencommercesearch.AbstractSearchServer.java
License:Apache License
@Override public SearchResponse browse(BrowseOptions options, SolrQuery query, Site site, Locale locale, FilterQuery... filterQueries) throws SearchServerException { boolean hasCategoryId = StringUtils.isNotBlank(options.getCategoryId()); boolean hasCategoryPath = StringUtils.isNotBlank(options.getCategoryPath()); boolean hasBrandId = StringUtils.isNotBlank(options.getBrandId()); boolean addCategoryGraph = (options.isFetchCategoryGraph() || (hasBrandId && options.isFetchProducts() && !hasCategoryId)) && !options.isRuleBasedPage(); String categoryPath = null;/* w w w .j a v a2s .co m*/ if (hasCategoryPath) { categoryPath = options.getCategoryPath(); } else { categoryPath = options.getCatalogId() + "."; } if (options.isRuleBasedPage()) { //handle rule based pages String filter = rulesBuilder.buildRulesFilter(options.getCategoryId(), locale); query.addFilterQuery(filter); query.setParam("q", "*:*"); } else { //handle brand, category or onsale pages if (addCategoryGraph) { query.setFacetPrefix(CATEGORY_PATH, categoryPath); query.addFacetField(CATEGORY_PATH); query.set("f.categoryPath.facet.limit", options.getMaxCategoryResults()); } if (!options.isFetchProducts()) { query.setRows(0); } List<String> queryAltParams = new ArrayList<String>(); if (hasCategoryId) { queryAltParams.add(CATEGORY_PATH + ":" + categoryPath); query.setParam("q", ""); } if (hasBrandId) { queryAltParams.add(BRAND_ID + ":" + options.getBrandId()); query.setParam("q", ""); } if (options.isOnSale()) { queryAltParams.add("onsale" + locale.getCountry() + ":true"); } if (queryAltParams.size() > 0) { query.set(Q_ALT, "(" + StringUtils.join(queryAltParams, " AND ") + ")"); } } RepositoryItem catalog = null; if (site != null) { catalog = (RepositoryItem) site.getPropertyValue("defaultCatalog"); } SearchResponse response = null; if (options.isRuleBasedPage()) { response = doSearch(query, site, catalog, locale, false, true, categoryPath, options.isOnSale(), options.getBrandId(), filterQueries); } else if (hasCategoryPath) { response = doSearch(query, site, catalog, locale, false, false, categoryPath, options.isOnSale(), options.getBrandId(), filterQueries); } else { response = doSearch(query, site, catalog, locale, false, false, null, options.isOnSale(), options.getBrandId(), filterQueries); } if (addCategoryGraph) { response.setCategoryGraph( createCategoryGraph(response, options.getCategoryPath(), options.getCatalogId(), options.getCategoryId(), options.getDepthLimit(), options.getSeparator())); } return response; }
From source file:org.opencommercesearch.AbstractSearchServer.java
License:Apache License
private SearchResponse doSearch(SolrQuery query, Site site, RepositoryItem catalog, Locale locale, boolean isSearch, boolean isRuleBasedPage, String categoryPath, boolean isOutletPage, String brandId, FilterQuery... filterQueries) throws SearchServerException { if (site == null) { throw new IllegalArgumentException("Missing site"); }//from w ww .j av a 2 s . c o m if (catalog == null) { throw new IllegalArgumentException("Missing catalog"); } long startTime = System.currentTimeMillis(); query.addFacetField("category"); query.set("facet.mincount", 1); RuleManager ruleManager = new RuleManager(getSearchRepository(), getRulesBuilder(), getRulesSolrServer(locale)); if ((query.getRows() != null && query.getRows() > 0) || (query.get("group") != null && query.getBool("group"))) { setGroupParams(query, locale); setFieldListParams(query, locale.getCountry(), catalog.getRepositoryId()); try { ruleManager.setRuleParams(query, isSearch, isRuleBasedPage, categoryPath, filterQueries, catalog, isOutletPage, brandId); if (ruleManager.getRules().containsKey(SearchRepositoryItemDescriptor.REDIRECT_RULE)) { Map<String, List<RepositoryItem>> rules = ruleManager.getRules(); List<RepositoryItem> redirects = rules.get(SearchRepositoryItemDescriptor.REDIRECT_RULE); if (redirects != null) { RepositoryItem redirect = redirects.get(0); return new SearchResponse(query, null, null, null, (String) redirect.getPropertyValue(RedirectRuleProperty.URL), null, true); } } } catch (RepositoryException ex) { if (isLoggingError()) { logError("Unable to load search rules: " + ex.getMessage()); } throw create(SEARCH_EXCEPTION, ex); } catch (SolrServerException ex) { if (isLoggingError()) { logError("Unable to load search rules: " + ex.getMessage()); } throw create(SEARCH_EXCEPTION, ex); } catch (SolrException ex) { if (isLoggingError()) { logError("Unable to load search rules: " + ex.getMessage()); } throw create(SEARCH_EXCEPTION, ex); } } else { ruleManager.setFilterQueries(filterQueries, catalog.getRepositoryId(), query); } try { QueryResponse queryResponse = getCatalogSolrServer(locale).query(query); String correctedTerm = null; boolean matchesAll = true; //if no results, check for spelling errors if (query.getRows() > 0 && isEmptySearch(queryResponse.getGroupResponse()) && StringUtils.isNotEmpty(query.getQuery())) { SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse(); //try to do searching for the corrected term matching all terms (q.op=AND) QueryResponse tentativeResponse = handleSpellCheck(spellCheckResponse, getCatalogSolrServer(locale), query, "AND"); if (tentativeResponse != null) { //if we got results, set the corrected term variable and proceed to return the results queryResponse = tentativeResponse; correctedTerm = spellCheckResponse.getCollatedResult(); } else { //if we didn't got any response, try doing another search matching any term (q.op=OR) tentativeResponse = handleSpellCheck(spellCheckResponse, getCatalogSolrServer(locale), query, "OR"); if (tentativeResponse != null) { //if we got results for the match any term scenario. Set similar results to true //and set the corrected term. queryResponse = tentativeResponse; matchesAll = false; correctedTerm = query.getQuery(); } } } long searchTime = System.currentTimeMillis() - startTime; if (isLoggingDebug()) { logDebug("Search time is " + searchTime + ", search engine time is " + queryResponse.getQTime()); } SearchResponse searchResponse = new SearchResponse(query, queryResponse, ruleManager, filterQueries, null, correctedTerm, matchesAll); searchResponse.setRuleQueryTime(ruleManager.getLoadRulesTime()); return searchResponse; } catch (SolrServerException ex) { throw create(SEARCH_EXCEPTION, ex); } catch (SolrException ex) { throw create(SEARCH_EXCEPTION, ex); } }
From source file:org.opencommercesearch.AbstractSearchServer.java
License:Apache License
public void setGroupParams(SolrQuery query, Locale locale) { query.set("group", true); query.set("group.ngroups", true); query.set("group.limit", 50); query.set("group.field", "productId"); query.set("group.facet", false); if (isGroupSortingEnabled()) { List<SolrQuery.SortClause> clauses = query.getSorts(); boolean isSortByScore = false; if (clauses.size() > 0) { for (SolrQuery.SortClause clause : clauses) { if (SCORE.equals(clause.getItem())) { isSortByScore = true; }/*from w ww . ja va 2s . c o m*/ } } else { isSortByScore = true; } if (isSortByScore) { // break ties with custom sort field query.set("group.sort", "isCloseout asc, salePrice" + locale.getCountry() + " asc, sort asc, score desc"); } } }
From source file:org.pentaho.di.trans.steps.solrinput.SolrInput.java
License:Apache License
/** * Once the transformation starts executing, the processRow() method is called repeatedly * by PDI for as long as it returns true. To indicate that a step has finished processing rows * this method must call setOutputDone() and return false; * // w ww. java 2s. c o m * Steps which process incoming rows typically call getRow() to read a single row from the * input stream, change or add row content, call putRow() to pass the changed row on * and return true. If getRow() returns null, no more rows are expected to come in, * and the processRow() implementation calls setOutputDone() and returns false to * indicate that it is done too. * * Steps which generate rows typically construct a new row Object[] using a call to * RowDataUtil.allocateRowData(numberOfFields), add row content, and call putRow() to * pass the new row on. Above process may happen in a loop to generate multiple rows, * at the end of which processRow() would call setOutputDone() and return false; * * @param smi the step meta interface containing the step settings * @param sdi the step data interface that should be used to store * * @return true to indicate that the function should be called again, false if the step is done */ public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { if (first) { first = false; // Create the output row meta-data data.outputRowMeta = new RowMeta(); meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore); // For String to <type> conversions, we allocate a conversion meta data row as well... data.convertRowMeta = data.outputRowMeta.cloneToType(ValueMetaInterface.TYPE_STRING); // get real values boolean tryCursor = true; Integer startRecord = 0; Integer chunkRowSize = 20; String realURL = meta.getURL(); String realQ = meta.getQ(); String realSort = meta.getSort(); String realCursor = meta.getCursor(); String realFq = meta.getFq(); String realFl = meta.getFl(); String realFacetQuery = meta.getFacetQuery(); String realFacetField = meta.getFacetField(); /* Send and Get the report */ SolrQuery query = new SolrQuery(); query.set("rows", chunkRowSize); if (realQ != null && !realQ.equals("")) { query.set("q", realQ); } if (realSort != null && !realSort.equals("")) { query.set("sort", realSort); } else { tryCursor = false; } if (realCursor != null && !realCursor.equals("")) { if (realCursor.equals("No")) { tryCursor = false; } } if (realFl != null && !realFl.equals("")) { query.set("fl", realFl); } if (realFq != null && !realFq.equals("")) { query.set("fq", realFq); } if (realFacetField != null && !realFacetField.equals("")) { //TODO incorporate multiple facet fields at once //String[] facetFields = realFacetField.split("\\s*,\\s*"); //for(int i =0; i < facetFields.length; i++){ query.addFacetField(realFacetField); //} query.setFacet(true); query.setFacetLimit(-1); query.setFacetMinCount(0); query.setFacetSort("count"); query.set("rows", 0); tryCursor = false; data.facetRequested = true; } if (realFacetQuery != null && !realFacetQuery.equals("")) { query.addFacetQuery(realFacetQuery); } // You can't use "TimeAllowed" with "CursorMark" // The documentation says "Values <= 0 mean // no time restriction", so setting to 0. query.setTimeAllowed(0); HttpSolrServer solr = new HttpSolrServer(realURL); String cursorMark = CursorMarkParams.CURSOR_MARK_START; boolean done = false; QueryResponse rsp = null; while (!done) { if (tryCursor) { query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark); } else { query.setStart(startRecord); } try { rsp = solr.query(query); } catch (SolrServerException e) { e.printStackTrace(); } if (data.facetRequested) { data.facetCountName = rsp.getFacetFields().get(0).getName(); data.facetCounts = rsp.getFacetFields().get(0).getValues(); done = true; } else { SolrDocumentList theseDocs = rsp.getResults(); for (SolrDocument doc : theseDocs) { data.documentList.add(doc); } } if (tryCursor) { String nextCursorMark = rsp.getNextCursorMark(); if (cursorMark.equals(nextCursorMark)) { done = true; } else { cursorMark = nextCursorMark; } } else { startRecord = startRecord + chunkRowSize; if (startRecord >= rsp.getResults().getNumFound()) { done = true; } } } } Object[] outputRowData = null; try { if (data.facetRequested) { // get one row if we can if (data.facetCounts.size() - 1 < data.recordIndex) { setOutputDone(); return false; } FacetField.Count facetRecord = data.facetCounts.get(data.recordIndex); outputRowData = prepareFacetRecord(facetRecord); } else { // get one row if we can if (data.documentList.size() - 1 < data.recordIndex) { setOutputDone(); return false; } SolrDocument record = data.documentList.get(data.recordIndex); outputRowData = prepareRecord(record); } putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s); data.recordIndex++; return true; } catch (KettleException e) { boolean sendToErrorRow = false; String errorMessage = null; if (getStepMeta().isDoingErrorHandling()) { sendToErrorRow = true; errorMessage = e.toString(); } else { logError(BaseMessages.getString(PKG, "SolrInput.log.Exception", e.getMessage())); logError(Const.getStackTracker(e)); setErrors(1); stopAll(); setOutputDone(); // signal end to receiver(s) return false; } if (sendToErrorRow) { // Simply add this row to the error row putError(getInputRowMeta(), outputRowData, 1, errorMessage, null, "SolrInput001"); } } return true; }
From source file:org.pentaho.di.ui.trans.steps.solrinput.SolrInputDialog.java
License:Apache License
private void getFields() { try {//w w w . j ava 2 s. c om SolrInputMeta meta = new SolrInputMeta(); getInfo(meta); // clear the current fields grid wFields.removeAll(); // get real values boolean tryCursor = true; boolean facetRequested = false; Integer startRecord = 0; Integer chunkRowSize = 20; String realURL = transMeta.environmentSubstitute(meta.getURL()); String realQ = transMeta.environmentSubstitute(meta.getQ()); String realSort = transMeta.environmentSubstitute(meta.getSort()); String realCursor = transMeta.environmentSubstitute(meta.getCursor()); String realFq = transMeta.environmentSubstitute(meta.getFq()); String realFl = transMeta.environmentSubstitute(meta.getFl()); String realFacetQuery = transMeta.environmentSubstitute(meta.getFacetQuery()); String realFacetField = transMeta.environmentSubstitute(meta.getFacetField()); /* Send and Get the report */ SolrQuery query = new SolrQuery(); query.set("rows", chunkRowSize); if (realQ != null && !realQ.equals("")) { query.set("q", realQ); } if (realSort != null && !realSort.equals("")) { query.set("sort", realSort); } else { tryCursor = false; } if (realCursor != null && !realCursor.equals("")) { if (realCursor.equals("No")) { tryCursor = false; } } if (realFl != null && !realFl.equals("")) { query.set("fl", realFl); } if (realFq != null && !realFq.equals("")) { query.set("fq", realFq); } if (realFacetField != null && !realFacetField.equals("")) { //TODO incorporate multiple facet fields at once //String[] facetFields = realFacetField.split("\\s*,\\s*"); //for(int i =0; i < facetFields.length; i++){ query.addFacetField(realFacetField); //} query.setFacet(true); query.setFacetLimit(-1); query.setFacetMinCount(0); query.setFacetSort("count"); query.set("rows", 0); tryCursor = false; facetRequested = true; } if (realFacetQuery != null && !realFacetQuery.equals("")) { query.addFacetQuery(realFacetQuery); } // You can't use "TimeAllowed" with "CursorMark" // The documentation says "Values <= 0 mean // no time restriction", so setting to 0. query.setTimeAllowed(0); HttpSolrServer solr = new HttpSolrServer(realURL); String cursorMark = CursorMarkParams.CURSOR_MARK_START; boolean done = false; List<String> headerNames = new ArrayList<String>(); QueryResponse rsp = null; while (!done) { if (tryCursor) { query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark); } else { query.setStart(startRecord); } try { rsp = solr.query(query); } catch (SolrServerException e) { e.printStackTrace(); } if (facetRequested) { headerNames.add(rsp.getFacetFields().get(0).getName()); headerNames.add("count"); done = true; } else { SolrDocumentList docs = rsp.getResults(); for (SolrDocument doc : docs) { Collection<String> thisNamesArray = doc.getFieldNames(); String[] a = thisNamesArray.toArray(new String[thisNamesArray.size()]); for (int j = 0; j < a.length; j++) { if (!headerNames.contains(a[j])) { headerNames.add(a[j]); } } } } if (tryCursor) { String nextCursorMark = rsp.getNextCursorMark(); if (cursorMark.equals(nextCursorMark)) { done = true; } else { cursorMark = nextCursorMark; } } else { startRecord = startRecord + chunkRowSize; if (startRecord >= rsp.getResults().getNumFound()) { done = true; } } } getTableView().table.setItemCount(headerNames.size()); for (int j = 0; j < headerNames.size(); j++) { TableItem item = getTableView().table.getItem(j); item.setText(1, headerNames.get(j)); } wFields.removeEmptyRows(); wFields.setRowNums(); wFields.optWidth(true); getInput().setChanged(); } catch (Exception e) { new ErrorDialog(shell, BaseMessages.getString(PKG, "SolrInputMeta.ErrorRetrieveData.DialogTitle"), BaseMessages.getString(PKG, "SolrInputMeta.ErrorRetrieveData.DialogMessage"), e); } }