String textToSearchFor = "someText"; HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); SolrQuery query = new SolrQuery(); query.setQuery(textToSearchFor); QueryResponse rsp = solrServer.query( query );
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { for (String solrUrl : solrUrls) { // delete all previous records HttpSolrServer solrServer = new HttpSolrServer(solrUrl); solrServer.deleteByQuery("*:*"); // optimize here solrServer.optimize(); contribution.getExitStatus().addExitDescription("Cleared: " + solrUrl); solrServer.shutdown(); } return RepeatStatus.FINISHED; }
/** * Remove all documents from the Solr index with the given URL, then commit and optimise the index. * * @param solrUrl URL of the Solr core to clear. * @throws IOException if there is a problem in communicating with Solr. * @throws SolrServerException if there is a problem in communicating with Solr. */ public static void clearIndex(String solrUrl) throws IOException, SolrServerException { HttpSolrServer solr = new HttpSolrServer(solrUrl); solr.deleteByQuery("*:*"); solr.commit(); solr.optimize(); }
HttpSolrServer server = new HttpSolrServer("your restful url goes here"); SolrQuery query = new SolrQuery(); QueryResponse response = server.query(query); List<MyClass> beans = response.getBeans(MyClass.class);
log.info("Connecting to Solr cloud cluster: "+zkHost); CloudSolrServer cloudSolrServer = null; try { cloudSolrServer.connect(); log.info("Commit succeeded, took: "+diffMs+" (ms). Collecting cluster state information ..."); leaders = getShardLeaders(cloudSolrServer, collection); } finally { for (String shard : leaders.keySet()) { String leaderUrl = leaders.get(shard); HttpSolrServer solr = new HttpSolrServer(leaderUrl); if (restartDate == null) { HttpClient httpClient = solr.getHttpClient(); log.info("Sending backup request to "+shard+" leader at: "+leaderUrl); try { snapshotCompletedAt = requestSnapshotCompletedAt(driver, backupState.solrServer.getHttpClient(), leaderUrl+"replication?command=details"); } catch (Exception exc) { leaderClients.get(leaderUrl).solrServer.shutdown();
if (isThereAnythingToShrink(thothDocumentsFacets)){ LOG.info("Found documents to shrink for server " + serverDetail.getName() + "("+serverDetail.getCore()+"):" + serverDetail.getPort()); shrankServer.add(shrankDocument); realTimeServer.deleteByQuery(getRealTimeCoreCleanUpQuery()); } else { LOG.info("Nothing to shrink for server " + serverDetail.getName() +":"+serverDetail.getPort()+" core:"+serverDetail.getCore()+" ..."); LOG.info( serverDetail.getName() +":"+serverDetail.getPort()+" core:"+serverDetail.getCore()+ " shrinking completed."); } catch (SolrServerException e) { LOG.error(e);
public static synchronized SolrServer getSolrServer(String url) { SolrServer server = servers.get(url); if(server == null) { logger.info("Connecting to Solr: " + url); HttpSolrServer httpServer = new HttpSolrServer(url); httpServer.setSoTimeout(Integer.parseInt(SolrMeterConfiguration.getProperty("solr.server.configuration.soTimeout", "60000"))); // socket read timeout httpServer.setConnectionTimeout(Integer.parseInt(SolrMeterConfiguration.getProperty("solr.server.configuration.connectionTimeout", "60000"))); httpServer.setDefaultMaxConnectionsPerHost(Integer.parseInt(SolrMeterConfiguration.getProperty("solr.server.configuration.defaultMaxConnectionsPerHost", "100000"))); httpServer.setMaxTotalConnections(Integer.parseInt(SolrMeterConfiguration.getProperty("solr.server.configuration.maxTotalConnections", "1000000"))); httpServer.setFollowRedirects(Boolean.parseBoolean(SolrMeterConfiguration.getProperty("solr.server.configuration.followRedirect", "false"))); // defaults to false httpServer.setAllowCompression(Boolean.parseBoolean(SolrMeterConfiguration.getProperty("solr.server.configuration.allowCompression", "true"))); httpServer.setMaxRetries(Integer.parseInt(SolrMeterConfiguration.getProperty("solr.server.configuration.maxRetries", "1"))); // defaults to 0. > 1 not recommended. setAuthentication(httpServer); servers.put(url, httpServer); return httpServer; } return server; }
public void createDocumentShrinkers() throws SolrServerException, InterruptedException, ExecutionException { ExecutorService service = Executors.newFixedThreadPool(threadPoolSize); CompletionService<String> completionService = new ExecutorCompletionService<String>(service); HttpSolrServer thothServer = new HttpSolrServer(thothIndexUrl + realTimeCore); HttpSolrServer thothShrankServer = new HttpSolrServer(thothIndexUrl + shrankCore); ArrayList<ServerDetail> listOfServers = new ThothServers().getList(thothServer); for (ServerDetail serverDetail: listOfServers){ LOG.info("Shrinking docs for server("+serverDetail.getName()+"):("+serverDetail.getPort()+") "); completionService.submit(new DocumentShrinker(serverDetail, nowMinusTimeToShrink, thothServer, thothShrankServer)); } // Wait for all the executors to finish for(int i = 0; i < listOfServers.size(); i++){ completionService.take().get(); } LOG.info("Done Shrinking."); }
/** * Tag slower documents and add them to the shrank core */ private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException { // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents QueryResponse qr = realTimeServer.query( new SolrQuery() .setQuery(createThothDocsAggregationQuery()) .addSort(QTIME, SolrQuery.ORDER.desc) .setRows(MAX_NUMBER_SLOW_THOTH_DOCS) ); for (SolrDocument solrDocument: qr.getResults()){ SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument); // Remove old ID and version si.removeField(ID); si.removeField("_version_"); // Tag document as slow si.addField(SLOW_QUERY_DOCUMENT, true); LOG.debug("Adding slow query document for server " + serverDetail.getName()); shrankServer.add(si); } }
HttpSolrServer solr = new HttpSolrServer(solrUrl); SolrQuery query = new SolrQuery("*:*"); if (StringUtils.isNotBlank(fromWhen)) { String lastValueFilter = makeFilterQuery(timeField, fromWhen); if (StringUtils.isNotBlank(lastValueFilter)) { query.addFilterQuery(lastValueFilter); query.setRows(0); query.setGetFieldStatistics(timeField); Map<String, FieldStatsInfo> fieldInfo = solr.query(query).getFieldStatsInfo(); if (fieldInfo == null || !fieldInfo.containsKey(timeField)) { log.warn(String.format( query.setFacetMinCount(1); List<RangeFacet.Count> monthFacets = solr.query(query).getFacetRanges().get(0).getCounts();
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocumentList; import java.net.MalformedURLException; public class SolrJSearcher { public static void main(String[] args) throws MalformedURLException, SolrServerException { HttpSolrServer solr = new HttpSolrServer("url"); SolrQuery query = new SolrQuery(); query.setQuery("sony digital camera"); query.addFilterQuery("cat:electronics","store:amazon.com"); query.setFields("id","price","merchant","cat","store"); query.setStart(0); query.set("defType", "edismax"); QueryResponse response = solr.query(query); SolrDocumentList results = response.getResults(); for (int i = 0; i < results.size(); ++i) { System.out.println(results.get(i)); } } }
SolrServer solrServer = new HttpSolrServer(serverUrl); int numQueries = 10000; int randomPrice = 0; int randomPrice2 = 0; String queryStr; SolrQuery query = new SolrQuery(); query.setStart(0); query.setRows(10); query.setFields("id","listing_price"); long startAt = System.currentTimeMillis();
SolrQuery yearRangeQuery = new SolrQuery(); yearRangeQuery.setQuery("*:*"); yearRangeQuery.setRows(0); yearRangeQuery.setFacet(true); yearRangeQuery.add(FacetParams.FACET_RANGE, "time"); String solrRequestUrl = ((HttpSolrServer) solr).getBaseURL() + "/select"; solrRequestUrl = generateURL(solrRequestUrl, yearQueryParams); statisticsYearServer.request(contentStreamUpdateRequest); statisticsYearServer.commit(true, true);
/** * Retrieves all the metadata fields which are indexed in the authority control * * @return a list of metadata fields * @throws Exception if error */ @Override 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; } }
private int updateRecords(String query) throws SolrServerException, SQLException, IOException { int initNumProcessed = numProcessed; SolrQuery sQ = new SolrQuery(); sQ.setQuery(query); sQ.setRows(batchSize); // Ensure that items are grouped by id // Sort by id fails due to presense of id and string fields. The ord function // seems to help sQ.addSort("type", SolrQuery.ORDER.desc); sQ.addSort("scopeType", SolrQuery.ORDER.desc); sQ.addSort("ord(owningItem)", SolrQuery.ORDER.desc); sQ.addSort("id", SolrQuery.ORDER.asc); sQ.addSort("scopeId", SolrQuery.ORDER.asc); QueryResponse sr = server.query(sQ); SolrDocumentList sdl = sr.getResults(); for (int i = 0; i < sdl.size() && (numProcessed < numRec); i++) { SolrDocument sd = sdl.get(i); SolrInputDocument input = ClientUtils.toSolrInputDocument(sd); input.remove("_version_"); for (FIELD col : FIELD.values()) { mapField(input, col); } docs.add(input); ++numProcessed; } return numProcessed - initNumProcessed; }
public static void main() { String url = "http://localhost:8983/solr/autocomplete"; SolrServer solrServer = new HttpSolrServer(url); SolrQuery query = new SolrQuery(); query.set("q", "*"); query.addFilterQuery("name:*"); query.setFacet(true); query.addFacetField("name"); System.out.println(query); QueryResponse queryResponse = solrServer.query(query); List<FacetField> facetFields = queryResponse.getFacetFields(); FacetField cnameMainFacetField = queryResponse.getFacetField("name"); for (Count cnameAndCount : cnameMainFacetField.getValues()) { String cnameMain = cnameAndCount.getName(); System.out.println(cnameMain); System.out.println(cnameAndCount.getCount()); }
HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr"); SolrQuery query = new SolrQuery(); query.setQuery("company name");
HttpSolrServer solr = new HttpSolrServer( SOLR_URL ); String optFields, Map<String,String>extraParams ) throws Exception SolrQuery q = new SolrQuery( queryStr ); if ( null!=optFilter ) { q.addFilterQuery( optFilter ); q.setParam( "fl", optFields );