You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by mitu2009 <mu...@gmail.com> on 2009/06/27 22:08:04 UTC

SpatialQuery for location based search using Lucene

Hi,

My lucene index has got latitude and longitudes fields indexed as follows:

    doc.Add(new Field("latitude", latitude.ToString() , Field.Store.YES,
Field.Index.UN_TOKENIZED));
    
    doc.Add(new Field("longitude", longitude.ToString(), Field.Store.YES,
Field.Index.UN_TOKENIZED));

I want to retrieve a set of documents from this index whose lat and long
values are in a given range.

As you already know, Lat and long could be negative values.
Given this,would the approach mentioned below give correct results or is
there any other way to do this?

   

     Term lowerLatitude = new Term("latitude", bounds.South.ToString() );
                    Term upperLatitude = new Term("latitude",
bounds.North.ToString());
                    RangeQuery latitudeRangeQuery = new
RangeQuery(lowerLatitude, upperLatitude, true);
                    findLocationQuery.Add(latitudeRangeQuery,
BooleanClause.Occur.SHOULD);
        
    
    
                    Term lowerLongitude = new Term("longitude",
bounds.West.ToString());
                    Term upperLongitude = new Term("longitude",
bounds.East.ToString());
                    RangeQuery longitudeRangeQuery = new
RangeQuery(lowerLongitude, upperLongitude, true);
                    findLocationQuery.Add(longitudeRangeQuery,
BooleanClause.Occur.SHOULD);


Also,I wanted to know how Lucene's ConstantScoreRangeQuery is better than
RangeQuery  class.


Am facing another problem in this context:
I've one of the documents in the index with the following 3 cities:

 - Lyons, IL
   
   Oak Brook, IL
   
   San Francisco, CA

If i give input as "Lyons, IL" then this record comes up.
But if i give San Francisco, CA as input, then it does not.

However, if i store the cities for this document as follows:

 - San Francisco, CA 
   
   Lyons, IL
   
   Oak Brook, IL

 and when i give San Francisco, CA  as input, then this record shows in the
search results.

What i want here is that if i type any of the 3 cities in input,I should get
this document in the search results.

Please help me achieve this.

Thanks.







-- 
View this message in context: http://www.nabble.com/SpatialQuery-for-location-based-search-using-Lucene-tp24236232p24236232.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


RE: SpatialQuery for location based search using Lucene

Posted by Uwe Schindler <uw...@thetaphi.de>.
Hi mitu2009,

