List of usage examples for org.apache.solr.client.solrj SolrQuery getRows
public Integer getRows()
From source file:nl.knaw.huygens.timbuctoo.index.solr.SolrQueryMatcher.java
License:Open Source License
public SolrQueryMatcher withRows(int rows) { this.addMatcher(new PropertyEqualityMatcher<SolrQuery, Integer>("rows", rows) { @Override//w ww .j a v a2s . c o m protected Integer getItemValue(SolrQuery item) { return item.getRows(); } }); return this; }
From source file:org.apache.metron.solr.matcher.SolrQueryMatcher.java
License:Apache License
@Override public boolean matches(Object o) { SolrQuery solrQuery = (SolrQuery) o; return Objects.equals(solrQuery.getStart(), expectedSolrQuery.getStart()) && Objects.equals(solrQuery.getRows(), expectedSolrQuery.getRows()) && Objects.equals(solrQuery.getQuery(), expectedSolrQuery.getQuery()) && Objects.equals(solrQuery.getSorts(), expectedSolrQuery.getSorts()) && Objects.equals(solrQuery.getFields(), expectedSolrQuery.getFields()) && Arrays.equals(solrQuery.getFacetFields(), expectedSolrQuery.getFacetFields()) && Objects.equals(solrQuery.get("collection"), expectedSolrQuery.get("collection")) && Objects.equals(solrQuery.get("stats"), expectedSolrQuery.get("stats")) && Objects.equals(solrQuery.get("stats.field"), expectedSolrQuery.get("stats.field")) && Objects.equals(solrQuery.get("facet"), expectedSolrQuery.get("facet")) && Objects.equals(solrQuery.get("facet.pivot"), expectedSolrQuery.get("facet.pivot")); }
From source file:org.apache.nifi.processors.solr.QuerySolr.java
License:Apache License
@Override public void doOnTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); FlowFile flowFileOriginal = session.get(); FlowFile flowFileResponse;// w ww. ja v a2s. c o m if (flowFileOriginal == null) { if (context.hasNonLoopConnection()) { return; } flowFileResponse = session.create(); } else { flowFileResponse = session.create(flowFileOriginal); } final SolrQuery solrQuery = new SolrQuery(); final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue()); final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFileResponse) .getValue(); final StringBuilder transitUri = new StringBuilder("solr://"); transitUri.append(getSolrLocation()); if (isSolrCloud) { transitUri.append(":").append(collection); } final StopWatch timer = new StopWatch(false); try { solrQuery.setQuery(context.getProperty(SOLR_PARAM_QUERY).evaluateAttributeExpressions(flowFileResponse) .getValue()); solrQuery.setRequestHandler(context.getProperty(SOLR_PARAM_REQUEST_HANDLER) .evaluateAttributeExpressions(flowFileResponse).getValue()); if (context.getProperty(SOLR_PARAM_FIELD_LIST).isSet()) { for (final String field : context.getProperty(SOLR_PARAM_FIELD_LIST) .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) { solrQuery.addField(field.trim()); } } // Avoid ArrayIndexOutOfBoundsException due to incorrectly configured sorting try { if (context.getProperty(SOLR_PARAM_SORT).isSet()) { final List<SolrQuery.SortClause> sortings = new ArrayList<>(); for (final String sorting : context.getProperty(SOLR_PARAM_SORT) .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) { final String[] sortEntry = sorting.trim().split(" "); sortings.add(new SolrQuery.SortClause(sortEntry[0], sortEntry[1])); } solrQuery.setSorts(sortings); } } catch (Exception e) { throw new ProcessException("Error while parsing the sort clauses for the Solr query"); } final Integer startParam = context.getProperty(SOLR_PARAM_START).isSet() ? Integer.parseInt(context.getProperty(SOLR_PARAM_START) .evaluateAttributeExpressions(flowFileResponse).getValue()) : CommonParams.START_DEFAULT; solrQuery.setStart(startParam); final Integer rowParam = context.getProperty(SOLR_PARAM_ROWS).isSet() ? Integer.parseInt(context.getProperty(SOLR_PARAM_ROWS) .evaluateAttributeExpressions(flowFileResponse).getValue()) : CommonParams.ROWS_DEFAULT; solrQuery.setRows(rowParam); final Map<String, String[]> additionalSolrParams = SolrUtils.getRequestParams(context, flowFileResponse); final Set<String> searchComponents = extractSearchComponents(additionalSolrParams); solrQuery.add(new MultiMapSolrParams(additionalSolrParams)); final Map<String, String> attributes = new HashMap<>(); attributes.put(ATTRIBUTE_SOLR_CONNECT, getSolrLocation()); if (isSolrCloud) { attributes.put(ATTRIBUTE_SOLR_COLLECTION, collection); } attributes.put(ATTRIBUTE_SOLR_QUERY, solrQuery.toString()); if (flowFileOriginal != null) { flowFileOriginal = session.putAllAttributes(flowFileOriginal, attributes); } flowFileResponse = session.putAllAttributes(flowFileResponse, attributes); final boolean getEntireResults = RETURN_ALL_RESULTS .equals(context.getProperty(AMOUNT_DOCUMENTS_TO_RETURN).getValue()); boolean processFacetsAndStats = true; boolean continuePaging = true; while (continuePaging) { timer.start(); Map<String, String> responseAttributes = new HashMap<>(); responseAttributes.put(ATTRIBUTE_SOLR_START, solrQuery.getStart().toString()); responseAttributes.put(ATTRIBUTE_SOLR_ROWS, solrQuery.getRows().toString()); if (solrQuery.getStart() > UPPER_LIMIT_START_PARAM) { logger.warn( "The start parameter of Solr query {} exceeded the upper limit of {}. The query will not be processed " + "to avoid performance or memory issues on the part of Solr.", new Object[] { solrQuery.toString(), UPPER_LIMIT_START_PARAM }); flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes); timer.stop(); break; } final QueryRequest req = new QueryRequest(solrQuery); if (isBasicAuthEnabled()) { req.setBasicAuthCredentials(getUsername(), getPassword()); } final QueryResponse response = req.process(getSolrClient()); timer.stop(); final Long totalNumberOfResults = response.getResults().getNumFound(); responseAttributes.put(ATTRIBUTE_SOLR_NUMBER_RESULTS, totalNumberOfResults.toString()); responseAttributes.put(ATTRIBUTE_CURSOR_MARK, response.getNextCursorMark()); responseAttributes.put(ATTRIBUTE_SOLR_STATUS, String.valueOf(response.getStatus())); responseAttributes.put(ATTRIBUTE_QUERY_TIME, String.valueOf(response.getQTime())); flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes); if (response.getResults().size() > 0) { if (context.getProperty(RETURN_TYPE).getValue().equals(MODE_XML.getValue())) { flowFileResponse = session.write(flowFileResponse, SolrUtils.getOutputStreamCallbackToTransformSolrResponseToXml(response)); flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_XML); } else { final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER) .evaluateAttributeExpressions(flowFileResponse) .asControllerService(RecordSetWriterFactory.class); final RecordSchema schema = writerFactory.getSchema(flowFileResponse.getAttributes(), null); final RecordSet recordSet = SolrUtils.solrDocumentsToRecordSet(response.getResults(), schema); final StringBuffer mimeType = new StringBuffer(); final FlowFile flowFileResponseRef = flowFileResponse; flowFileResponse = session.write(flowFileResponse, out -> { try (final RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, flowFileResponseRef)) { writer.write(recordSet); writer.flush(); mimeType.append(writer.getMimeType()); } catch (SchemaNotFoundException e) { throw new ProcessException("Could not parse Solr response", e); } }); flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(), mimeType.toString()); } if (processFacetsAndStats) { if (searchComponents.contains(FacetParams.FACET)) { FlowFile flowFileFacets = session.create(flowFileResponse); flowFileFacets = session.write(flowFileFacets, out -> { try (final OutputStreamWriter osw = new OutputStreamWriter(out); final JsonWriter writer = new JsonWriter(osw)) { addFacetsFromSolrResponseToJsonWriter(response, writer); } }); flowFileFacets = session.putAttribute(flowFileFacets, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_JSON); session.getProvenanceReporter().receive(flowFileFacets, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileFacets, FACETS); } if (searchComponents.contains(StatsParams.STATS)) { FlowFile flowFileStats = session.create(flowFileResponse); flowFileStats = session.write(flowFileStats, out -> { try (final OutputStreamWriter osw = new OutputStreamWriter(out); final JsonWriter writer = new JsonWriter(osw)) { addStatsFromSolrResponseToJsonWriter(response, writer); } }); flowFileStats = session.putAttribute(flowFileStats, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_JSON); session.getProvenanceReporter().receive(flowFileStats, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileStats, STATS); } processFacetsAndStats = false; } } if (getEntireResults) { final Integer totalDocumentsReturned = solrQuery.getStart() + solrQuery.getRows(); if (totalDocumentsReturned < totalNumberOfResults) { solrQuery.setStart(totalDocumentsReturned); session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileResponse, RESULTS); flowFileResponse = session.create(flowFileResponse); } else { continuePaging = false; } } else { continuePaging = false; } } } catch (Exception e) { flowFileResponse = session.penalize(flowFileResponse); flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION, e.getClass().getName()); flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION_MESSAGE, e.getMessage()); session.transfer(flowFileResponse, FAILURE); logger.error("Failed to execute query {} due to {}. FlowFile will be routed to relationship failure", new Object[] { solrQuery.toString(), e }, e); if (flowFileOriginal != null) { flowFileOriginal = session.penalize(flowFileOriginal); } } if (!flowFileResponse.isPenalized()) { session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileResponse, RESULTS); } if (flowFileOriginal != null) { if (!flowFileOriginal.isPenalized()) { session.transfer(flowFileOriginal, ORIGINAL); } else { session.remove(flowFileOriginal); } } }
From source file:org.entrystore.repository.util.SolrSupport.java
License:Apache License
public QueryResult sendQuery(SolrQuery query) { Set<URI> entries = new LinkedHashSet<URI>(); Set<Entry> result = new LinkedHashSet<Entry>(); long hits = -1; int limit = query.getRows(); int offset = query.getStart(); query.setIncludeScore(true);//from w w w . j av a 2 s.c o m int resultFillIteration = 0; do { if (resultFillIteration++ > 0) { if (resultFillIteration > 10) { log.warn("Breaking after 10 result fill interations to prevent too many loops"); break; } offset += 10; log.warn("Increasing offset to fill the result limit"); } hits = sendQueryForEntryURIs(query, entries, solrServer, offset, -1); Date before = new Date(); for (URI uri : entries) { try { Entry entry = rm.getContextManager().getEntry(uri); if (entry != null) { PrincipalManager pm = entry.getRepositoryManager().getPrincipalManager(); // If linkReference or reference to a entry in the same // repository // check that the referenced metadata is accessible. if ((entry.getEntryType() == EntryType.Reference || entry.getEntryType() == EntryType.LinkReference) && entry.getCachedExternalMetadata() instanceof LocalMetadataWrapper) { Entry refEntry = entry.getRepositoryManager().getContextManager() .getEntry(entry.getExternalMetadataURI()); pm.checkAuthenticatedUserAuthorized(refEntry, AccessProperty.ReadMetadata); } else { // Check that the local metadata is accessible. pm.checkAuthenticatedUserAuthorized(entry, AccessProperty.ReadMetadata); } result.add(entry); if (result.size() == limit) { // we have enough results break; } } } catch (AuthorizationException ae) { hits--; continue; } } log.info("Entry fetching took " + (new Date().getTime() - before.getTime()) + " ms"); } while ((limit > result.size()) && (hits > (offset + limit))); return new QueryResult(result, hits); }
From source file:org.geotools.data.solr.SolrFeatureReader.java
License:Open Source License
private String getCursorMarkForStart(HttpSolrServer server, SolrQuery solrQuery) throws SolrServerException { Integer prevRows = solrQuery.getRows(); solrQuery.setRows(solrQuery.getStart()); solrQuery.setStart(0);// w w w. j a v a 2s.c om solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, CursorMarkParams.CURSOR_MARK_START); QueryResponse rsp = server.query(solrQuery); if (this.solrDataStore.getLogger().isLoggable(Level.FINE)) { this.solrDataStore.getLogger().log(Level.FINE, "SOLR query done: " + solrQuery.toString()); } String nextC = rsp.getNextCursorMark(); solrQuery.setRows(prevRows); return nextC; }
From source file:org.hbird.business.archive.solr.SolrProducer.java
License:Apache License
/** * Retrieves a number of entries from the repository, based on the configured * query./* w w w .j a v a 2s . co m*/ * * @param exchange * @throws SolrServerException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws RemoteException */ protected List<EntityInstance> retrieve(SolrQuery query) { LOG.debug("Issuing request '{}'.", query.toString()); /** * Configure the request. * * One keywords are supported * FROMLAST. Will be replaced with the timestamp of the last retrieval (initial is 0). */ /** * Search and set result set. Notice that this will return the results upto the * configured number of rows. More results may thus be in the repository. */ QueryResponse response = null; try { response = endpoint.getServer().query(query); if (response.getStatus() != 0) { LOG.error("Failed to execute retrieval request. Failed with status '{}'.", response.getStatus()); } } catch (Exception e) { LOG.error("Failed to handle query; query: '{}'.", query.toString(), e); } return Utilities.getResultSet(response, query.getRows()); }
From source file:org.janusgraph.diskstorage.solr.SolrIndex.java
License:Apache License
private <E> Stream<E> executeQuery(Integer limit, int offset, String collection, SolrQuery solrQuery, Function<SolrDocument, E> function) throws PermanentBackendException { try {//from w w w. j ava 2 s . c om final SolrResultIterator<E> resultIterator = new SolrResultIterator<>(solrClient, limit, offset, solrQuery.getRows(), collection, solrQuery, function); return StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false); } catch (final IOException | UncheckedIOException e) { logger.error("Query did not complete : ", e); throw new PermanentBackendException(e); } catch (final SolrServerException | UncheckedSolrException e) { logger.error("Unable to query Solr index.", e); throw new PermanentBackendException(e); } }
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"); }/* w w w. 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.rsc.liferay.solr.SolrIndexSearcher.java
License:Open Source License
@Override public Hits search(String searchEngineId, long companyId, Query query, Sort[] sorts, int start, int end) throws SearchException { try {/* w w w.jav a2 s .co m*/ SolrQuery solrQuery = translateQuery(companyId, query, sorts, start, end); QueryResponse queryResponse = _solrServer.query(solrQuery, METHOD.POST); boolean allResults = false; if (solrQuery.getRows() == 0) { allResults = true; } return subset(solrQuery, query, query.getQueryConfig(), queryResponse, allResults); } catch (Exception e) { _log.error(e, e); if (!_swallowException) { throw new SearchException(e.getMessage()); } return new HitsImpl(); } }
From source file:org.rsc.liferay.solr.SolrIndexSearcher.java
License:Open Source License
protected Hits doSearch(SearchContext searchContext, Query query) throws Exception { SolrQuery solrQuery = translateQuery(searchContext.getCompanyId(), query, searchContext.getSorts(), searchContext.getStart(), searchContext.getEnd()); Map<String, Facet> facets = searchContext.getFacets(); for (Facet facet : facets.values()) { if (facet.isStatic()) { continue; }// w ww . ja va 2s. c o m FacetConfiguration facetConfiguration = facet.getFacetConfiguration(); if (facet instanceof RangeFacet) { solrQuery.addFacetField(facetConfiguration.getFieldName()); JSONObject dataJSONObject = facetConfiguration.getData(); JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges"); if (rangesJSONArray == null) { continue; } for (int i = 0; i < rangesJSONArray.length(); i++) { JSONObject rangeJSONObject = rangesJSONArray.getJSONObject(i); String range = rangeJSONObject.getString("range"); String facetQuery = facetConfiguration.getFieldName() + StringPool.COLON + range; solrQuery.addFacetQuery(facetQuery); } } else { solrQuery.addFacetField(facetConfiguration.getFieldName()); } String facetSort = FacetParams.FACET_SORT_COUNT; String order = facetConfiguration.getOrder(); if (order.equals("OrderValueAsc")) { facetSort = FacetParams.FACET_SORT_INDEX; } solrQuery.add("f." + facetConfiguration.getFieldName() + ".facet.sort", facetSort); } solrQuery.setFacetLimit(-1); QueryResponse queryResponse = _solrServer.query(solrQuery, METHOD.POST); boolean allResults = false; if (solrQuery.getRows() == 0) { allResults = true; } List<FacetField> facetFields = queryResponse.getFacetFields(); if (facetFields != null) { for (FacetField facetField : facetFields) { Facet facet = facets.get(facetField.getName()); FacetCollector facetCollector = null; if (facet instanceof RangeFacet) { facetCollector = new SolrFacetQueryCollector(facetField.getName(), queryResponse.getFacetQuery()); } else { facetCollector = new SolrFacetFieldCollector(facetField.getName(), facetField); } facet.setFacetCollector(facetCollector); } } return subset(solrQuery, query, query.getQueryConfig(), queryResponse, allResults); }