现在移动开发中越来越多的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,内容如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <solrpersistent="false">
- <coresadminPath="/admin/cores">
- <corename="core0"instanceDir="core0"/>
- <!--<core name="core1" instanceDir="core1" />-->
- </cores>
- </solr>
2.4 在D:\hotel_solr\core0\conf目录下,新建schema.xml文件,内容如下:
- <?xmlversion="1.0"?>
- <schemaname="example core zero"version="1.1">
- <types>
- <fieldTypename="string"class="solr.StrField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="boolean"class="solr.BoolField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="integer"class="solr.IntField"omitNorms="true"/>
- <fieldTypename="int"class="solr.TrieIntField"precisionStep="0"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="float"class="solr.TrieFloatField"precisionStep="0"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="long"class="solr.TrieLongField"precisionStep="0"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="double"class="solr.TrieDoubleField"precisionStep="0"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="tint"class="solr.TrieIntField"precisionStep="8"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="tfloat"class="solr.TrieFloatField"precisionStep="8"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="tlong"class="solr.TrieLongField"precisionStep="8"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="tdouble"class="solr.TrieDoubleField"precisionStep="8"omitNorms="true"positionIncrementGap="0"/>
- <fieldTypename="sint"class="solr.SortableIntField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="slong"class="solr.SortableLongField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="sfloat"class="solr.SortableFloatField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="sdouble"class="solr.SortableDoubleField"sortMissingLast="true"omitNorms="true"/>
- <fieldTypename="date"class="solr.TrieDateField"omitNorms="true"precisionStep="0"positionIncrementGap="0"/>
- <fieldTypename="tdate"class="solr.TrieDateField"omitNorms="true"precisionStep="6"positionIncrementGap="0"/>
- <fieldtypename="ignored"stored="false"indexed="false"class="solr.StrField"/>
- <fieldtypename="location"class="solr.LatLonType"subFieldSuffix="_d"/>
- <fieldTypename="text"class="solr.TextField"positionIncrementGap="100">
- <analyzertype="index">
- <tokenizerclass="solr.StandardTokenizerFactory"/>
- <filterclass="solr.LowerCaseFilterFactory"/>
- </analyzer>
- <analyzertype="query">
- <tokenizerclass="solr.StandardTokenizerFactory"/>
- <filterclass="solr.LowerCaseFilterFactory"/>
- </analyzer>
- </fieldType>
- </types>
- <fields>
- <fieldname="hotel_id"type="integer"indexed="true"stored="true"required="true"/>
- <fieldname="crawl_hotel_id" type="string" indexed="true" stored="true" multiValued="false"required="true"/>
- <fieldname="name" type="text" indexed="true" stored="true" multiValued="false"required="true"/>
- <fieldname="address" type="text" indexed="true" stored="true" multiValued="false"required="true"/>
- <fieldname="search_kw"type="text"indexed="true"multiValued="true"stored="false"/>
- <fieldname="latlng"type="location"indexed="true"/>
- <dynamicFieldname="*_d"type="double"indexed="true"stored="true"/>
- </fields>
- <copyFieldsource="name"dest="search_kw"/>
- <copyFieldsource="address"dest="search_kw"/>
- <uniqueKey>hotel_id</uniqueKey>
- <defaultSearchField>search_kw</defaultSearchField>
- <solrQueryParserdefaultOperator="AND"/>
- </schema>
- <?xmlversion="1.0"encoding="UTF-8"?>
- <config>
- <luceneMatchVersion>LUCENE_36</luceneMatchVersion>
- <directoryFactoryname="DirectoryFactory"class="${solr.directoryFactory:solr.StandardDirectoryFactory}"/>
- <updateHandlerclass="solr.DirectUpdateHandler2"/>
- <requestDispatcherhandleSelect="true">
- <requestParsersenableRemoteStreaming="false"multipartUploadLimitInKB="2048"/>
- </requestDispatcher>
- <requestHandlername="standard"class="solr.StandardRequestHandler"default="true"/>
- <requestHandlername="/update"class="solr.XmlUpdateRequestHandler"/>
- <requestHandlername="/admin/"class="org.apache.solr.handler.admin.AdminHandlers"/>
- <admin>
- <defaultQuery>*:*</defaultQuery>
- </admin>
- </config>
3 配置tomcat
3.1 让tomcat支持HTTP GET UTF-8编码的支持, 打开D:\apache-tomcat-7.0.16\conf\server.xml,修成如下地方:
- <Connectorport="8080"protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443"URIEncoding="UTF-8"/>
在D:\apache-tomcat-7.0.16\conf\Catalina\localhost新建solr.xml,内容如下:
- <ContextdocBase="F:/apache-solr-3.6.2.war"debug="0"crossContext="true">
- <Environmentname="solr/home"type="java.lang.String"value="D:/hotel_solr"override="true"/>
- </Context>