org.apache.solr.common.params.SpatialParams Java Examples

The following examples show how to use org.apache.solr.common.params.SpatialParams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocationGroupingHandler.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
@Override
public void prepareGroupQueryParams(ModifiableSolrParams params) {

  String pt = params.get(SpatialParams.POINT);
  if (pt == null) {
    return;
  }

  params.add(SpatialParams.POINT, pt);
  params.add(CommonParams.FQ, fq);
  params.add(SpatialParams.FIELD, sfield);

  // Use distance from config if not available in the request
  String d = params.get(SpatialParams.DISTANCE);
  if (d == null) {
    params.add(SpatialParams.DISTANCE, distance);
  }
}
 
Example #2
Source File: GeoDistValueSourceParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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) {
    @SuppressWarnings({"rawtypes"})
    AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
    return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
  }
  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);
}
 
Example #3
Source File: CentroidComponentTest.java    From query-segmenter with Apache License 2.0 6 votes vote down vote up
/**
 * The CentroidComponent should change the POINT parameter to Alcova 1 center (42.4525,-107.1897) because it is
 * closest to the original user location.
 */
@Test
public void test_closest_centroid() {

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(CommonParams.QT, CENTROID_REQUEST_HANDLER);
  params.add(CommonParams.Q, "Alcova");
  // We choose a point that is closer to Alcova 1 than Alcova 2, but far enough of any documents indexed, so that if
  // this user location if used instead of Alcova 1, no document will matched.
  params.add(SpatialParams.POINT, "42.4,-100");

  SolrQueryRequest req = request(params);

  assertQ(req, "//result[@name='response'][@numFound='1']",
      "//result[@name='response']/doc[1]/str[@name='id'][.='3']");

}
 
Example #4
Source File: QueryParserBase.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected String createSpatialFunctionFragment(String fieldName, org.springframework.data.geo.Point location,
		Distance distance, String function) {
	String spatialFragment = "{!" + function + " " + SpatialParams.POINT + "=";
	spatialFragment += filterCriteriaValue(location);
	spatialFragment += " " + SpatialParams.FIELD + "=" + fieldName;
	spatialFragment += " " + SpatialParams.DISTANCE + "=" + filterCriteriaValue(distance);
	spatialFragment += "}";
	return spatialFragment;
}
 
Example #5
Source File: GeoDistValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private MultiValueSource parsePoint(FunctionQParser fp) throws SyntaxError {
  String ptStr = fp.getParam(SpatialParams.POINT);
  if (ptStr == null) return null;
  Point point = SpatialUtils.parsePointSolrException(ptStr, SpatialContext.GEO);
  //assume Lat Lon order
  return new VectorValueSource(
      Arrays.<ValueSource>asList(new DoubleConstValueSource(point.getY()), new DoubleConstValueSource(point.getX())));
}
 
Example #6
Source File: CentroidComponent.java    From query-segmenter with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {

  SolrParams params = rb.req.getParams();
  String q = params.get(CommonParams.Q);

  List<TypedSegment> typedSegments = segmenter.segment(q);
  if (typedSegments.isEmpty()) {
    return;
  }

  // If multiple matches, use the closest from the user
  CentroidTypedSegment centroidSegment;
  if (typedSegments.size() > 1) {
    double[] userLatlon = getUserLocation(params);
    centroidSegment = getClosestSegment(typedSegments, userLatlon);
  } else {
    centroidSegment = (CentroidTypedSegment) typedSegments.get(0);
  }

  // Override point to use the value from the matching centroid.
  ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams(params);
  modifiableSolrParams.set(SpatialParams.POINT,
      String.format("%s,%s", centroidSegment.getLatitude(), centroidSegment.getLongitude()));
  
  q = q.replaceAll(centroidSegment.getSegment(), "");
  
  modifiableSolrParams.set(CommonParams.Q, q);
  
  rb.req.setParams(modifiableSolrParams);
}
 
Example #7
Source File: CentroidComponent.java    From query-segmenter with Apache License 2.0 5 votes vote down vote up
private double[] getUserLocation(SolrParams params) {
  String userLocation = params.get(SpatialParams.POINT);
  if (userLocation == null || userLocation.isEmpty()) {
    throw new IllegalArgumentException("pt is missing.");
  }
  String[] latlon = userLocation.split(",");
  if (latlon.length != 2) {
    throw new IllegalArgumentException("pt is invalid (should be [lat,lon]).");
  }
  return new double[] { Double.valueOf(latlon[0]), Double.valueOf(latlon[1]) };
}
 
Example #8
Source File: CentroidComponentTest.java    From query-segmenter with Apache License 2.0 5 votes vote down vote up
/**
 * The CentroidComponent should change the POINT parameter to Ace center (30.539,-94.788) so instead of returning
 * document 2, which is closer to the original user location, it will return document 1 which is closer to the new
 * POINT.
 */
@Test
public void test_centroid() {

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(CommonParams.QT, CENTROID_REQUEST_HANDLER);
  params.add(CommonParams.Q, "near Ace");
  params.add(SpatialParams.POINT, "42.1548,-93.9497");

  SolrQueryRequest req = request(params);

  assertQ(req, "//result[@name='response'][@numFound='1']",
      "//result[@name='response']/doc[1]/str[@name='id'][.='1']");

}
 
