List of usage examples for org.apache.solr.common.params SolrParams getInt
public Integer getInt(String param)
null) vs the default value for int - zero (0). From source file:net.semanticmetadata.lire.solr.FastLireRequestHandler.java
License:Open Source License
/** * Searches for an image given by an URL. Note that (i) extracting image features takes time and * (ii) not every image is readable by Java. * * @param req/*w w w. jav a2 s .c o m*/ * @param rsp * @throws java.io.IOException * @throws InstantiationException * @throws IllegalAccessException */ private void handleUrlSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException, InstantiationException, IllegalAccessException { SolrParams params = req.getParams(); String paramUrl = params.get("url"); String paramField = "cl_ha"; if (req.getParams().get("field") != null) paramField = req.getParams().get("field"); int paramRows = defaultNumberOfResults; if (params.get("rows") != null) paramRows = params.getInt("rows"); numberOfQueryTerms = req.getParams().getDouble("accuracy", DEFAULT_NUMBER_OF_QUERY_TERMS); numberOfCandidateResults = req.getParams().getInt("candidates", DEFAULT_NUMBER_OF_CANDIDATES); LireFeature feat = null; List<Term> termFilter = null; int[] hashes = null; // wrapping the whole part in the try try { BufferedImage img = ImageIO.read(new URL(paramUrl).openStream()); img = ImageUtils.trimWhiteSpace(img); // getting the right feature per field: if (paramField == null || FeatureRegistry.getClassForHashField(paramField) == null) // if the feature is not registered. feat = new EdgeHistogram(); else { feat = (LireFeature) FeatureRegistry.getClassForHashField(paramField).newInstance(); } feat.extract(img); hashes = BitSampling.generateHashes(feat.getDoubleHistogram()); termFilter = createTermFilter(hashes, paramField); } catch (Exception e) { rsp.add("Error", "Error reading image from URL: " + paramUrl + ": " + e.getMessage()); e.printStackTrace(); } // search if the feature has been extracted. if (feat != null) doSearch(req, rsp, req.getSearcher(), paramField, paramRows, termFilter, createQuery(hashes, paramField, numberOfQueryTerms), feat); }
From source file:net.semanticmetadata.lire.solr.FastLireRequestHandler.java
License:Open Source License
/** * Search based on the given image hashes. * * @param req/*from w ww . j av a 2 s. co m*/ * @param rsp * @throws java.io.IOException * @throws IllegalAccessException * @throws InstantiationException */ private void handleHashSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException, IllegalAccessException, InstantiationException { SolrParams params = req.getParams(); SolrIndexSearcher searcher = req.getSearcher(); // get the params needed: // hashes=x y z ... // feature=<base64> // field=<cl_ha|ph_ha|...> String[] hashStrings = params.get("hashes").trim().split(" "); byte[] featureVector = Base64.decodeBase64(params.get("feature")); String paramField = "cl_ha"; if (req.getParams().get("field") != null) paramField = req.getParams().get("field"); int paramRows = defaultNumberOfResults; if (params.getInt("rows") != null) paramRows = params.getInt("rows"); numberOfQueryTerms = req.getParams().getDouble("accuracy", DEFAULT_NUMBER_OF_QUERY_TERMS); numberOfCandidateResults = req.getParams().getInt("candidates", DEFAULT_NUMBER_OF_CANDIDATES); // create boolean query: // System.out.println("** Creating query."); LinkedList<Term> termFilter = new LinkedList<Term>(); BooleanQuery query = new BooleanQuery(); for (int i = 0; i < hashStrings.length; i++) { // be aware that the hashFunctionsFileName of the field must match the one you put the hashes in before. hashStrings[i] = hashStrings[i].trim(); if (hashStrings[i].length() > 0) { termFilter.add(new Term(paramField, hashStrings[i].trim())); // System.out.println("** " + field + ": " + hashes[i].trim()); } } Collections.shuffle(termFilter); for (int k = 0; k < termFilter.size() * numberOfQueryTerms; k++) { query.add(new BooleanClause(new TermQuery(termFilter.get(k)), BooleanClause.Occur.SHOULD)); } // System.out.println("** Doing search."); // query feature LireFeature queryFeature = (LireFeature) FeatureRegistry.getClassForHashField(paramField).newInstance(); queryFeature.setByteArrayRepresentation(featureVector); // get results: doSearch(req, rsp, searcher, paramField, paramRows, termFilter, new MatchAllDocsQuery(), queryFeature); }
From source file:org.phenotips.ontology.internal.solr.SolrQueryUtilsTest.java
License:Open Source License
@Test public void testEnhanceParamsDefaultValues() { ModifiableSolrParams input = new ModifiableSolrParams(); SolrParams output = SolrQueryUtils.enhanceParams(input); Assert.assertNull(output.get(CommonParams.Q)); Assert.assertEquals("* score", output.get(CommonParams.FL)); Assert.assertEquals(true, output.getBool(SpellingParams.SPELLCHECK_COLLATE)); Assert.assertEquals(0, (int) output.getInt(CommonParams.START)); Assert.assertTrue(output.getInt(CommonParams.ROWS) > 100); }
From source file:org.phenotips.ontology.internal.solr.SolrQueryUtilsTest.java
License:Open Source License
@Test public void testEnhanceParamsDoesntReplaceExistingValues() { ModifiableSolrParams input = new ModifiableSolrParams(); input.set(CommonParams.Q, "field:value"); input.set(CommonParams.FL, "id"); input.set(CommonParams.START, 30);/*from w ww .java2 s.c o m*/ input.set(CommonParams.ROWS, 10); SolrParams output = SolrQueryUtils.enhanceParams(input); Assert.assertEquals("field:value", output.get(CommonParams.Q)); Assert.assertEquals("id", output.get(CommonParams.FL)); Assert.assertEquals(true, output.getBool(SpellingParams.SPELLCHECK_COLLATE)); Assert.assertEquals(30, (int) output.getInt(CommonParams.START)); Assert.assertEquals(10, (int) output.getInt(CommonParams.ROWS)); }
From source file:org.socialhistoryservices.api.oai.OAIRequestHandler.java
License:Open Source License
@Override public void init(NamedList args) { super.init(args); Utils.setParam(args, "wt", "oai"); Utils.setParam(args, "proxyurl", ""); Utils.setParam(args, "maxrecords", 200); Utils.setParam(args, "resumptionTokenExpirationInSeconds", 86400); Utils.setParam(args, "separator", ","); Utils.setParam(args, "field_index_identifier", "id"); Utils.setParam(args, "prefix", ""); Utils.setParam(args, "field_index_datestamp", "datestamp"); Utils.setParam(args, "field_sort_datestamp", "datestamp"); Utils.setParam(args, "field_index_set", "set"); Utils.setParam(args, "identify", "Identify.xml"); Utils.setParam(args, "listsets", "ListSets.xml"); Utils.setParam(args, "listmetadataformats", "ListMetadataFormats.xml"); final List maxrecords = args.getAll("maxrecords"); if (maxrecords == null) Utils.setParam(args, "maxrecords_default", 200); else {/*from w w w.java 2s . co m*/ SolrParams p = SolrParams.toSolrParams((NamedList) maxrecords.get(0)); final Iterator<String> iterator = p.getParameterNamesIterator(); while (iterator.hasNext()) { String key = iterator.next(); Utils.setParam(args, "maxrecords_" + key, p.getInt(key)); } } // Find the application's solr home folder String solr_home = SolrResourceLoader.locateSolrHome(); if (solr_home == null) solr_home = System.getProperty("solr.solr.home"); String oai_home = (String) args.get("oai_home"); oai_home = (oai_home == null) ? solr_home + File.separatorChar + "oai" : solr_home + oai_home; args.remove("oai_home"); Utils.setParam(args, "oai_home", oai_home); try { final JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class); Utils.setParam("marshaller", jc.createMarshaller()); Utils.setParam("unmarshaller", jc.createUnmarshaller()); } catch (JAXBException e) { log.error(e); } try { initOaiDocument(VerbType.IDENTIFY); initOaiDocument(VerbType.LIST_SETS); initOaiDocument(VerbType.LIST_METADATA_FORMATS); } catch (FileNotFoundException e) { log.error(e); } catch (JAXBException e) { log.error(e); } addStylesheets(oai_home, (OAIPMHtype) Utils.getParam("ListMetadataFormats")); }