List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
From source file:org.dspace.app.webui.cris.servlet.ProfileResearcherNetworkServlet.java
public Map<String, Integer> getRelations(String from, String to) throws SearchServiceException { Map<String, Integer> result = new HashMap<String, Integer>(); SolrQuery solrQuery = new SolrQuery(); Matcher matcher = patternRP.matcher(from); String field1 = ""; String field2 = ""; if (matcher.find()) { field1 = "focus_auth"; } else {//ww w . j a va 2s. co m field1 = "focus_val"; } matcher = patternRP.matcher(to); if (matcher.find()) { field2 = "focus_auth"; } else { field2 = "focus_val"; } solrQuery.setQuery( field1 + ":\"" + from + "\" AND " + field2 + ":\"" + to + "\"" ); solrQuery.setFacet(true); solrQuery.addFacetField("type"); solrQuery.setFacetLimit(Integer.MAX_VALUE); solrQuery.setFacetMinCount(1); solrQuery.setRows(0); QueryResponse rsp = service.search(solrQuery); FacetField facets = rsp.getFacetField("type"); for (Count facet : facets.getValues()) { result.put(facet.getName(), Integer.valueOf((int) facet.getCount())); } return result; }
From source file:org.dspace.app.webui.cris.servlet.RelationsResearcherNetworkServlet.java
public MultiValueMap getRelationsInformation(String type, String from, String to) throws SearchServiceException { MultiValueMap result = new MultiValueMap(); SolrQuery solrQuery = new SolrQuery(); Matcher matcher = patternRP.matcher(from); String field1 = ""; String field2 = ""; if (matcher.find()) { field1 = "focus_auth"; } else {//from w w w .j ava 2 s . co m field1 = "focus_val"; } matcher = patternRP.matcher(to); if (matcher.find()) { field2 = "focus_auth"; } else { field2 = "focus_val"; } solrQuery.setQuery(field1 + ":\"" + from + "\" AND " + field2 + ":\"" + to + "\""); solrQuery.addFilterQuery("type:" + type); solrQuery.setRows(Integer.MAX_VALUE); QueryResponse rsp = service.search(solrQuery); for (SolrDocument doc : rsp.getResults()) { String resultField = ""; if (doc.getFieldValue("value") instanceof String) { resultField = (String) doc.getFieldValue("value"); } else { for (String ss : (List<String>) doc.getFieldValue("value")) { resultField += ss; } } String resultFieldExtra = ""; if (doc.getFieldValue("extra") != null) { if (doc.getFieldValue("extra") instanceof String) { resultFieldExtra = (String) doc.getFieldValue("extra"); } else { for (String ss : (List<String>) doc.getFieldValue("extra")) { resultFieldExtra += ss; } } } result.put(resultField, resultFieldExtra); } return result; }
From source file:org.dspace.app.webui.cris.servlet.ResearcherNetworkServlet.java
private boolean checkAvailableData(HttpServletRequest request, String connection, String authority) throws SearchServiceException { String query = "type:" + connection + " AND focus_auth:" + authority + " AND entity:" + ConstantNetwork.ENTITY_RP; SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query); solrQuery.setStart(0);/* w ww . ja v a 2s .co m*/ solrQuery.setRows(0); QueryResponse rsp = getService().search(solrQuery); SolrDocumentList docs = rsp.getResults(); if (docs != null) { if (docs.getNumFound() > 0) { return true; } } return false; }
From source file:org.dspace.app.webui.cris.util.RelationPreferenceUtil.java
public RelatedObjects getRelatedObject(Context context, ACrisObject cris, String relationType, String userQuery, String status, List<Sort> sorts, int rpp, int offset) throws SearchServiceException, SQLException { String uuid = cris.getUuid(); String configurationName = preferenceService.getConfigurationName(cris, relationType); RelationPreferenceConfiguration configuration = preferenceService.getConfigurationService() .getRelationPreferenceConfiguration(configurationName); List<ColumnVisualizationConfiguration> columns = configuration.getColumnsVisualizationConfiguration(); RelatedObjects result = new RelatedObjects(); List<RelatedObject> related = result.getObjects(); String query = MessageFormat.format(configuration.getRelationConfiguration().getQuery(), cris.getCrisID(), cris.getUuid());//from ww w. j a va2 s. c o m boolean sysAdmin = AuthorizeManager.isAdmin(context); SolrQuery solrQuery = new SolrQuery(); if (StringUtils.isNotEmpty(userQuery)) { solrQuery.addFilterQuery("{!tag=user}" + ClientUtils.escapeQueryChars(userQuery) + "*"); solrQuery.addFacetQuery("{!ex=user}*:*"); } if (!configuration.isActionEnabled(RelationPreference.HIDED, sysAdmin)) { solrQuery.addFilterQuery(getHiddenFilter(cris, configurationName)); } if (!configuration.isActionEnabled(RelationPreference.SELECTED, sysAdmin)) { solrQuery.addFilterQuery(getSelectedFilter(cris, configurationName)); } if (configuration.isActionEnabled(RelationPreference.UNLINKED, sysAdmin)) { query = getUnlinkedFilter(cris, configurationName, query); } solrQuery.setQuery(query); if (StringUtils.isNotEmpty(status)) { solrQuery.addFilterQuery(RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + status.toLowerCase() + ":\"" + cris.getUuid() + "\""); } if (sorts != null) { for (Sort s : sorts) { if (s.col < 0) { solrQuery.addSortField("cris-uuid", s.asc ? ORDER.asc : ORDER.desc); solrQuery.addSortField("search.resourceid", s.asc ? ORDER.asc : ORDER.desc); } else { solrQuery.addSortField(columns.get(s.col).getSortField(), s.asc ? ORDER.asc : ORDER.desc); } } } solrQuery.setRows(rpp); solrQuery.setStart(offset); solrQuery.setFields("search.resourceid", "cris-uuid", RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.UNLINKED, RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.HIDED, RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.SELECTED); QueryResponse qRes = searcher.search(solrQuery); result.setFilterRecords((int) qRes.getResults().getNumFound()); if (StringUtils.isNotEmpty(userQuery)) { result.setTotalRecords(qRes.getFacetQuery().get("{!ex=user}*:*")); } else { result.setTotalRecords((int) qRes.getResults().getNumFound()); } List<Object[]> dsoList = new ArrayList<Object[]>(); for (SolrDocument doc : qRes.getResults()) { List<String> unlinked = (List<String>) doc.getFieldValue(RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.UNLINKED); List<String> hided = (List<String>) doc.getFieldValue(RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.HIDED); List<String> selected = (List<String>) doc.getFieldValue(RelationPreference.PREFIX_RELATIONPREFERENCES + configurationName + "." + RelationPreference.SELECTED); String relStatus = null; if (unlinked != null && unlinked.contains(uuid)) { relStatus = RelationPreference.UNLINKED; } else if (hided != null && hided.contains(uuid)) { relStatus = RelationPreference.HIDED; } else if (selected != null && selected.contains(uuid)) { relStatus = RelationPreference.SELECTED; } if (doc.getFieldValue("cris-uuid") == null) { try { dsoList.add(new Object[] { Item.find(context, (Integer) doc.getFieldValue("search.resourceid")), relStatus }); } catch (SQLException e) { throw new RuntimeException(e.getMessage(), e); } } else { dsoList.add(new Object[] { applicationService.getEntityByUUID((String) doc.getFieldValue("cris-uuid")), relStatus }); } } for (Object[] dso : dsoList) { related.add(convert(context, (DSpaceObject) dso[0], configuration, (String) dso[1])); } return result; }
From source file:org.dspace.app.xmlui.aspect.discovery.BrowseFacet.java
License:Open Source License
/** * Retrieves the lowest date value in the given field * @param query a solr query//from w w w.ja va 2 s.co m * @param dateField the field for which we want to retrieve our date * @param filterquery the filterqueries * @return the lowest date found, in a date object */ private Date getLowestDateValue(String query, String dateField, String... filterquery) { try { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query); solrQuery.setFields(dateField); solrQuery.setRows(1); solrQuery.setSortField(dateField, SolrQuery.ORDER.asc); solrQuery.setFilterQueries(filterquery); QueryResponse rsp = searchService.search(solrQuery); if (0 < rsp.getResults().getNumFound()) { return (Date) rsp.getResults().get(0).getFieldValue(dateField); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.dspace.authority.AuthoritySolrServiceImpl.java
License:BSD License
/** * Retrieves all the metadata fields which are indexed in the authority control * @return a list of metadata fields//from www. ja v a 2 s. c om */ public List<String> getAllIndexedMetadataFields() throws Exception { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); solrQuery.setFacet(true); solrQuery.addFacetField("field"); QueryResponse response = getSolr().query(solrQuery); List<String> results = new ArrayList<String>(); FacetField facetField = response.getFacetField("field"); if (facetField != null) { List<FacetField.Count> values = facetField.getValues(); if (values != null) { for (FacetField.Count facetValue : values) { if (facetValue != null && facetValue.getName() != null) { results.add(facetValue.getName()); } } } } return results; }
From source file:org.dspace.authority.AuthorityValueFinder.java
License:BSD License
private List<AuthorityValue> find(Context context, String queryString) { List<AuthorityValue> findings = new ArrayList<AuthorityValue>(); try {/*from w ww . j a va 2s.c om*/ SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(filtered(queryString)); log.debug("AuthorityValueFinder makes the query: " + queryString); QueryResponse queryResponse = SolrAuthority.getSearchService().search(solrQuery); if (queryResponse != null && queryResponse.getResults() != null && 0 < queryResponse.getResults().getNumFound()) { for (SolrDocument document : queryResponse.getResults()) { AuthorityValue authorityValue = AuthorityValue.fromSolr(document); findings.add(authorityValue); log.debug("AuthorityValueFinder found: " + authorityValue.getValue()); } } } catch (Exception e) { log.error(LogManager.getHeader(context, "Error while retrieving AuthorityValue from solr", "query: " + queryString), e); } return findings; }
From source file:org.dspace.authority.AuthorityValueServiceImpl.java
License:BSD License
protected List<AuthorityValue> find(Context context, String queryString) { List<AuthorityValue> findings = new ArrayList<AuthorityValue>(); try {//from w w w . j a v a2 s .c o m SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(filtered(queryString)); log.debug("AuthorityValueFinder makes the query: " + queryString); QueryResponse queryResponse = SolrAuthority.getSearchService().search(solrQuery); if (queryResponse != null && queryResponse.getResults() != null && 0 < queryResponse.getResults().getNumFound()) { for (SolrDocument document : queryResponse.getResults()) { AuthorityValue authorityValue = fromSolr(document); findings.add(authorityValue); log.debug("AuthorityValueFinder found: " + authorityValue.getValue()); } } } catch (Exception e) { log.error(LogManager.getHeader(context, "Error while retrieving AuthorityValue from solr", "query: " + queryString), e); } return findings; }
From source file:org.dspace.authority.SolrAuthority.java
License:BSD License
public Choices getMatches(String field, String text, int collection, int start, int limit, String locale, boolean bestMatch) { if (limit == 0) limit = 10;/* w w w.j a va 2 s .c o m*/ SolrQuery queryArgs = new SolrQuery(); if (text == null || text.trim().equals("")) { queryArgs.setQuery("*:*"); } else { String searchField = "value"; String localSearchField = ""; try { //A downside of the authors is that the locale is sometimes a number, make sure that this isn't one Integer.parseInt(locale); locale = null; } catch (NumberFormatException e) { //Everything is allright } if (locale != null && !"".equals(locale)) { localSearchField = searchField + "_" + locale; } String query = "(" + toQuery(searchField, text) + ") "; if (!localSearchField.equals("")) { query += " or (" + toQuery(localSearchField, text) + ")"; } queryArgs.setQuery(query); } queryArgs.addFilterQuery("field:" + field); queryArgs.set(CommonParams.START, start); //We add one to our facet limit so that we know if there are more matches int maxNumberOfSolrResults = limit + 1; if (externalResults) { maxNumberOfSolrResults = ConfigurationManager.getIntProperty("xmlui.lookup.select.size", 12); } queryArgs.set(CommonParams.ROWS, maxNumberOfSolrResults); String sortField = "value"; String localSortField = ""; if (StringUtils.isNotBlank(locale)) { localSortField = sortField + "_" + locale; queryArgs.setSortField(localSortField, SolrQuery.ORDER.asc); } else { queryArgs.setSortField(sortField, SolrQuery.ORDER.asc); } Choices result; try { int max = 0; boolean hasMore = false; QueryResponse searchResponse = getSearchService().search(queryArgs); SolrDocumentList authDocs = searchResponse.getResults(); ArrayList<Choice> choices = new ArrayList<Choice>(); if (authDocs != null) { max = (int) searchResponse.getResults().getNumFound(); int maxDocs = authDocs.size(); if (limit < maxDocs) maxDocs = limit; List<AuthorityValue> alreadyPresent = new ArrayList<AuthorityValue>(); for (int i = 0; i < maxDocs; i++) { SolrDocument solrDocument = authDocs.get(i); if (solrDocument != null) { AuthorityValue val = AuthorityValue.fromSolr(solrDocument); Map<String, String> extras = val.choiceSelectMap(); extras.put("insolr", val.getId()); choices.add(new Choice(val.getId(), val.getValue(), val.getValue(), extras)); alreadyPresent.add(val); } } if (externalResults && StringUtils.isNotBlank(text)) { int sizeFromSolr = alreadyPresent.size(); int maxExternalResults = limit <= 10 ? Math.max(limit - sizeFromSolr, 2) : Math.max(limit - 10 - sizeFromSolr, 2) + limit - 10; addExternalResults(text, choices, alreadyPresent, maxExternalResults); } // hasMore = (authDocs.size() == (limit + 1)); hasMore = true; } int confidence; if (choices.size() == 0) confidence = Choices.CF_NOTFOUND; else if (choices.size() == 1) confidence = Choices.CF_UNCERTAIN; else confidence = Choices.CF_AMBIGUOUS; result = new Choices(choices.toArray(new Choice[choices.size()]), start, hasMore ? max : choices.size() + start, confidence, hasMore); } catch (Exception e) { log.error("Error while retrieving authority values {field: " + field + ", prefix:" + text + "}", e); result = new Choices(true); } return result; //To change body of implemented methods use File | Settings | File Templates. }
From source file:org.dspace.authority.SolrAuthority.java
License:BSD License
@Override public String getLabel(String field, String key, String locale) { try {//from w w w .j a va2 s .com if (log.isDebugEnabled()) { log.debug("requesting label for key " + key + " using locale " + locale); } SolrQuery queryArgs = new SolrQuery(); queryArgs.setQuery("id:" + key); queryArgs.setRows(1); QueryResponse searchResponse = getSearchService().search(queryArgs); SolrDocumentList docs = searchResponse.getResults(); if (docs.getNumFound() == 1) { String label = null; try { label = (String) docs.get(0).getFieldValue("value_" + locale); } catch (Exception e) { //ok to fail here } if (label != null) { if (log.isDebugEnabled()) { log.debug("returning label " + label + " for key " + key + " using locale " + locale + " and fieldvalue " + "value_" + locale); } return label; } try { label = (String) docs.get(0).getFieldValue("value"); } catch (Exception e) { log.error("couldn't get field value for key " + key, e); } if (label != null) { if (log.isDebugEnabled()) { log.debug("returning label " + label + " for key " + key + " using locale " + locale + " and fieldvalue " + "value"); } return label; } try { label = (String) docs.get(0).getFieldValue("value_en"); } catch (Exception e) { log.error("couldn't get field value for key " + key, e); } if (label != null) { if (log.isDebugEnabled()) { log.debug("returning label " + label + " for key " + key + " using locale " + locale + " and fieldvalue " + "value_en"); } return label; } } } catch (Exception e) { log.error("error occurred while trying to get label for key " + key, e); } return key; }