Correctly indexing latitude and longitude values in Lucene

故事扮演 提交于 2019-12-04 12:32:52

问题


Am working on a "US based nearest city search within a given radius" functionality using Lucene API. Am indexing city's lat and long values in Lucene as follows:

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

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

Since Lucene only understands strings and not numbers, am padding lat and long values.

For example, if original lat and long are 41.811846 and -87.820628 respectively, after padding,values look like:

paddedLatitude -->"0041.811846" and paddedLongitude-->"-087.820628"

Am doing the same padding while building the nearest city query(using Lucene's ConstantScoreRangeQuery class).

Given the fact that lat and long values could be decimal/negative numbers, is this the right approach to index them so that I would get correct nearest cities in the search results when lucene would perform a number Range/comparison operation on these values?

Thanks.


回答1:


Here's the bleeding edge about Searching Numerical Fields in Lucene by Uwe Schindler, the expert on the subject. You may need to use the older (and slower) ConstantScoreRangeQuery because Lucene.net is a bit behind Lucene, and the class NumericRangeQuery described in the link was not yet released in Java Lucene.




回答2:


The linked article in Yuval F's answer made me realize I was wrong in an earlier answer, which you seem to be relying on.

You shouldn't index negative numbers as is, especially in this case, where some of the values are negative and some are positive.

This article seems to have a pretty good discussion of spatial search. He uses some transformations to make all the values positive, and he also touches on other subjects you should probably be aware of, like distance calculations.

One thing to remember if you're encoding the values is to encode them both for the indexing and when building the query.



来源:https://stackoverflow.com/questions/1055175/correctly-indexing-latitude-and-longitude-values-in-lucene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!