Return-Path: Delivered-To: apmail-lucene-solr-commits-archive@minotaur.apache.org Received: (qmail 9509 invoked from network); 15 Feb 2011 12:38:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Feb 2011 12:38:34 -0000 Received: (qmail 94023 invoked by uid 500); 15 Feb 2011 12:38:34 -0000 Delivered-To: apmail-lucene-solr-commits-archive@lucene.apache.org Received: (qmail 93896 invoked by uid 500); 15 Feb 2011 12:38:32 -0000 Mailing-List: contact solr-commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: solr-dev@lucene.apache.org Delivered-To: mailing list solr-commits@lucene.apache.org Received: (qmail 93889 invoked by uid 99); 15 Feb 2011 12:38:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Feb 2011 12:38:31 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.131] (HELO eos.apache.org) (140.211.11.131) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Feb 2011 12:38:30 +0000 Received: from eosnew.apache.org (localhost [127.0.0.1]) by eos.apache.org (Postfix) with ESMTP id 2D593169 for ; Tue, 15 Feb 2011 12:38:00 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Apache Wiki To: Apache Wiki Date: Tue, 15 Feb 2011 12:37:58 -0000 Message-ID: <20110215123758.11647.73227@eosnew.apache.org> Subject: =?utf-8?q?=5BSolr_Wiki=5D_Update_of_=22SpatialSearch=22_by_GrantIngersoll?= Dear Wiki user, You have subscribed to a wiki page or wiki category on "Solr Wiki" for chan= ge notification. The "SpatialSearch" page has been changed by GrantIngersoll. http://wiki.apache.org/solr/SpatialSearch?action=3Ddiff&rev1=3D53&rev2=3D54 -------------------------------------------------- = Since the !LatLonType field also supports field queries and range queries= , so one can manually create their own bounding box rather than using bbox: = - . [[http://localhost:8983/solr/select?wt=3Djson&indent=3Dtrue&fl=3Dname,= store&q=3D*:*&fq=3Dstore:[45,-94%20TO%2046,-93]|...&q=3D*:*&fq=3Dstore:[45,= -94 TO 46,-93] ]] + . [[http://localhost:8983/solr/select?wt=3Djson&indent=3Dtrue&fl=3Dname,= store&q=3D*:*&fq=3Dstore:[45,-94%20TO%2046,-93]|...&q=3D*:*&fq=3Dstore:[45,= -94 TO 46,-93]]] = =3D=3D geodist - The distance function =3D=3D '''geodist''' is a function query that yields the calculated distance. T= his gives the flexibility to do a number of interesting things, such as sor= ting by the distance (Solr can sort by any function query), or combining th= e distance with the relevancy score, such as boosting by the inverse of the= distance. @@ -87, +87 @@ =3D=3D How to combine with a sub-query to expand results =3D=3D It is possible to filter by other criteria with an OR clause. Here is an = example that says return by Jacksonville, FL or within 50 km from 45.15,-93= .85: = - . [[http://localhost:8983/solr/select?fl=3Dname,store,city,state&q=3D*:*= &fq=3D(state:"FL" AND city:"Jacksonville") OR _query_:"{!geofilt}"&sfield= =3Dstore&pt=3D45.15,-93.85&d=3D50&sort=3Dgeodist()%20asc|...&q=3D*:*&fq=3D(= state:"FL" AND city:"Jacksonville") OR _query_:"{!geofilt}"&sfield=3Dstore&= pt=3D45.15,-93.85&d=3D50&sort=3Dgeodist() asc]] + . [[http://localhost:8983/solr/select?fl=3Dname,store,city,state&q=3D*:*= &fq=3D(state:"FL"%20AND%20city:"Jacksonville")%20OR%20_query_:"{!geofilt}"&= sfield=3Dstore&pt=3D45.15,-93.85&d=3D50&sort=3Dgeodist()%20asc|...&q=3D*:*&= fq=3D(state:"FL" AND city:"Jacksonville") OR _query_:"{!geofilt}"&sfield=3D= store&pt=3D45.15,-93.85&d=3D50&sort=3Dgeodist() asc]] + = + =3D=3D How to facet by distance =3D=3D + Faceting by distance can be done using the frange QParser. Unfortunately= , right now, it is a bit inefficient, but it likely will be fine in most si= tuations: + = + . [[http://localhost:8983/solr/select?&q=3D*:*&sfield=3Dstore&pt=3D45.15= ,-93.85&facet.query=3D{!frange%20l=3D0%20u=3D5}geodist%28%29&facet.query=3D= {!frange%20l=3D5.001%20u=3D3000}geodist%28%29&wt=3Dxml&facet=3Dtrue|&q=3D*:= *&sfield=3Dstore&pt=3D45.15,-93.85&facet.query=3D{!frange l=3D0 u=3D5}geodi= st()&facet.query=3D{!frange l=3D5.001 u=3D3000}geodist()]] = =3D=3D How to boost closest results (with dismax) =3D=3D It is possible also boost the query by closest results by combining bq wi= th geodist(): = - . [[http://localhost:8983/solr/select?fl=3Dname,store,score&defType=3Ddi= smax&q.alt=3D*:*&fq=3D{!geofilt}&sfield=3Dstore&pt=3D45.15,-93.85&d=3D50&bq= =3D_val_:"recip(geodist(), 2, 200, 20)"^1.0&sort=3Dscore%20desc|...&defType= =3Ddismax&q.alt=3D*:*&fq=3D{!geofilt}&sfield=3Dstore&pt=3D45.15,-93.85&d=3D= 50&bq=3D_val_:"recip(geodist(), 2, 200, 20)"^1.0&sort=3Dscore desc]] + . [[http://localhost:8983/solr/select?fl=3Dname,store,score&defType=3Ddi= smax&q.alt=3D*:*&fq=3D{!geofilt}&sfield=3Dstore&pt=3D45.15,-93.85&d=3D50&bq= =3D_val_:"recip(geodist(),%202,%20200,%2020)"^1.0&sort=3Dscore%20desc|...&d= efType=3Ddismax&q.alt=3D*:*&fq=3D{!geofilt}&sfield=3Dstore&pt=3D45.15,-93.8= 5&d=3D50&bq=3D_val_:"recip(geodist(), 2, 200, 20)"^1.0&sort=3Dscore desc]] + = + =3D Advanced Spatial Options =3D + ''''''Solr also supports other spatial capabilities beyond just latitude = and longitude. For example, a PointType can be used to represent a point i= n an n-dimensional space. This can be useful, for instance, for searching = in CAD drawings or blueprints. Solr also supports other distance measures.= See the FunctionQuery page for more information and look for hsin, ghhsin= and others. + = + =3D=3D Field Types =3D=3D + =3D=3D=3D PointType =3D=3D=3D + {{{ + + }}} + =3D=3D=3D LatLonType =3D=3D=3D + The !LatLonType combines a latitude/longitude point. All input is interp= reted as latitude then longitude. The LatLonType is similar to !PointType,= but it does distance calculations based on Great Circle (haversine) and is= only two dimensional (lat/lon). + = + =3D=3D=3D=3D Example =3D=3D=3D=3D + {{{ + + ... + + }}} + =3D=3D=3D Geohash =3D=3D=3D + A geohash is a way of encoding lat/lon into a single field as a String. = As of https://issues.apache.org/jira/browse/SOLR-1586, it will be possible = to create a geohash via FieldType, simply by passing in a Point (lat,lon). = Solr will do the work of converting the point to a geohash. + = + See http://www.geohash.org and http://en.wikipedia.org/wiki/Geohash + = + If you need to index a multi-valued point field, say because you have a v= ariable number of points per document, then check out https://issues.apache= .org/jira/browse/SOLR-2155 which uses a hierarchical grid geohash prefix te= chnique to efficiently filter documents by a geographic shape. + = + =3D=3D=3D=3D Example =3D=3D=3D=3D + {{{ + + ... + + }}} + =3D Indexing =3D + Indexing is handled by the various !FieldType instances in the schema. = At the most basic, the user can represent their own spatial data using int= s, floats or doubles. Beyond that, the !PointType, !GeoHashField and !LatL= onType can be used to index spatial information automatically. + = + When indexing, the format is something like: + = + {{{ + 12.34,-123.45 + }}} + (It can vary based on the number of values. When using a !LatLonType or = a !GeoHashField, it is always latitude, then longitude. + = + =3D Filtering =3D + There are several different ways to filter in spatial search: + = + 1. By Range Query, as in {{{fq=3Dlat:[-23.0 TO -79.5] AND lon:[56.3 TO 6= 0.3]}}} -- Already implemented + 1. By the Spatial Filter QParser (!SpatialQParser) - e.g. {!sfilt fl=3D= location}&pt=3D49.32,-79.0&d=3D20 + 1. Using the "frange" QParser, as in {{{fq=3D{!frange l=3D0 u=3D400}hsin= (0.57, -1.3, lat_rad, lon_rad, 3963.205)}}} + = + In practice, for those using Solr's field types above, the Spatial Filte= r !QParser will automatically make the correct decision about how best to = filter. If an application needs a specific type of filtering for perform= ance or other needs, the best bet is to extend the !FieldType in question w= ith your own needs. + = + =3D=3D Spatial Filter QParser =3D=3D + See https://issues.apache.org/jira/browse/SOLR-1568. + = + ''NOTE: Depending on the !FieldType, different calculations for distance = will be applied''. For instance, the !PointType uses a rectangular coordin= ate system and uses the Euclidean distance while !LatLonType uses Haversine= by default. + = + See !SpatialFilterTest for examples of the various points. + = + The following parameters are supported: + ||Parameter ||Description ||E= xample || + ||pt ||The Point to use as the center of the filter. Specified as a comm= a separated list of doubles. If using the !LatLonType, then it is lat,lon.= ||&pt=3D33.4,29.0 &pt=3D27.3,83.9,10.0,5.5 || + ||d ||The distance from the point to the outer edge of whatever is being= used to filter on (bounding box, pure distance, something else). Must be= greater than or equal to 0 ||&d=3D10.0 || + ||sphere_radius ||The radius of the sphere to be used when calculating d= istances on a sphere (i.e. haversine). Default is the Earth's mean radius= in kilometers (see org.apache.lucene.spatial.DistanceUtils.EARTH_MEAN_RADI= US_KM) which is set to 6371.009. Most applications will not need to set th= is. ||&sphere_radius=3D10.3 || + ||meas ||NOTE: This value is experimental and subject to removal. Most a= pplications will not need to change the measure. The !FieldTypes usually = make the proper choice given the data stored. The distance measure to use = when calculating distance. The default is dependent on the FieldType. Su= pported values are: 1. hsin - The haversine 2. 0, 1, 2, ... INF for the app= ropriate p-norm (2 is the Euclidean Distance) ||&meas=3Dhsin. || = = - =3D Advanced Spatial Options - Under Development =3D - SpatialSearchDev -- Covers things like Geohash (supports multivalue lat-= lon points), North/South Pole issues, other distance functions, etc. = + = + For !LatLonType, the sfilt command calculates a bounding box by calculat= ing the East and West Longitudes and the North and South Latitudes of a bo= x that transcribes the circle with radius d (using hsin). There are other= ways that this can be implemented by overriding the createSpatialQuery m= ethod on !LatLonType. + = + For !PointType, the bounding box is calculated by standard rectangular co= ordinate system measures. + = + =3D=3D Filtering Caveats =3D=3D + =3D=3D=3D North/South Poles =3D=3D=3D + When the bounding box includes a Pole, then the !LatLonType will automati= cally switch from producing a bounding box to a "bounding bowl" (i.e. a sph= erical cap: http://mathworld.wolfram.com/SphericalCap.html) whereby it wil= l include all values that are North or South of the latitude of the would = be bounding box (the lower left and the upper right) that is closer to the= equator. In other words, we still calculate what the coordinates of the = upper right corner and the lower left corner of the box would be just as i= n all other filtering cases, but we then take the corner that is closest t= o the equator (since it goes over the pole it may not be the lower left, d= espite the name) and do a latitude only filter. Obviously, this means the= re will be more matches than a pure bounding box match, but the query is m= uch easier to construct and will likely be faster, too. + = + =3D Sorting =3D + https://issues.apache.org/jira/browse/SOLR-1297 added the ability to sor= t by function, so sorting by distance is now simply a matter of sorting us= ing the appropriate distance function, just like boosting. + = + =3D Scoring =3D + Scoring by distance works just like any other FunctionQuery. See the dis= tance methods on the FunctionQuery page for examples and method signatures. + = + =3D Query Parsing =3D + TODO + = + https://issues.apache.org/jira/browse/SOLR-1578 See https://issues.apache= .org/jira/browse/SOLR-1568 + = + =3D Other Caveats =3D + Unless otherwise specified, all units are kilometers. + = + =3D Known Issues =3D + See https://issues.apache.org/jira/browse/SOLR-773 for tracking + = + =3D Useful References =3D + 1. http://www.movable-type.co.uk/scripts/latlong.html + 1. http://www.ibm.com/developerworks/opensource/library/j-spatial/index.= html + 1. http://www.nsshutdown.com/projects/lucene/whitepaper/locallucene_v2.h= tml +=20