List of usage examples for org.apache.solr.schema FieldType getValueSource
public ValueSource getValueSource(SchemaField field, QParser parser)
From source file:GeonamesQParserPlugin.java
License:Apache License
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override/*from w ww .j a v a 2 s. c o m*/ public Query parse() throws ParseException { final int rows = localParams.getInt("rows", 1); final int start = localParams.getInt("start", 0); String topo = localParams.get("topo"); String distFunc = localParams.get("dist"); String latFieldName = localParams.get("lat"); String lonFieldName = localParams.get("lon"); String ghFieldName = localParams.get("gh"); String units = localParams.get("unit", "M"); float boost = localParams.getFloat("boost", 1.0f); double radius = Constants.EARTH_RADIUS_MI; if (units.equalsIgnoreCase("KM")) { radius = Constants.EARTH_RADIUS_KM; } ValueSource vs = null, latVS = null, lonVS = null, ghVS = null; IndexSchema schema = req.getSchema(); DistType distType = DistType.NORM; float power = 2; List<ValueSource> latLon = new ArrayList<ValueSource>(2); if (ghFieldName != null && ghFieldName.equals("") == false) { FieldType ft = schema.getFieldType(ghFieldName); SchemaField sf = schema.getField(ghFieldName); ghVS = ft.getValueSource(sf, this);//Should we pass this here? distType = DistType.GHHSIN; } else if (latFieldName != null && latFieldName.equals("") == false && lonFieldName != null && lonFieldName.equals("") == false) { FieldType ftLat = schema.getFieldType(latFieldName); FieldType ftLon = schema.getFieldType(lonFieldName); SchemaField sfLat = schema.getField(latFieldName); SchemaField sfLon = schema.getField(lonFieldName); latVS = ftLat.getValueSource(sfLat, this); lonVS = ftLon.getValueSource(sfLon, this); latLon.add(latVS); latLon.add(lonVS); try { power = Float.parseFloat(distFunc); distType = DistType.NORM; } catch (NumberFormatException e) { if (distFunc.equals("hsin")) { distType = DistType.HSIN; } else if (distFunc.equals("ghsin")) { distType = DistType.GHHSIN; } } } else { throw new ParseException("Either gh or both lat and lon must be specified."); } ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria(); searchCriteria.setQ(topo); searchCriteria.setMaxRows(rows); searchCriteria.setStartRow(start); Query query = null; try { ToponymSearchResult searchResult = WebService.search(searchCriteria); List<Toponym> topos = searchResult.getToponyms(); if (topos.size() > 1) { BooleanQuery tmp = new BooleanQuery(); for (Toponym toponym : topos) { double lat = toponym.getLatitude(); double lon = toponym.getLongitude(); FunctionQuery fq = getFunction(distType, ghVS, power, latLon, lat, lon, radius); tmp.add(fq, BooleanClause.Occur.SHOULD); } query = tmp; } else if (topos.size() == 1) { Toponym curr = topos.get(0); query = getFunction(distType, ghVS, power, latLon, curr.getLatitude(), curr.getLongitude(), radius); } } catch (Exception e) { //TODO: deal with errors } query.setBoost(boost); return query; } }; }
From source file:com.gogobot.DistanceParser.java
License:Apache License
private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError { String sfield = fp.getParam(SpatialParams.FIELD); if (sfield == null) return null; SchemaField sf = fp.getReq().getSchema().getField(sfield); FieldType type = sf.getType(); if (type instanceof AbstractSpatialFieldType) { AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type; return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield)); }//from ww w . j a va2 s. c om ValueSource vs = type.getValueSource(sf, fp); if (vs instanceof MultiValueSource) { return (MultiValueSource) vs; } throw new SyntaxError( "Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf); }