For numeric searches the not yet released version 2.9 has new possibilities
for numeric searches. You can index int, long, float or double in a special
field type (NumericField instead of Field) and can query it with
NumericRangeQuery/NumericRangeFilter (see
http://hudson.zones.apache.org/hudson/job/Lucene-trunk/javadoc/core/org/apac
he/lucene/search/NumericRangeQuery.html). You can download this unreleased
version from svn or the hudson server (see developers resources on the
homepage, download @
http://hudson.zones.apache.org/hudson/job/Lucene-trunk/).

If you want to work with older versions, consult also this Wiki article:
http://wiki.apache.org/lucene-java/SearchNumericalFields

What you should do with older Lucene versions:
- Pad all your numbers, so they sort alphabetically. E.g. index terms like
1,4,6,9,10,11 would not sort in this order and range queries would not work
(they would sort 1,10,11,4,6,9). So the simpliest is to pad the numbers with
zeros in front (what your example does not seem to do), e.g. by using
DecimalFormat (new DecimalFormat("000.000000")):

> My lucene index has got latitude and longitudes fields indexed as follows:
> 
>     doc.Add(new Field("latitude", latitude.ToString() , Field.Store.YES,
> Field.Index.UN_TOKENIZED));
> 
>     doc.Add(new Field("longitude", longitude.ToString(), Field.Store.YES,
> Field.Index.UN_TOKENIZED));
> 
> I want to retrieve a set of documents from this index whose lat and long
> values are in a given range.
> 
> As you already know, Lat and long could be negative values.

The negative values are a problem (not with NumericRangeQuery for 2.9), but
for latitudes longitudes the solution is easy. Just add a constant offset to
all numbers (e.g. 90 or 180). Then they are positive and can be easily
queried (when padding with 0 is on). On the query side you have to add the
offset, too.

[...]

> Also,I wanted to know how Lucene's ConstantScoreRangeQuery is better than
> RangeQuery  class.

ConstantScore is better for numeric Ranges, because, if you have too many
different lat/lon values in your field, the non-constant query will fail
with TooManyClausesExceptions.

> Am facing another problem in this context:
> I've one of the documents in the index with the following 3 cities:
> 
>  - Lyons, IL
> 
>    Oak Brook, IL
> 
>    San Francisco, CA
> 
> If i give input as "Lyons, IL" then this record comes up.
> But if i give San Francisco, CA as input, then it does not.
> 
> However, if i store the cities for this document as follows:
> 
>  - San Francisco, CA
> 
>    Lyons, IL
> 
>    Oak Brook, IL
> 
>  and when i give San Francisco, CA  as input, then this record shows in
> the
> search results.
> 
> What i want here is that if i type any of the 3 cities in input,I should
> get
> this document in the search results.


The question: How did you index those values? If you have done it with
UN_TOKENIZED you must create a TermQuery with the exact text (because the
whole name is the term) in correct case and spelling. For full text engines
to work correctly, you must choose the correct anaylzer to create tokens
from your indexed text and the query parser.

Uwe


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: SpatialQuery for location based search using Lucene

Posted by Marcelo Ochoa <ma...@gmail.com>.
Hi:
  Did you know Local Lucene extension?
http://sourceforge.net/projects/locallucene/
  Some test are similar to your example:
http://locallucene.svn.sourceforge.net/viewvc/locallucene/trunk/locallucene/src/java/com/pjaol/search/test/UnitTests/
  Best regards, Marcelo.

On Sat, Jun 27, 2009 at 5:08 PM, mitu2009<mu...@gmail.com> wrote:
>
> Hi,
>
> My lucene index has got latitude and longitudes fields indexed as follows:
>
>    doc.Add(new Field("latitude", latitude.ToString() , Field.Store.YES,
> Field.Index.UN_TOKENIZED));
>
>    doc.Add(new Field("longitude", longitude.ToString(), Field.Store.YES,
> Field.Index.UN_TOKENIZED));
>
> I want to retrieve a set of documents from this index whose lat and long
> values are in a given range.
>
> As you already know, Lat and long could be negative values.
> Given this,would the approach mentioned below give correct results or is
> there any other way to do this?
>
>
>
>     Term lowerLatitude = new Term("latitude", bounds.South.ToString() );
>                    Term upperLatitude = new Term("latitude",
> bounds.North.ToString());
>                    RangeQuery latitudeRangeQuery = new
> RangeQuery(lowerLatitude, upperLatitude, true);
>                    findLocationQuery.Add(latitudeRangeQuery,
> BooleanClause.Occur.SHOULD);
>
>
>
>                    Term lowerLongitude = new Term("longitude",
> bounds.West.ToString());
>                    Term upperLongitude = new Term("longitude",
> bounds.East.ToString());
>                    RangeQuery longitudeRangeQuery = new
> RangeQuery(lowerLongitude, upperLongitude, true);
>                    findLocationQuery.Add(longitudeRangeQuery,
> BooleanClause.Occur.SHOULD);
>
>
> Also,I wanted to know how Lucene's ConstantScoreRangeQuery is better than
> RangeQuery  class.
>
>
> Am facing another problem in this context:
> I've one of the documents in the index with the following 3 cities:
>
>  - Lyons, IL
>
>   Oak Brook, IL
>
>   San Francisco, CA
>
> If i give input as "Lyons, IL" then this record comes up.
> But if i give San Francisco, CA as input, then it does not.
>
> However, if i store the cities for this document as follows:
>
>  - San Francisco, CA
>
>   Lyons, IL
>
>   Oak Brook, IL
>
>  and when i give San Francisco, CA  as input, then this record shows in the
> search results.
>
> What i want here is that if i type any of the 3 cities in input,I should get
> this document in the search results.
>
> Please help me achieve this.
>
> Thanks.
>
>
>
>
>
>
>
> --
> View this message in context: http://www.nabble.com/SpatialQuery-for-location-based-search-using-Lucene-tp24236232p24236232.html
> Sent from the Lucene - Java Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>



-- 
Marcelo F. Ochoa
http://marceloochoa.blogspot.com/
http://marcelo.ochoa.googlepages.com/home
______________
Want to integrate Lucene and Oracle?
http://marceloochoa.blogspot.com/2007/09/running-lucene-inside-your-oracle-jvm.html
Is Oracle 11g REST ready?
http://marceloochoa.blogspot.com/2008/02/is-oracle-11g-rest-ready.html

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org