List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
From source file:org.codelibs.fess.helper.IndexingHelper.java
License:Apache License
public SolrDocumentList getSolrDocumentListByPrefixId(final SolrGroup solrGroup, final String id, final String[] fields) { final FieldHelper fieldHelper = ComponentUtil.getFieldHelper(); final SolrQuery solrQuery = new SolrQuery(); final StringBuilder queryBuf = new StringBuilder(500); queryBuf.append("{!prefix f=").append(fieldHelper.idField).append("}"); queryBuf.append(id);/*w w w.jav a2 s. c o m*/ solrQuery.setQuery(queryBuf.toString()); if (fields != null) { solrQuery.setFields(fields); } final QueryResponse response = solrGroup.query(solrQuery); final SolrDocumentList docList = response.getResults(); if (docList.isEmpty()) { return null; } if (logger.isDebugEnabled()) { logger.debug("Found solr documents: " + docList); } return docList; }
From source file:org.codelibs.fess.helper.IndexingHelper.java
License:Apache License
protected SolrDocumentList getChildSolrDocumentList(final SolrGroup solrGroup, final String id, final String[] fields, final int row) { final FieldHelper fieldHelper = ComponentUtil.getFieldHelper(); final SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("{!raw f=" + fieldHelper.parentIdField + " v=\"" + id + "\"}"); if (fields != null) { solrQuery.setFields(fields);//w w w . ja va 2 s . co m } solrQuery.setRows(row); final SolrDocumentList docList = solrGroup.query(solrQuery).getResults(); if (docList.getNumFound() <= row) { return docList; } return getChildSolrDocumentList(solrGroup, id, fields, (int) docList.getNumFound()); }
From source file:org.codelibs.fess.service.SearchService.java
License:Apache License
public List<Map<String, Object>> getDocumentList(final String query, final int start, final int rows, final FacetInfo facetInfo, final GeoInfo geoInfo, final MoreLikeThisInfo mltInfo, final String[] responseFields, final String[] docValuesFields, final boolean forUser) { if (start > queryHelper.getMaxSearchResultOffset()) { throw new ResultOffsetExceededException("The number of result size is exceeded."); }//from w w w . j a v a2 s . co m final long startTime = System.currentTimeMillis(); final SolrGroup solrGroup = solrGroupManager.getSolrGroup(QueryType.QUERY); QueryResponse queryResponse = null; final SolrQuery solrQuery = new SolrQuery(); final SearchQuery searchQuery = queryHelper.build(query, forUser); final String q = searchQuery.getQuery(); if (StringUtil.isNotBlank(q)) { // fields solrQuery.setFields(responseFields); // query solrQuery.setQuery(q); solrQuery.setStart(start); solrQuery.setRows(rows); solrQuery.set("mm", searchQuery.getMinimumShouldMatch()); solrQuery.set("defType", searchQuery.getDefType()); for (final Map.Entry<String, String[]> entry : queryHelper.getQueryParamMap().entrySet()) { solrQuery.set(entry.getKey(), entry.getValue()); } // filter query if (searchQuery.hasFilterQueries()) { solrQuery.addFilterQuery(searchQuery.getFilterQueries()); } // sort final SortField[] sortFields = searchQuery.getSortFields(); if (sortFields.length != 0) { for (final SortField sortField : sortFields) { solrQuery.addSort(sortField.getField(), Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc : SolrQuery.ORDER.asc); } } else if (queryHelper.hasDefaultSortFields()) { for (final SortField sortField : queryHelper.getDefaultSortFields()) { solrQuery.addSort(sortField.getField(), Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc : SolrQuery.ORDER.asc); } } // highlighting if (queryHelper.getHighlightingFields() != null && queryHelper.getHighlightingFields().length != 0) { for (final String hf : queryHelper.getHighlightingFields()) { solrQuery.addHighlightField(hf); } solrQuery.setHighlightSnippets(queryHelper.getHighlightSnippetSize()); } // shards if (queryHelper.getShards() != null) { solrQuery.setParam("shards", queryHelper.getShards()); } // geo if (geoInfo != null && geoInfo.isAvailable()) { solrQuery.addFilterQuery(geoInfo.toGeoQueryString()); final String additionalGeoQuery = queryHelper.getAdditionalGeoQuery(); if (StringUtil.isNotBlank(additionalGeoQuery)) { solrQuery.addFilterQuery(additionalGeoQuery); } } // facets if (facetInfo != null) { solrQuery.setFacet(true); if (facetInfo.field != null) { for (final String f : facetInfo.field) { if (queryHelper.isFacetField(f)) { solrQuery.addFacetField(f); } else { throw new FessSolrQueryException("EFESS0002", new Object[] { f }); } } } if (facetInfo.query != null) { for (final String fq : facetInfo.query) { final String facetQuery = queryHelper.buildFacetQuery(fq); if (StringUtil.isNotBlank(facetQuery)) { solrQuery.addFacetQuery(facetQuery); } else { throw new FessSolrQueryException("EFESS0003", new Object[] { fq, facetQuery }); } } } if (facetInfo.limit != null) { solrQuery.setFacetLimit(Integer.parseInt(facetInfo.limit)); } if (facetInfo.minCount != null) { solrQuery.setFacetMinCount(Integer.parseInt(facetInfo.minCount)); } if (facetInfo.missing != null) { solrQuery.setFacetMissing(Boolean.parseBoolean(facetInfo.missing)); } if (facetInfo.prefix != null) { solrQuery.setFacetPrefix(facetInfo.prefix); } if (facetInfo.sort != null && queryHelper.isFacetSortValue(facetInfo.sort)) { solrQuery.setFacetSort(facetInfo.sort); } } // mlt if (mltInfo != null) { final String mltField = queryHelper.getMoreLikeThisField(mltInfo.field); if (mltField != null) { solrQuery.set("mlt", true); if (mltInfo.count != null) { solrQuery.set("mlt.count", Integer.parseInt(mltInfo.count)); } solrQuery.set("mlt.fl", mltField); } } if (queryHelper.getTimeAllowed() >= 0) { solrQuery.setTimeAllowed(queryHelper.getTimeAllowed()); } final Set<Entry<String, String[]>> paramSet = queryHelper.getRequestParameterSet(); if (!paramSet.isEmpty()) { for (final Map.Entry<String, String[]> entry : paramSet) { solrQuery.set(entry.getKey(), entry.getValue()); } } if (docValuesFields != null) { for (final String docValuesField : docValuesFields) { solrQuery.add(Constants.DCF, docValuesField); } } queryResponse = solrGroup.query(solrQuery, SolrRequest.METHOD.POST); } final long execTime = System.currentTimeMillis() - startTime; final QueryResponseList queryResponseList = ComponentUtil.getQueryResponseList(); queryResponseList.init(queryResponse, rows); queryResponseList.setSearchQuery(q); queryResponseList.setSolrQuery(solrQuery.toString()); queryResponseList.setExecTime(execTime); return queryResponseList; }
From source file:org.codelibs.fess.web.admin.DocumentAction.java
License:Apache License
protected SessionIdList<Map<String, String>> getSessionIdList(final String groupName) { final SessionIdList<Map<String, String>> sessionIdList = new SessionIdList<Map<String, String>>(); SolrGroup serverGroup;//w w w . j av a 2 s. c om try { serverGroup = solrGroupManager.getSolrGroup(groupName); } catch (final Exception e) { if (logger.isInfoEnabled()) { logger.info(e.getMessage()); } return sessionIdList; } final SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setFacet(true); query.addFacetField(fieldHelper.segmentField); query.addSort(fieldHelper.segmentField, ORDER.desc); final QueryResponse queryResponse = serverGroup.query(query); final List<FacetField> facets = queryResponse.getFacetFields(); for (final FacetField facet : facets) { final List<FacetField.Count> facetEntries = facet.getValues(); if (facetEntries != null) { for (final FacetField.Count fcount : facetEntries) { final Map<String, String> map = new HashMap<String, String>(3); map.put("label", fcount.getName() + " (" + fcount.getCount() + ")"); map.put("value", fcount.getName()); map.put("count", Long.toString(fcount.getCount())); sessionIdList.add(map); sessionIdList.addTotalCount(fcount.getCount()); } } } return sessionIdList; }
From source file:org.codice.solr.query.SolrQueryFilterVisitorTest.java
License:Open Source License
@Test @Ignore//from w w w . j a v a2s .c om public void test() throws Exception { LOGGER.info("Running test ..."); // setup String workingDir = System.getProperty("user.dir") + "/src/test/resources/"; String solrConfDir = workingDir + "solr/conf/"; File solrConfigFile = new File(solrConfDir + "solrconfig.xml"); //getConfigFile(solrConfigFileName, configProxy); assertTrue(solrConfigFile.exists()); File solrSchemaFile = new File(solrConfDir + "schema.xml"); //getConfigFile(schemaFileName, configProxy); assertTrue(solrSchemaFile.exists()); File solrFile = new File(solrConfDir + "solr.xml"); //getConfigFile(DEFAULT_SOLR_XML, configProxy); assertTrue(solrFile.exists()); File solrConfigHome = new File(solrConfigFile.getParent()); assertTrue(solrConfigHome.exists()); SolrConfig solrConfig = null; IndexSchema indexSchema = null; SolrResourceLoader resourceLoader = null; SolrCoreContainer container = null; try { // NamedSPILoader uses the thread context classloader to lookup // codecs, posting formats, and analyzers solrConfig = new SolrConfig(solrConfigHome.getParent(), "solrConfig.xml", new InputSource(FileUtils.openInputStream(solrConfigFile))); assertNotNull(solrConfig); indexSchema = new IndexSchema(solrConfig, "schema.xml", new InputSource(FileUtils.openInputStream(solrSchemaFile))); assertNotNull(indexSchema); resourceLoader = new SolrResourceLoader(solrConfigHome.getAbsolutePath()); assertNotNull(resourceLoader); container = new SolrCoreContainer(resourceLoader, solrFile); assertNotNull(container); CoreDescriptor coreDescriptor = new CoreDescriptor(container, CORE_NAME, solrConfig.getResourceLoader().getInstanceDir()); assertNotNull(coreDescriptor); File dataDir = new File(workingDir + "data"); //configProxy.getDataDirectory(); LOGGER.debug("Using data directory [{}]", dataDir); SolrCore core = new SolrCore(CORE_NAME, dataDir.getAbsolutePath(), solrConfig, indexSchema, coreDescriptor); container.register(CORE_NAME, core, false); assertNotNull(core); EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, CORE_NAME); // the test SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrServer, CORE_NAME); Filter filter = ECQL.toFilter("Name = 'Hugh'"); SolrQuery solrQuery = (SolrQuery) filter.accept(visitor, null); assertNotNull(solrQuery); // Solr does not support outside parenthesis in certain queries and throws EOF exception. String queryPhrase = solrQuery.getQuery().trim(); if (queryPhrase.matches("\\(\\s*\\{!.*\\)")) { solrQuery.setQuery(queryPhrase.replaceAll("^\\(\\s*|\\s*\\)$", "")); } LOGGER.info("solrQuery = {}", solrQuery); QueryResponse solrResponse = solrServer.query(solrQuery, METHOD.POST); assertNotNull(solrResponse); long numResults = solrResponse.getResults().getNumFound(); LOGGER.info("numResults = {}", numResults); } catch (ParserConfigurationException e) { LOGGER.warn("Parser configuration exception loading index schema", e); } catch (IOException e) { LOGGER.warn("IO exception loading index schema", e); } catch (SAXException e) { LOGGER.warn("SAX exception loading index schema", e); } }
From source file:org.dataone.proto.trove.mn.dao.v1.SolrLogIndexer.java
License:Apache License
/** * Pull solr query field parameters from a Properties file and use them to issue a solr query. * * The solr query results are transformed into a Log object and returned. * * The following fields are currently supported and passed through to the solr log index as a query * * fromDate (Types.DateTime) Records with time stamp greater than or equal to (>=) this value will be returned. * Transmitted as a URL query parameter, and so must be escaped accordingly. toDate (Types.DateTime) Records with * a time stamp less than this value will be returned. If not specified, then defaults to now. Transmitted as a URL * query parameter, and so must be escaped accordingly. event (Types.Event) Return only log records for the * specified type of event. Default is all. Transmitted as a URL query parameter, and so must be escaped * accordingly. pidFilter (string) Return only log records for identifiers that start with the supplied identifier * string. Support for this parameter is optional and MAY be ignored by the Member Node implementation with no * warning. Transmitted as a URL query parameter, and so must be escaped accordingly. start=0 (integer) Optional * zero based offset from the first record in the set of matching log records. Used to assist with paging the * response. Transmitted as a URL query parameter, and so must be escaped accordingly. count=1000 (integer) The * maximum number of log records that should be returned in the response. The Member Node may return fewer and the * caller should check the total in the response to determine if further pages may be retrieved. Transmitted as a * URL query parameter, and so must be escaped accordingly. * * * * * @param query/* w w w .java 2 s .c o m*/ * @return Log * @throws org.apache.commons.configuration.ConfigurationException * @throws java.io.IOException * @throws org.apache.solr.client.solrj.SolrServerException */ @Override public Log search(Properties query) throws ConfigurationException, IOException, SolrServerException { // since this is the implementation assume the input to be a properties file. Configuration apacheConfiguration = getApacheConfiguration(query); List<LogEntrySolrItem> outputList = new ArrayList(); // may not have to initialize Log log = new Log(); SolrQuery queryParams = new SolrQuery(); queryParams.setQuery(queryStringBuilder(apacheConfiguration)); queryParams.setSortField("dateAggregated", SolrQuery.ORDER.desc); if (apacheConfiguration.containsKey("start")) { queryParams.setStart(apacheConfiguration.getInt("start")); } else { queryParams.setStart(0); } if (apacheConfiguration.containsKey("count")) { queryParams.setRows(apacheConfiguration.getInt("count")); } else { queryParams.setRows(defaultCount); } QueryResponse queryResponse = solrServer.query(queryParams); queryResponse.getResults(); List<LogEntrySolrItem> logEntryList = queryResponse.getBeans(LogEntrySolrItem.class); for (LogEntrySolrItem lesi : logEntryList) { log.addLogEntry(lesi.getLogEntry()); } log.setTotal(Long.valueOf(queryResponse.getResults().getNumFound()).intValue()); log.setStart(Long.valueOf(queryResponse.getResults().getStart()).intValue()); log.setCount(logEntryList.size()); return log; }
From source file:org.dataone.proto.trove.mn.dao.v1.SolrQueryIndexer.java
License:Apache License
@Override public ObjectList search(Properties query) throws Exception { // since this is the implementation assume the input to be a properties file. Configuration apacheConfiguration = getApacheConfiguration(query); ObjectList objectList = new ObjectList(); SolrQuery queryParams = new SolrQuery(); queryParams.setQuery(queryStringBuilder(apacheConfiguration)); queryParams.setSortField("id", SolrQuery.ORDER.desc); if (apacheConfiguration.containsKey("start")) { queryParams.setStart(apacheConfiguration.getInt("start")); } else {//from w w w. java 2s . c o m queryParams.setStart(0); } if (apacheConfiguration.containsKey("count")) { queryParams.setRows(apacheConfiguration.getInt("count")); } else { queryParams.setRows(defaultCount); } QueryResponse queryResponse = solrServer.query(queryParams); queryResponse.getResults(); List<ObjectInfoSolrItem> objectInfoList = queryResponse.getBeans(ObjectInfoSolrItem.class); for (ObjectInfoSolrItem lesi : objectInfoList) { objectList.addObjectInfo(lesi.getObjectInfo()); } objectList.setTotal(Long.valueOf(queryResponse.getResults().getNumFound()).intValue()); objectList.setStart(Long.valueOf(queryResponse.getResults().getStart()).intValue()); objectList.setCount(objectInfoList.size()); return objectList; }
From source file:org.dbflute.solr.cbean.AbstractSolrConditionBean.java
License:Apache License
@Override public SolrQuery buildSolrQuery() { SolrQuery query = new SolrQuery(); if (this.isSpecified()) { query.setFields(this.getSpecifyFields()); } else {// w w w. j av a 2 s. co m query.setFields(this.getAllFields()); } if (this.isUseFilterQuery()) { query.setQuery(this.getMinimumQuery()); query.setFilterQueries(this.getQueryArray()); } else { query.setQuery(this.getQueryString()); String[] filterQueryArray = this.getFilterQueryArray(); if (filterQueryArray != null && filterQueryArray.length != 0) { query.setFilterQueries(this.getFilterQueryArray()); } } if (this.isUseSort()) { for (SortClause sortClause : this.getSolrSortClauseList()) { query.addSort(sortClause); } } for (SolrQueryBean queryBean : this.getFacetQueryList()) { query.addFacetQuery(queryBean.getQueryString()); } SolrSpecification facetSpecifyBean = getFacetSpecification(); if (facetSpecifyBean.getSpecifyFields().length > 0) { query.addFacetField(facetSpecifyBean.getSpecifyFields()); } query.setStart(this.getPageStart()); query.setRows(this.getPageSize()); return query; }
From source file:org.dspace.app.cris.batch.ScriptAddPMCDataToRP.java
/** * Batch script to aggregate PMC data to RPs. See the technical * documentation for further details./* w w w .ja va 2s .c o m*/ * * @throws SearchServiceException */ public static void main(String[] args) throws ParseException, SQLException, SearchServiceException { log.info("#### START AddPMCDataToRP: -----" + new Date() + " ----- ####"); DSpace dspace = new DSpace(); ApplicationService applicationService = dspace.getServiceManager().getServiceByName("applicationService", ApplicationService.class); CrisSearchService searchService = dspace.getServiceManager() .getServiceByName(CrisSearchService.class.getName(), CrisSearchService.class); PMCPersistenceService pmcService = dspace.getServiceManager() .getServiceByName(PMCPersistenceService.class.getName(), PMCPersistenceService.class); List<ResearcherPage> rs = applicationService.getList(ResearcherPage.class); for (ResearcherPage rp : rs) { boolean updated = false; int itemsCited = 0; int citations = 0; SolrQuery query = new SolrQuery(); query.setQuery("dc.identifier.pmid:[* TO *]"); query.addFilterQuery("{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp), "NOT(withdrawn:true)"); query.setFields("dc.identifier.pmid"); query.setRows(Integer.MAX_VALUE); QueryResponse response = searchService.search(query); SolrDocumentList results = response.getResults(); for (SolrDocument doc : results) { Integer pmid = null; try { pmid = Integer.valueOf((String) doc.getFirstValue("dc.identifier.pmid")); } catch (NumberFormatException e) { log.warn("Found invalid pmid: " + doc.getFieldValue("dc.identifier.pmid") + " for rp: " + ResearcherPageUtils.getPersistentIdentifier(rp)); } if (pmid != null) { PMCCitation pmccitation = pmcService.get(PMCCitation.class, pmid); if (pmccitation != null && pmccitation.getNumCitations() > 0) { itemsCited++; citations += pmccitation.getNumCitations(); } } } updated = setValue(applicationService, rp, itemsCitedTP, String.valueOf(itemsCited)); // caution don't use the short-circuit OR operator (i.e || otherwise // only the first found pmcdata value will be recorded!) updated = updated | setValue(applicationService, rp, citationsTP, String.valueOf(citations)); updated = updated | setValue(applicationService, rp, itemsInPubmedTP, String.valueOf(results.getNumFound())); if (StringUtils.isNotEmpty(itemsInPMCTP)) { query = new SolrQuery(); query.setQuery("dc.identifier.pmcid:[* TO *]"); query.addFilterQuery( "{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp), "NOT(withdrawn:true)"); query.setRows(0); response = searchService.search(query); results = response.getResults(); // caution don't use the short-circuit OR operator (i.e || otherwise // only the first found pmcdata value will be recorded!) updated = updated | setValue(applicationService, rp, itemsInPMCTP, String.valueOf(results.getNumFound())); } if (updated) { applicationService.saveOrUpdate(ResearcherPage.class, rp); } } log.info("#### END AddPMCDataToRP: -----" + new Date() + " ----- ####"); }
From source file:org.dspace.app.cris.batch.ScriptCrisSubscribe.java
License:Open Source License
/** * Sends an email to the given e-person with details of new items in the * given dspace object (MUST be a community or a collection), items that * appeared yesterday. No e-mail is sent if there aren't any new items in * any of the dspace objects.//from w ww . j ava 2 s . com * * @param context * DSpace context object * @param eperson * eperson to send to * @param rpkeys * List of DSpace Objects * @param test * @throws SearchServiceException */ public static void sendEmail(Researcher researcher, Context context, EPerson eperson, List<String> rpkeys, boolean test, List<String> relationFields) throws IOException, MessagingException, SQLException, SearchServiceException { CrisSearchService searchService = researcher.getCrisSearchService(); // Get a resource bundle according to the eperson language preferences Locale supportedLocale = I18nUtil.getEPersonLocale(eperson); StringBuffer emailText = new StringBuffer(); boolean isFirst = true; for (String rpkey : rpkeys) { SolrQuery query = new SolrQuery(); query.setFields("search.resourceid"); query.addFilterQuery("{!field f=search.resourcetype}" + Constants.ITEM, "{!field f=inarchive}true"); for (String tmpRelations : relationFields) { String fq = "{!field f=" + tmpRelations + "}" + rpkey; query.addFilterQuery(fq); } query.setRows(Integer.MAX_VALUE); if (ConfigurationManager.getBooleanProperty("eperson.subscription.onlynew", false)) { // get only the items archived yesterday query.setQuery("dateaccessioned:(NOW/DAY-1DAY)"); } else { // get all item modified yesterday but not published the day // before // and all the item modified today and archived yesterday query.setQuery( "(item.lastmodified:(NOW/DAY-1DAY) AND dateaccessioned:(NOW/DAY-1DAY)) OR ((item.lastmodified:(NOW/DAY) AND dateaccessioned:(NOW/DAY-1DAY)))"); } QueryResponse qResponse = searchService.search(query); SolrDocumentList results = qResponse.getResults(); // Only add to buffer if there are new items if (results.getNumFound() > 0) { if (!isFirst) { emailText.append("\n---------------------------------------\n"); } else { isFirst = false; } emailText.append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.new-items", supportedLocale)) .append(" ").append(rpkey).append(": ").append(results.getNumFound()).append("\n\n"); for (SolrDocument solrDoc : results) { Item item = Item.find(context, (Integer) solrDoc.getFieldValue("search.resourceid")); DCValue[] titles = item.getDC("title", null, Item.ANY); emailText.append(" ") .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.title", supportedLocale)) .append(" "); if (titles.length > 0) { emailText.append(titles[0].value); } else { emailText.append( I18nUtil.getMessage("org.dspace.eperson.Subscribe.untitled", supportedLocale)); } DCValue[] authors = item.getDC("contributor", Item.ANY, Item.ANY); if (authors.length > 0) { emailText.append("\n ").append( I18nUtil.getMessage("org.dspace.eperson.Subscribe.authors", supportedLocale)) .append(" ").append(authors[0].value); for (int k = 1; k < authors.length; k++) { emailText.append("\n ").append(authors[k].value); } } emailText.append("\n ") .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.id", supportedLocale)) .append(" ").append(HandleManager.getCanonicalForm(item.getHandle())).append("\n\n"); context.removeCached(item, item.getID()); } } } // Send an e-mail if there were any new items if (emailText.length() > 0) { if (test) { log.info(LogManager.getHeader(context, "subscription:", "eperson=" + eperson.getEmail())); log.info(LogManager.getHeader(context, "subscription:", "text=" + emailText.toString())); } else { Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscription")); email.addRecipient(eperson.getEmail()); email.addArgument(emailText.toString()); email.send(); log.info(LogManager.getHeader(context, "sent_subscription", "eperson_id=" + eperson.getID())); } } }