List of usage examples for org.apache.solr.common.params ModifiableSolrParams set
public ModifiableSolrParams set(String name, boolean val)
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
private void showFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer coreContainer) throws KeeperException, InterruptedException, UnsupportedEncodingException { SolrZkClient zkClient = coreContainer.getZkController().getZkClient(); String adminFile = getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles); if (adminFile == null) { return;// w w w . j ava 2 s . c o m } // Show a directory listing List<String> children = zkClient.getChildren(adminFile, null, true); if (children.size() > 0) { NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>(); for (String f : children) { if (isHiddenFile(req, rsp, f, false, hiddenFiles)) { continue; } SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>(); files.add(f, fileInfo); List<String> fchildren = zkClient.getChildren(adminFile + "/" + f, null, true); if (fchildren.size() > 0) { fileInfo.add("directory", true); } else { // TODO? content type fileInfo.add("size", f.length()); } // TODO: ? // fileInfo.add( "modified", new Date( f.lastModified() ) ); } rsp.add("files", files); } else { // Include the file contents // The file logic depends on RawResponseWriter, so force its use. ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set(CommonParams.WT, "raw"); req.setParams(params); ContentStreamBase content = new ContentStreamBase.ByteArrayStream( zkClient.getData(adminFile, null, null, true), adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); // Velocity parsing here! // http://velocity.apache.org/engine/devel/developer-guide.html#The_Context Velocity.init(); VelocityContext context = new VelocityContext(); //add some vars?? //context.put( "context", new String("Velocity") ); for (int i = 0; i < rsp.getValues().size(); i++) { context.put(rsp.getValues().getName(i), rsp.getValues().getVal(i)); } Template template = null; String fname = req.getParams().get("file", null); try { //TODO what if fname is null? template = Velocity.getTemplate(fname); } catch (ResourceNotFoundException rnfe) { // couldn't find the template, try to load it // TODO it should be fired only for SOME mimetypes (..through an annotation??) StringBuilder sb = this.getTemplate(content); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(sb.toString()); SimpleNode node = null; try { node = runtimeServices.parse(reader, fname); } catch (ParseException e) { // TODO Auto-generated catch block logger.error("error while parsing new template", e); } template = new Template(); template.setRuntimeServices(runtimeServices); if (node != null) { template.setData(node); } else { logger.error("node null, can't set on template"); } template.initDocument(); } catch (ParseErrorException pee) { // syntax error: problem parsing the template logger.error("error while parsing template: ", pee); } catch (MethodInvocationException mie) { // something invoked in the template // threw an exception logger.error("error while parsing temaplate: ", mie); } catch (Exception e) { logger.error("error while parsing temaplate: ", e); } StringWriter sw = new StringWriter(); template.merge(context, sw); // http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte content = new ContentStreamBase.ByteArrayStream( sw.getBuffer().toString().getBytes(Charset.forName("UTF-8")), adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); rsp.add(RawResponseWriter.CONTENT, content); } rsp.setHttpCaching(false); }
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
private void showFromFileSystem(SolrQueryRequest req, SolrQueryResponse rsp) { File adminFile = getAdminFileFromFileSystem(req, rsp, hiddenFiles); if (adminFile == null) { // exception already recorded return;/* w w w . j av a2s . c o m*/ } // Make sure the file exists, is readable and is not a hidden file if (!adminFile.exists()) { log.error("Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"); rsp.setException(new SolrException(ErrorCode.NOT_FOUND, "Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]")); return; } if (!adminFile.canRead() || adminFile.isHidden()) { log.error("Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"); rsp.setException(new SolrException(ErrorCode.NOT_FOUND, "Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]")); return; } // Show a directory listing if (adminFile.isDirectory()) { // it's really a directory, just go for it. int basePath = adminFile.getAbsolutePath().length() + 1; NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>(); for (File f : adminFile.listFiles()) { String path = f.getAbsolutePath().substring(basePath); path = path.replace('\\', '/'); // normalize slashes if (isHiddenFile(req, rsp, f.getName().replace('\\', '/'), false, hiddenFiles)) { continue; } SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>(); files.add(path, fileInfo); if (f.isDirectory()) { fileInfo.add("directory", true); } else { // TODO? content type fileInfo.add("size", f.length()); } fileInfo.add("modified", new Date(f.lastModified())); } rsp.add("files", files); } else { // Include the file contents //The file logic depends on RawResponseWriter, so force its use. ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set(CommonParams.WT, "raw"); req.setParams(params); ContentStreamBase content = new ContentStreamBase.FileStream(adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); rsp.add(RawResponseWriter.CONTENT, content); } rsp.setHttpCaching(false); }
From source file:alba.solr.searchcomponents.AlbaRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { // TODO Auto-generated method stub /* List<SearchComponent> components = new ArrayList<SearchComponent>(); /*from w w w. jav a 2 s .c o m*/ MySearchComponent msc = new MySearchComponent(); ResponseBuilder rb = new ResponseBuilder(req, rsp, components); msc.process(rb);*/ //rsp.add("hello", rb.rsp.getValues()); req.getContext().put(Loader.FUNCTIONS, functions); Object params[] = new Object[2]; params[0] = req; params[1] = rsp; // what if this method calls rsp.add( .. ) ???? Object result = this.function.getMethod().invoke(this.function.getInstance(), params); if (Map.class.isAssignableFrom(result.getClass())) { // if we got a Map, just return it as-is rsp.add(this.sectionName, result); } else // if we got anything else, try to serialize it! if (List.class.isAssignableFrom(result.getClass())) { for (Object o : (List) result) { DocumentObjectBinder dob = new DocumentObjectBinder(); SolrInputDocument sd = dob.toSolrInputDocument(o); SolrDocument dest = ClientUtils.toSolrDocument(sd); HashMap<Object, Object> nl = (HashMap<Object, Object>) dest.get("functionDescriptor"); //rsp.add(nl.get("name").toString(), dest2); rsp.add(null, dest); } } if (StaticResource.class.isAssignableFrom(result.getClass())) { FilteredShowFileRequestHandler file = new FilteredShowFileRequestHandler(); file.init(new NamedList()); //to initialize internal variables - but in this way it will NOT get the proper configuration from SolrConfig! ModifiableSolrParams solrParams = new ModifiableSolrParams(req.getParams()); StaticResource resource = ((StaticResource) result); solrParams.set("file", resource.getName()); //TODO Proper mapping here!! //solrParams.set("contentType", "text/xml;charset=utf-8"); solrParams.set("contentType", resource.getContentType()); req.setParams(solrParams); file.handleRequest(req, rsp); // logger.error("returning the content of " + ); } else { // unable to do any kind of serialization.. just add the result and let the ResponseWriter handle it rsp.add(null, result); } }
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
/** * Gets the details about the SOLR fields using the LukeRequestHandler: * See http://wiki.apache.org/solr/LukeRequestHandler for more information *///ww w. j a va 2 s . c om public Set<IndexFieldDTO> getIndexFieldDetails(String... fields) throws Exception { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/admin/luke"); params.set("tr", "luke.xsl"); if (fields != null) { params.set("fl", fields); params.set("numTerms", "1"); } else { // TODO: We should be caching the result locally without calling Solr in this case, as it is called very often params.set("numTerms", "0"); } QueryResponse response = query(params, queryMethod); return parseLukeResponse(response.toString(), fields != null); }
From source file:com.adr.bigdata.search.product.fe.BaseSuggestionHandler.java
@Override public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) { String cacheKey = SolrParamUtils.transform(req.getParams()); try {/*from w w w . j a v a 2 s.c om*/ String cacheResponse = this.rdModel.get(cacheKey); if (cacheResponse == null) { //cacheMiss try { final Object[] terms = new Object[1]; suggestionLogic.execute(req, rsp, new Callable() { @Override public void call(Object... args) { terms[0] = args[0]; } }); SolrQueryResponse _rsp = new SolrQueryResponse(); super.handleRequest(req, _rsp); suggestionLogic.writeRsp(req, _rsp, terms[0]); String result = outerString(req, _rsp); if (!zeroResult(_rsp)) { this.rdModel.put(cacheKey, result); } ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set("vincache", true); SolrParams _params = SolrParams.wrapDefaults(params, defaults); _params = SolrParams.wrapAppended(_params, appends); req.setParams(_params); rsp.add("vincache", result); } catch (Exception e) { error(req, rsp); getLogger().error("", e); } } else { //cache hit ModifiableSolrParams solrParam = new ModifiableSolrParams(req.getParams()); solrParam.set("vincache", true); SolrParams params = SolrParams.wrapAppended(solrParam, appends); params = SolrParams.wrapDefaults(params, defaults); req.setParams(params); rsp.add("vincache", cacheResponse); } } catch (Exception cacheEx) { getLogger().error("fail to get from redis cache.....{}", cacheEx.getMessage()); try { final Object[] terms = new Object[1]; suggestionLogic.execute(req, rsp, new Callable() { @Override public void call(Object... args) { terms[0] = args[0]; } }); super.handleRequest(req, rsp); suggestionLogic.writeRsp(req, rsp, terms[0]); } catch (Exception e) { error(req, rsp); getLogger().error("", e); } } }
From source file:com.apexxs.neonblack.solr.Queries.java
License:Apache License
public List<GeoNamesEntry> doNameQuery(String searchTerm) { List<GeoNamesEntry> entries = new ArrayList<>(); String sortString;//from w w w . j a v a 2 s . co m if (config.populationSort()) { sortString = "population desc, score desc"; } else { sortString = "score desc"; } ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/nameSearcher"); params.set("q", searchTerm); params.set("mm", "100%"); params.set("qs", "3"); params.set("fl", "*, score"); params.set("sort", sortString); params.set("rows", config.getMaxHits()); try { QueryResponse response = geonamesCore.query(params); entries = response.getBeans(GeoNamesEntry.class); } catch (Exception ex) { logger.error("Error in doNameQuery for argument " + searchTerm + " " + ex.getMessage()); } return entries; }
From source file:com.apexxs.neonblack.solr.Queries.java
License:Apache License
public List<GeoNamesEntry> doNameQuery(String searchTerm, String minShouldMatch) { List<GeoNamesEntry> entries = new ArrayList<>(); String sortString;// w w w . j a va 2s. co m if (config.populationSort()) { sortString = "population desc, score desc"; } else { sortString = "score desc"; } ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/nameSearcher"); params.set("q", searchTerm); params.set("mm", minShouldMatch); params.set("fl", "*, score"); params.set("sort", sortString); params.set("rows", config.getMaxHits()); try { QueryResponse response = geonamesCore.query(params); entries = response.getBeans(GeoNamesEntry.class); } catch (Exception ex) { logger.error("Error in doNameQuery for argument " + searchTerm + " " + ex.getMessage()); } return entries; }
From source file:com.apexxs.neonblack.solr.Queries.java
License:Apache License
public List<GeoNamesEntry> doSelectQuery(String searchTerm) { List<GeoNamesEntry> entries = new ArrayList<>(); String sortString;/*ww w . j a va 2 s . c o m*/ if (config.populationSort()) { sortString = "population desc, score desc"; } else { sortString = "score desc"; } ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/select"); params.set("q", searchTerm); params.set("fl", "*, score"); params.set("sort", sortString); params.set("rows", config.getMaxHits()); try { QueryResponse response = geonamesCore.query(params); entries = response.getBeans(GeoNamesEntry.class); } catch (Exception ex) { logger.error("Error in doSelectQuery for argument " + searchTerm + " " + ex.getMessage()); } return entries; }
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.comm.sr.common.solr.SolrQueryService.java
public SolrQueryService(CacheService<String, String> cacheService, Properties settings) { super(cacheService, settings); try {//from w ww.j a v a 2 s . c o m String zkHost = settings.getProperty("solrcloud.zkHost"); int max_connections = Integer.parseInt(settings.getProperty("solrcloud.max_connections")); int max_connections_per_host = Integer .parseInt(settings.getProperty("solrcloud.max_connections_per_host")); int zkConnectTimeout = Integer.parseInt(settings.getProperty("solrcloud.zkConnectTimeout")); int zkClientTimeout = Integer.parseInt(settings.getProperty("solrcloud.zkClientTimeout")); ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, max_connections); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, max_connections_per_host); HttpClient client = HttpClientUtil.createClient(params); LBHttpSolrServer lbServer = new LBHttpSolrServer(client); cloudSolrServer = new CloudSolrServer(zkHost, lbServer); cloudSolrServer.setZkConnectTimeout(zkConnectTimeout); cloudSolrServer.setZkClientTimeout(zkClientTimeout); } catch (Exception e) { } }