List of usage examples for org.apache.solr.client.solrj.request QueryRequest QueryRequest
public QueryRequest(SolrParams q)
From source file:com.cloudera.cdk.morphline.solr.SolrMorphlineZkAliasTest.java
License:Apache License
private NamedList<Object> createAlias(String alias, String collections) throws SolrServerException, IOException { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("collections", collections); params.set("name", alias); params.set("action", CollectionAction.CREATEALIAS.toString()); QueryRequest request = new QueryRequest(params); request.setPath("/admin/collections"); return cloudClient.request(request); }
From source file:com.databasepreservation.visualization.utils.SolrUtils.java
public static InputStream findCSV(SolrClient index, String collection, Filter filter, Sorter sorter, Sublist sublist, List<String> fields) throws GenericException, RequestNotValidException { SolrQuery query = new SolrQuery(); query.setQuery(parseFilter(filter)); query.setSorts(parseSorter(sorter)); if (sublist != null) { query.setStart(sublist.getFirstElementIndex()); query.setRows(sublist.getMaximumElementCount()); }/* w ww.jav a2 s. com*/ query.setFields(fields.toArray(new String[0])); LOGGER.debug("CSV export query object: " + query.toString()); LOGGER.debug("CSV export query: " + query.toQueryString()); try { QueryRequest queryRequest = new QueryRequest(query); queryRequest.setResponseParser(new InputStreamResponseParser("csv")); QueryResponse response = queryRequest.process(index, collection); Object stream = response.getResponse().get("stream"); if (stream instanceof InputStream) { return (InputStream) stream; } else { throw new GenericException( "Result was not an input stream. Its string representation was: " + stream.toString()); } } catch (SolrServerException | IOException e) { throw new GenericException("Could not query index", e); } }
From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java
License:Open Source License
public static void main(String[] args) throws Exception { SolrDocumentList solrDocumentList;/*from w w w. j a v a 2 s . co m*/ // // InputStream inputXML = OpenSearchSolrServer.class.getResourceAsStream("opensearch_example.xml"); // SAXReader saxReader = new SAXReader(); // Document doc = saxReader.read(inputXML); // IOUtils.closeQuietly(inputXML); // // System.out.println("Mock request"); // solrDocumentList = parse(doc.getRootElement()); // printResults(solrDocumentList); System.out.println("Real request"); OpenSearchSolrServer solrServer = new OpenSearchSolrServer(); ModifiableSolrParams params = new ModifiableSolrParams(); params.add("openSearchURL", "http://recherched.gouv.qc.ca/internet/opensearch"); params.add(CommonParams.Q, "doculibre"); params.add(CommonParams.START, "5"); params.add(CommonParams.ROWS, "10"); params.add("lang", "en"); NamedList<Object> results = solrServer.request(new QueryRequest(params)); solrDocumentList = (SolrDocumentList) results.get("response"); printResults(solrDocumentList); QueryResponse queryResponse = solrServer.query(params); solrDocumentList = queryResponse.getResults(); printResults(solrDocumentList); }
From source file:com.github.fengtan.sophie.tables.DocumentsTable.java
License:Open Source License
/** * Export documents into CSV file.//from w w w . j a va 2 s . c om * * @throws SophieException * If the documents could not be exported into a CSV file. */ public void export() throws SophieException { // Open dialog to let the user select where the file will be dumped. FileDialog dialog = new FileDialog(table.getShell(), SWT.SAVE); dialog.setFilterNames(new String[] { "CSV Files (*.csv)", "All Files (*.*)" }); dialog.setFilterExtensions(new String[] { "*.csv", "*.*" }); String date = new SimpleDateFormat("yyyy-MM-dd-HH:mm").format(new Date()); dialog.setFileName("documents_" + date + ".csv"); String path = dialog.open(); // User did not selected any location. if (path == null) { return; } // Send Solr query and write result into file. SolrQuery query = getBaseQuery(0, table.getItemCount()); QueryRequest request = new QueryRequest(query); request.setResponseParser(new NoOpResponseParser("csv")); NamedList<Object> response; try { response = Sophie.client.request(request); } catch (SolrServerException | IOException | SolrException e) { throw new SophieException("Unable to get CSV documents from Solr", e); } String csv = (String) response.get("response"); try { Writer writer = new PrintWriter(path, "UTF-8"); writer.write(csv); writer.close(); } catch (IOException e) { throw new SophieException("Unable to write into file " + path, e); } }
From source file:com.github.fengtan.sophie.tables.DocumentsTable.java
License:Open Source License
/** * Restore index from a backup/*from w ww .ja va2s . c o m*/ * * @param backupName * Backup file name. * @throws SophieException * If the backup could not be restored. */ public void restore(String backupName) throws SophieException { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("command", "restore"); params.set("name", backupName); QueryRequest request = new QueryRequest(params); request.setPath("/replication"); try { Sophie.client.request(request); refresh(); } catch (SolrServerException | IOException | SolrException e) { throw new SophieException("Unable to restore backup \"" + backupName + "\"", e); } refresh(); }
From source file:com.github.fengtan.sophie.toolbars.DocumentsToolbar.java
License:Open Source License
/** * Populate toolbar with buttons./*from www . ja v a 2 s.co m*/ * * @param composite * Parent composite. */ private void initToolbar(final Composite composite) { Display display = composite.getDisplay(); ClassLoader loader = getClass().getClassLoader(); // Instantiate toolbar. ToolBar toolBar = new ToolBar(composite, SWT.BORDER); // Instantiate images. imgRefresh = new Image(display, loader.getResourceAsStream("toolbar/refresh.png")); imgAdd = new Image(display, loader.getResourceAsStream("toolbar/add.png")); imgDelete = new Image(display, loader.getResourceAsStream("toolbar/delete.png")); imgClone = new Image(display, loader.getResourceAsStream("toolbar/clone.png")); imgUpload = new Image(display, loader.getResourceAsStream("toolbar/upload.png")); imgAddField = new Image(display, loader.getResourceAsStream("toolbar/add_field.png")); imgClear = new Image(display, loader.getResourceAsStream("toolbar/clear.png")); imgCommit = new Image(display, loader.getResourceAsStream("toolbar/commit.png")); imgOptimize = new Image(display, loader.getResourceAsStream("toolbar/optimize.png")); imgExport = new Image(display, loader.getResourceAsStream("toolbar/export.png")); imgBackup = new Image(display, loader.getResourceAsStream("toolbar/backup.png")); imgRestore = new Image(display, loader.getResourceAsStream("toolbar/restore.png")); // Instantiate buttons. itemRefresh = new ToolItem(toolBar, SWT.PUSH); itemRefresh.setImage(imgRefresh); itemRefresh.setText("Refresh"); itemRefresh.setToolTipText("Refresh from Solr: this will wipe out local modifications"); itemRefresh.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { table.refresh(); } catch (SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to refresh documents from Solr server", e)); } } }); new ToolItem(toolBar, SWT.SEPARATOR); itemAdd = new ToolItem(toolBar, SWT.PUSH); itemAdd.setImage(imgAdd); itemAdd.setText("Add"); itemAdd.setToolTipText("Add new document"); itemAdd.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { table.addDocument(new SolrDocument()); } }); itemDelete = new ToolItem(toolBar, SWT.PUSH); itemDelete.setImage(imgDelete); itemDelete.setText("Delete"); itemDelete.setToolTipText("Delete document"); itemDelete.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { table.deleteSelectedDocument(); } }); itemDelete.setEnabled(false); itemClone = new ToolItem(toolBar, SWT.PUSH); itemClone.setImage(imgClone); itemClone.setText("Clone"); itemClone.setToolTipText("Clone document"); itemClone.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { SolrDocument document = table.getSelectedDocument(); if (document == null) { return; } // Unset the unique key field so we don't have two rows // describing the same Solr document. try { String uniqueKey = SolrUtils.getRemoteUniqueField(); document.removeFields(uniqueKey); } catch (SophieException e) { Sophie.log.warn("Unable to unset unique key on cloned document"); } // Add cloned document. table.addDocument(document); } }); itemClone.setEnabled(false); itemUpload = new ToolItem(toolBar, SWT.PUSH); itemUpload.setImage(imgUpload); itemUpload.setText("Upload"); itemUpload.setToolTipText("Upload local modifications to Solr"); itemUpload.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { table.upload(); } catch (SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to upload local modifications to Solr", e)); } } }); itemUpload.setEnabled(false); new ToolItem(toolBar, SWT.SEPARATOR); itemAddField = new ToolItem(toolBar, SWT.PUSH); itemAddField.setImage(imgAddField); itemAddField.setText("Add field"); itemAddField.setToolTipText("Add new field"); itemAddField.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Get field schema fields. Map<String, FieldInfo> fields; try { fields = SolrUtils.getRemoteSchemaFields(); } catch (SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to fetch schema fields", e)); return; } // Remove universal pattern which is not useful and will match // any field name. fields.remove("*"); // Remove fields already displayed in the table. Collection<String> excludedFieldNames = table.getFieldNames(); for (String existingFieldName : excludedFieldNames) { fields.remove(existingFieldName); } // Extract and sort field names. Set<String> fieldNames = fields.keySet(); String[] fieldNamesArray = new String[fieldNames.size()]; fieldNames.toArray(fieldNamesArray); Arrays.sort(fieldNamesArray); // Prompt user for new field name. FieldValidator validator = new FieldValidator(fields, excludedFieldNames); CComboDialog dialog = new CComboDialog(composite.getShell(), "Add new field", "Field name:", fieldNamesArray, validator); dialog.open(); if (dialog.getReturnCode() != IDialogConstants.OK_ID) { return; } // Add new field. String fieldName = dialog.getValue(); FieldInfo field = validator.getMatchingField(fieldName); table.addField(fieldName, field); } }); itemClear = new ToolItem(toolBar, SWT.PUSH); itemClear.setImage(imgClear); itemClear.setText("Clear"); itemClear.setToolTipText("Clear index"); itemClear.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); messageBox.setText("Clear index"); messageBox.setMessage( "Do you really want to clear the index? This will remove all documents from the index."); int response = messageBox.open(); if (response == SWT.YES) { try { Sophie.client.deleteByQuery("*:*"); Sophie.client.commit(); table.refresh(); } catch (SolrServerException | IOException | SolrException | SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to clear index", e)); } } } }); itemCommit = new ToolItem(toolBar, SWT.PUSH); itemCommit.setImage(imgCommit); itemCommit.setText("Commit"); itemCommit.setToolTipText("Commit index"); itemCommit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { Sophie.client.commit(); table.refresh(); } catch (SolrServerException | IOException | SolrException | SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to commit index", e)); } } }); itemOptimize = new ToolItem(toolBar, SWT.PUSH); itemOptimize.setImage(imgOptimize); itemOptimize.setText("Optimize"); itemOptimize.setToolTipText("Optimize index"); itemOptimize.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); messageBox.setText("Optimize index"); messageBox.setMessage( "Do you really want to optimize the index? If the index is highly segmented, this may take several hours and will slow down requests."); int response = messageBox.open(); if (response == SWT.YES) { try { Sophie.client.optimize(); // Optimizing drops obsolete documents, obsolete facet // values, etc so we need to refresh the table. table.refresh(); } catch (SolrServerException | IOException | SolrException | SophieException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to optimize index", e)); } } } }); new ToolItem(toolBar, SWT.SEPARATOR); itemExport = new ToolItem(toolBar, SWT.PUSH); itemExport.setImage(imgExport); itemExport.setText("Export"); itemExport.setToolTipText("Export as CSV file"); itemExport.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { table.export(); } catch (SophieException e) { ExceptionDialog.open(composite.getShell(), e); } } }); itemBackup = new ToolItem(toolBar, SWT.PUSH); itemBackup.setImage(imgBackup); itemBackup.setText("Backup"); itemBackup.setToolTipText("Make a backup of the index"); itemBackup.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { InputDialog dialog = new InputDialog(composite.getShell(), "Make a backup of the index", "Backup name (leave empty to use a timestamp):", null, null); dialog.open(); if (dialog.getReturnCode() != IDialogConstants.OK_ID) { return; } String backupName = dialog.getValue(); ModifiableSolrParams params = new ModifiableSolrParams(); params.set("command", "backup"); params.set("name", backupName); QueryRequest request = new QueryRequest(params); request.setPath("/replication"); try { NamedList<Object> response = Sophie.client.request(request); // org.apache.solr.handler.ReplicationHandler.OK_STATUS is // "OK". if (StringUtils.equals(response.get("status").toString(), "OK")) { MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_INFORMATION | SWT.OK); messageBox.setText("Backup started"); messageBox.setMessage( "Backup started and will be saved into Solr data directory under the name \"snapshot." + (StringUtils.isEmpty(backupName) ? "<timestamp>" : backupName) + "\"."); messageBox.open(); } else { MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_ERROR | SWT.OK); messageBox.setText("Backup error"); messageBox.setMessage("Unable to backup the index."); messageBox.open(); } } catch (SolrServerException | IOException | SolrException e) { ExceptionDialog.open(composite.getShell(), new SophieException("Unable to create backup \"" + backupName + "\"", e)); } } }); itemRestore = new ToolItem(toolBar, SWT.PUSH); itemRestore.setImage(imgRestore); itemRestore.setText("Restore"); itemRestore.setToolTipText("Restore index from a backup"); itemRestore.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Restoring a backup requires Solr >=5.2 // @see SOLR-6637 InputDialog dialog = new InputDialog(composite.getShell(), "Restore index from a backup", "Note: backup restoration requires Solr >= 5.2.\n\nBackup name (leave empty to pick the latest backup available):", null, null); dialog.open(); if (dialog.getReturnCode() != IDialogConstants.OK_ID) { return; } try { table.restore(dialog.getValue()); } catch (SophieException e) { ExceptionDialog.open(composite.getShell(), e); } } }); // Pack. toolBar.pack(); }
From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.RankerCreationUtil.java
License:Open Source License
/** * Retrieve a {@link CandidateAnswer} with its {@code threadPostId} by querying the /select * endpoint with query THREAD_POST_ID:x//from w w w .ja v a 2 s. c om * * @param searcher An initialized {@link RetrieveAndRankSearcher} * @param threadPostId The THREAD_POST_ID of the answer thread * @return * @throws IOException * @throws IngestionException */ public static CandidateAnswer getCandidateAnswerById(RetrieveAndRankSearcher searcher, String threadPostId) throws IOException, IngestionException { CandidateAnswer answer = null; try { SolrQuery featureSolrQuery = new SolrQuery( RetrieveAndRankSearcherConstants.ID_FIELD + ":" + threadPostId); // specify the request handler for the feature query featureSolrQuery.setRequestHandler(RetrieveAndRankSearcherConstants.SELECT_REQUEST_HANDLER); // We expect only one response since THREAD_POST_ID is a unique key if (featureSolrQuery.size() != 1) { throw new IngestionException(threadPostId); } featureSolrQuery.setRows(1); final QueryRequest featureRequest = new QueryRequest(featureSolrQuery); QueryResponse featureResponse = null; featureResponse = searcher.processSolrRequest(featureRequest); for (SolrDocument doc : featureResponse.getResults()) { byte[] bin = (byte[]) doc.getFieldValue(IndexDocumentFieldName.SERIALIZED_THREAD.toString()); answer = StackExchangeThreadSerializer.deserializeThreadFromBinArr(bin); } } catch (IOException | SolrServerException | InterruptedException e) { logger.error(e.getMessage()); } return answer; }
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.RetrieveAndRankSearcher.java
License:Open Source License
public Collection<CandidateAnswer> performSearch(String query, int numAns) throws SearchException { SolrQuery featureSolrQuery = new SolrQuery(query); // Specify the request handler for the feature query featureSolrQuery.setRequestHandler(request_handler); // Specify parameters for the response featureSolrQuery.setParam(RetrieveAndRankSearcherConstants.FIELD_LIST_PARAM, RetrieveAndRankSearcherConstants.ID_FIELD + "," + RetrieveAndRankSearcherConstants.FEATURE_VECTOR_FIELD + "," + IndexDocumentFieldName.SERIALIZED_THREAD.toString()); featureSolrQuery.setRows(numAns);/*from w ww .j av a 2 s . c o m*/ // Make the request final QueryRequest featureRequest = new QueryRequest(featureSolrQuery); QueryResponse featureResponse = null; try { featureResponse = processSolrRequest(featureRequest); } catch (IOException | SolrServerException | InterruptedException e) { log.error(e.toString(), e); throw new SearchException(e); } return responseToCollection(featureResponse); }
From source file:com.ngdata.hbaseindexer.indexer.FusionPipelineClient.java
License:Apache License
public QueryResponse queryFusion(SolrQuery query) throws Exception { int requestId = requestCounter.incrementAndGet(); ArrayList<String> mutable = getAvailableEndpoints(); String endpoint = mutable.get(0); FusionSession fusionSession = null;/* w w w . j a v a 2 s .c om*/ long currTime = System.nanoTime(); synchronized (this) { fusionSession = sessions.get(endpoint); // ensure last request within the session timeout period, else reset the session if (fusionSession == null || (currTime - fusionSession.sessionEstablishedAt) > maxNanosOfInactivity) { log.info("Fusion session is likely expired (or soon will be) for endpoint " + endpoint + ", " + "pre-emptively re-setting this session before processing request " + requestId); fusionSession = resetSession(endpoint); if (fusionSession == null) throw new IllegalStateException("Failed to re-connect to " + endpoint + " after session loss when processing request " + requestId); } } if (fusionSession.solrClient == null) { fusionSession.solrClient = new HttpSolrClient(endpoint, httpClient); } QueryRequest qreq = new QueryRequest(query); qreq.setResponseParser(new XMLResponseParser()); QueryResponse qr = new QueryResponse((SolrClient) fusionSession.solrClient); qr.setResponse(fusionSession.solrClient.request(qreq)); return qr; }
From source file:com.sitewhere.connectors.solr.search.SolrSearchProvider.java
License:Open Source License
@Override public JsonNode executeQueryWithRawResponse(String queryString) throws SiteWhereException { try {/*from w w w. j av a 2s. c om*/ LOGGER.debug("About to execute Solr search with query string: " + queryString); NoOpResponseParser rawJsonResponseParser = new NoOpResponseParser(); rawJsonResponseParser.setWriterType("json"); SolrQuery query = new SolrQuery(); query.add(createParamsFromQueryString(queryString)); QueryRequest request = new QueryRequest(query); request.setResponseParser(rawJsonResponseParser); NamedList<?> results = getSolr().getSolrClient().request(request); return MAPPER.readTree((String) results.get("response")); } catch (SolrServerException e) { throw new SiteWhereException("Unable to execute query.", e); } catch (IOException e) { throw new SiteWhereException("Unable to execute query.", e); } }