现在移动开发中越来越多的App都有周边搜索,有找附近的人的,附近的酒店,附近的餐馆的。接下来我们就来讲一下强大的Solr来帮我们构建周边搜索之酒店搜索。
1 下载 Solr 3.6.2
下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/3.6.2
提取apache-solr-3.6.2.zip里的apache-solr-3.6.2.war到F:\, 后面会讲解如何部署这个war包
2 建立solr的索引库配置
2.1 在D:\建立目录hotel_solr
2.2 在D:\hotel_solr下新建solr.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <solr persistent="false"> <cores adminPath="/admin/cores"> <core name="core0" instanceDir="core0" /> <!--<core name="core1" instanceDir="core1" />--> </cores> </solr>2.3 在D:\hotel_solr新建core0\conf目录
2.4 在D:\hotel_solr\core0\conf目录下,新建schema.xml文件,内容如下:
<?xml version="1.0" ?> <schema name="example core zero" 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.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType> </types> <fields> <field name="hotel_id" type="integer" indexed="true" stored="true" required="true"/> <field name="crawl_hotel_id" type="string" indexed="true" stored="true" multiValued="false" required="true"/> <field name="name" type="text" indexed="true" stored="true" multiValued="false" required="true"/> <field name="address" type="text" indexed="true" stored="true" multiValued="false" required="true"/> <field name="search_kw" type="text" indexed="true" multiValued="true" stored="false" /> <field name="latlng" type="location" indexed="true" /> <dynamicField name="*_d" type="double" indexed="true" stored="true"/> </fields> <copyField source="name" dest="search_kw" /> <copyField source="address" dest="search_kw" /> <uniqueKey>hotel_id</uniqueKey> <defaultSearchField>search_kw</defaultSearchField> <solrQueryParser defaultOperator="AND"/> </schema>2.5 在D:\hotel_solr\core0\conf目录下,新建solrconfig.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <config> <luceneMatchVersion>LUCENE_36</luceneMatchVersion> <directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.StandardDirectoryFactory}"/> <updateHandler class="solr.DirectUpdateHandler2" /> <requestDispatcher handleSelect="true" > <requestParsers enableRemoteStreaming="false" multipartUploadLimitInKB="2048" /> </requestDispatcher> <requestHandler name="standard" class="solr.StandardRequestHandler" default="true" /> <requestHandler name="/update" class="solr.XmlUpdateRequestHandler" /> <requestHandler name="/admin/" class="org.apache.solr.handler.admin.AdminHandlers" /> <admin> <defaultQuery>*:*</defaultQuery> </admin> </config>到这里为止Solr的索引配置完成.
3 配置tomcat
3.1 让tomcat支持HTTP GET UTF-8编码的支持, 打开D:\apache-tomcat-7.0.16\conf\server.xml,修成如下地方:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>3.2 配置solr的war包
在D:\apache-tomcat-7.0.16\conf\Catalina\localhost新建solr.xml,内容如下:
<Context docBase="F:/apache-solr-3.6.2.war" debug="0" crossContext="true" > <Environment name="solr/home" type="java.lang.String" value="D:/hotel_solr" override="true" /> </Context>3.3 启动tomcat, 在浏览器中输入: http://localhost:8080/solr/ 如果出现 Admin core0 表示正常。