Example #9
Source File: CentroidComponentTest.java    From query-segmenter with Apache License 2.0 5 votes vote down vote up
@Test
public void test_centroid_2() {

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(CommonParams.QT, CENTROID_REQUEST_HANDLER);
  params.add(CommonParams.Q, "near Aaronsburg");
  params.add(SpatialParams.POINT, "80,-120");

  SolrQueryRequest req = request(params);

  assertQ(req, "//result[@name='response'][@numFound='1']",
      "//result[@name='response']/doc[1]/str[@name='id'][.='5']");

}
 
Example #10
Source File: SolrIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Iterable<? extends DocumentDistance> geoQuery(IRI geoProperty, Point p, final IRI units, double distance,
		String distanceVar, Var contextVar) throws MalformedQueryException, IOException {
	double kms = GeoUnits.toKilometres(distance, units);

	String qstr = "{!geofilt score=recipDistance}";
	if (contextVar != null) {
		Resource ctx = (Resource) contextVar.getValue();
		String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
		if (ctx != null) {
			qstr = tq + " AND " + qstr;
		} else {
			qstr = "-" + tq + " AND " + qstr;
		}
	}
	SolrQuery q = new SolrQuery(qstr);
	q.set(SpatialParams.FIELD, SearchFields.getPropertyField(geoProperty));
	q.set(SpatialParams.POINT, p.getY() + "," + p.getX());
	q.set(SpatialParams.DISTANCE, Double.toString(kms));
	q.addField(SearchFields.URI_FIELD_NAME);
	// ':' is part of the fl parameter syntax so we can't use the full
	// property field name
	// instead we use wildcard + local part of the property URI
	q.addField("*" + geoProperty.getLocalName());
	// always include the distance - needed for sanity checking
	q.addField(DISTANCE_FIELD + ":geodist()");
	boolean requireContext = (contextVar != null && !contextVar.hasValue());
	if (requireContext) {
		q.addField(SearchFields.CONTEXT_FIELD_NAME);
	}

	QueryResponse response;
	try {
		response = search(q);
	} catch (SolrServerException e) {
		throw new IOException(e);
	}

	SolrDocumentList results = response.getResults();
	return Iterables.transform(results, (SolrDocument document) -> {
		SolrSearchDocument doc = new SolrSearchDocument(document);
		return new SolrDocumentDistance(doc, units);
	});
}
 
Example #11
Source File: LocationGroupingHandler.java    From solr-autocomplete with Apache License 2.0 4 votes vote down vote up
public LocationGroupingHandler(String fieldName, String groupName, SolrParams additionalComponentParams) {
  super(fieldName, groupName, additionalComponentParams);
  fq = getAdditionalComponentParams().get(CommonParams.FQ);
  distance = getAdditionalComponentParams().get(SpatialParams.DISTANCE);
  sfield = getAdditionalComponentParams().get(SpatialParams.FIELD);
}
 
Example #12
Source File: SpatialFilterQParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse() throws SyntaxError {
  //if more than one, we need to treat them as a point...
  //TODO: Should we accept multiple fields
  String[] fields = localParams.getParams("f");
  if (fields == null || fields.length == 0) {
    String field = getParam(SpatialParams.FIELD);
    if (field == null)
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, " missing sfield for spatial request");
    fields = new String[] {field};
  }
  
  String pointStr = getParam(SpatialParams.POINT);
  if (pointStr == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, SpatialParams.POINT + " missing.");
  }

  double dist = -1;
  String distS = getParam(SpatialParams.DISTANCE);
  if (distS != null) dist = Double.parseDouble(distS);

  if (dist < 0) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, SpatialParams.DISTANCE + " must be >= 0");
  }

  String measStr = localParams.get(SpatialParams.MEASURE);
  //TODO: Need to do something with Measures
  Query result = null;
  //fields is valid at this point
  if (fields.length == 1) {
    SchemaField sf = req.getSchema().getField(fields[0]);
    FieldType type = sf.getType();

    if (type instanceof SpatialQueryable) {
      SpatialQueryable queryable = ((SpatialQueryable)type);
      double radius = localParams.getDouble(SpatialParams.SPHERE_RADIUS, queryable.getSphereRadius());
      SpatialOptions opts = new SpatialOptions(pointStr, dist, sf, measStr, radius);
      opts.bbox = bbox;
      result = queryable.createSpatialQuery(this, opts);
    } else {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The field " + fields[0]
              + " does not support spatial filtering");
    }
  } else {// fields.length > 1
    //TODO: Not sure about this just yet, is there a way to delegate, or do we just have a helper class?
    //Seems like we could just use FunctionQuery, but then what about scoring
    /*List<ValueSource> sources = new ArrayList<ValueSource>(fields.length);
    for (String field : fields) {
      SchemaField sf = schema.getField(field);
      sources.add(sf.getType().getValueSource(sf, this));
    }
    MultiValueSource vs = new VectorValueSource(sources);
    ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, "0", String.valueOf(dist), true, true);
    result = new SolrConstantScoreQuery(rf);*/
  }

  return result;
}