(转载)solr实现满足指定距离范围条件的搜索
配置schema.xml
<?xml version="1.0" encoding="UTF-8" ?> <schema name="aifang-loupan" version="1.1"> <types> <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/> <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/> <fieldType name="integer" class="solr.IntField" omitNorms="true"/> <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/> <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/> <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/> <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/> <fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/> <fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/> <fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/> <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/> <fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/> <fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/> <fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/> <fieldType name="date" class="solr.TrieDateField" omitNorms="true" precisionStep="0" positionIncrementGap="0"/> <fieldType name="tdate" class="solr.TrieDateField" omitNorms="true" precisionStep="6" positionIncrementGap="0"/> <fieldtype name="ignored" stored="false" indexed="false" class="solr.StrField"/> <fieldtype name="location" class="solr.LatLonType" subFieldSuffix="_d"/> <fieldType name="text" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType> </types> <fields> <field name="loupan_id" type="integer" indexed="true" stored="true" required="true"/> <field name="city_id" type="integer" indexed="true" stored="false"/> <field name="loupan_name" type="text" indexed="true" stored="false"/> <field name="loupan_pinyin" type="string" indexed="true" stored="false"/> <field name="alias_name" type="text" indexed="true" stored="false"/> <field name="former_name" type="text" indexed="true" stored="false"/> <field name="region_title" type="text" indexed="true" stored="false" /> <field name="region_id" type="int" indexed="true" stored="false"/> <field name="sub_region_title" type="text" indexed="true" stored="false"/> <field name="sub_region_id" type="int" indexed="true" stored="false"/> <field name="regions_string" type="string" indexed="true" stored="false" multiValued="true"/> <field name="address" type="text" indexed="true" stored="false"/> <field name="proxy_address" type="text" indexed="true" stored="false"/> <field name="chanquan" type="text" indexed="true" stored="false"/> <field name="build_type" type="text" indexed="true" stored="false" multiValued="true"/> <field name="property_type" type="text" indexed="true" stored="false" multiValued="true"/> <field name="fitment_type" type="text" indexed="true" stored="false" multiValued="true"/> <field name="search_kw" type="text" indexed="true" multiValued="true" stored="false" /> <field name="investor" type="text" indexed="true" stored="false"/> <field name="company" type="text" indexed="true" stored="false"/> <field name="loop_line" type="text" indexed="true" stored="false" /> <field name="status_sale" type="integer" indexed="true" stored="false"/> <field name="status_disp" type="integer" indexed="true" stored="false"/> <field name="search_sale" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="jiaofang_date" type="sint" indexed="true" stored="false"/> <field name="kaipan_date" type="sint" indexed="true" stored="false"/> <field name="price" type="sint" index="true" stored="false"/> <field name="baidu_lat" type="sfloat" indexed="true" stored="false"/> <field name="baidu_lng" type="sfloat" indexed="true" stored="false"/> <field name="lat" type="sfloat" indexed="true" stored="false"/> <field name="lng" type="sfloat" indexed="true" stored="false"/> <field name="metro_subway_id" type="integer" indexed="true" stored="false" multiValued="true" /> <field name="metro_station_id" type="integer" indexed="true" stored="false" multiValued="true" /> <field name="metro_enabled" type="integer" indexed="true" stored="false"/> <field name="metro_info" type="text" indexed="true" stored="false" multiValued="true" /> <field name="loop_line_id" type="integer" indexed="true" stored="false"/> <field name="property_type_id" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="build_type_id" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="fitment_type_id" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="developer_id" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="developer_title" type="string" indexed="true" stored="false"/> <field name="business_id" type="integer" indexed="true" stored="false"/> <field name="business_title" type="string" indexed="true" stored="false"/> <field name="loupan_extend_type" type="sint" indexed="true" stored="false" multiValued="true"/> <field name="trip_fitment_style" type="text" indexed="true" stored="false" /> <field name="trip_feature_recommend" type="text" indexed="true" stored="false" multiValued="true" /> <field name="trip_spot" type="text" indexed="true" stored="false" multiValued="true" /> <field name="trip_transport" type="text" indexed="true" stored="false" multiValued="true" /> <field name="trip_scenic_region" type="text" indexed="true" stored="false" multiValued="true" /> <field name="trip_property_service" type="text" indexed="true" stored="false" multiValued="true" /> <field name="trip_scenic_spot" type="text" indexed="true" stored="false"/> <field name="trip_created" type="sint" indexed="true" stored="false" /> <field name="is_trip" type="int" indexed="true" stored="false" /> <field name="level" type="sint" indexed="true" stored="false"/> <field name="loupan_pv_day" type="sint" indexed="true" stored="false" /> <field name="operator_ids" type="integer" indexed="true" stored="false" multiValued="true"/> <field name="operator_names" type="string" indexed="true" stored="false" multiValued="true"/> <dynamicField name="*_grade" type="sint" indexed="true" stored="false" /> <field name="updated" type="sint" indexed="true" stored="false" /> <field name="news_id" type="int" indexed="true" stored="false" /> <field name="news_update" type="sint" indexed="true" stored="false" /> <field name="rank" type="sfloat" indexed="true" stored="false" /> <field name="sale_order" type="sint" indexed="true" stored="false" /> <field name="vip_order" type="string" indexed="true" stored="false" /> <field name="kaipan_order" type="sint" indexed="true" stored="false"/> <field name="created" type="sint" indexed="true" stored="false" /> <field name="latlng" type="location" indexed="true" /> <dynamicField name="*_d" type="double" indexed="true" stored="true"/> </fields> <uniqueKey>loupan_id</uniqueKey> <defaultSearchField>search_kw</defaultSearchField> <solrQueryParser defaultOperator="AND"/> </schema>
在提交数据时的格式为:(纬度,经度:以字符串的方式提交给solr)
<field name="store">45.17614,-93.87341</field> <!-- Buffalo store --> <field name="store">40.7143,-74.006</field> <!-- NYC store --> <field name="store">37.7752,-122.4232</field> <!-- San Francisco store -->
例如要查寻距离纬度经度(31.254982,121.47501)5km的所有满足条件的楼盘:
http://192.168.181.140:8487/solr/loupan/select/?q=city_id:11&fq={!geofilt pt=31.254982,121.47501 sfield=latlng d=5}
查找满足指定距离的结果集,并按照距离由近到远排序
http://192.168.181.140:8487/solr/loupan/select/?q=*:*&fq={!geofilt}&pt=31.2367,121.501&sfield=latlng&d=5&sort=geodist()+asc
参考: