標籤:solr lucence
說明:solr 是一個非常完善的開源項目,非常優秀,他不緊緊是做個索引這麼簡單,而是可以做成一個垂直的網站,比如團購網站,快速搭建頁面。而且solr帶了一個非常優秀的管理後台。可以查看管理,匯入資料,重建索引,同步主從資料,非常強大。這裡主要展示的是solr 可以快速搭建一個團購網站,並且將代碼做成demo放到github上面供大家學習參考。代碼修改的比較倉促還在不斷完善。
1,環境搭建,使用solr4.10 版本
從apache 官方網站下載最新的 solr 4.10.0 http://lucene.apache.org/solr/
團購資料從 hao123 的官方網站介面找到的,大家都可以獲得,公開的資料。
http://www.hao123.com/redian/api.htm 美團資料下載:http://www.meituan.com/api/deals/hao123 一共 1.27G 非常大,因為是沒有壓縮的xml資料,(使用htttp下載,用工具可能會被封。)。
將工程修改成maven的工程,因為不是開發solr而是使用solr,在example下面有個war包,解壓縮到maven工程。
2,配置solr下載的資料是xml的格式:
<urlset> <url> <loc>http://bj.meituan.com/deal/6330826.html?source=hao123</loc> <data> <display> <website>美團網</website> <siteurl>http://bj.meituan.com</siteurl> <city>北京</city> <sort>休閑娛樂</sort> <title>【2店通用】CGV星星國際影城單人電影票1張,2D/3D可兌</title> <image>http://p1.meituan.net/275.168/deal/201301/04/173144_2860489.jpg</image> <startTime>1373040000</startTime> <endTime>1377943200</endTime> <value>100</value> <price>30</price> <rebate>3折</rebate> <bought>13593</bought> <spend_start_time>1373040000</spend_start_time> <spend_close_time>1377964799</spend_close_time> <longitude>116.490591</longitude> <latitude>39.970472</latitude> <collections>0</collections> <type>2</type> <soldout>no</soldout> </display> </data> </url>....</urlset>
工程目錄:
在工程根目錄下面 有個solr檔案夾,下面是solr的設定檔,其中一個子檔案夾,tuan是團購的設定檔夾。
工程是用mave搭建的,擴充性更強。
根據美團的資料,將配置solr的資料檔案。
參考:
http://forchenyun.iteye.com/blog/650372
配置 schema.xml
<?xml version="1.0" encoding="UTF-8" ?><schema name="example" version="1.5"><fields><field name="id" type="string" indexed="true" stored="true"required="true" multiValued="false" /><field name="title" type="text_general" indexed="true" stored="true" /><field name="image" type="string" indexed="false" stored="true" /><field name="value" type="double" indexed="false" stored="true" /><field name="price" type="double" indexed="true" stored="true" /><field name="rebate" type="double" indexed="true" stored="true" /><field name="bought" type="long" indexed="true" stored="true" /><field name="city" type="string" indexed="true" stored="true" /><field name="sort" type="string" indexed="true" stored="true" /><field name="loc" type="string" indexed="true" stored="true" /><field name="startTime" type="date" indexed="true" stored="true" /><field name="endTime" type="date" indexed="true" stored="true" /><!-- catchall field, containing all other searchable text fields (implemented via copyField further on in this schema --><field name="text" type="text_general" indexed="true" stored="false"multiValued="true" /><field name="_version_" type="long" indexed="true" stored="true" /></fields><uniqueKey>id</uniqueKey><copyField source="title" dest="text" /><types><fieldType name="string" class="solr.StrField"sortMissingLast="true" /><!-- boolean type: "true" or "false" --><fieldType name="boolean" class="solr.BoolField"sortMissingLast="true" /><fieldType name="int" class="solr.TrieIntField"precisionStep="0" positionIncrementGap="0" /><fieldType name="float" class="solr.TrieFloatField"precisionStep="0" positionIncrementGap="0" /><fieldType name="long" class="solr.TrieLongField"precisionStep="0" positionIncrementGap="0" /><fieldType name="double" class="solr.TrieDoubleField"precisionStep="0" positionIncrementGap="0" /><fieldType name="tint" class="solr.TrieIntField"precisionStep="8" positionIncrementGap="0" /><fieldType name="tfloat" class="solr.TrieFloatField"precisionStep="8" positionIncrementGap="0" /><fieldType name="tlong" class="solr.TrieLongField"precisionStep="8" positionIncrementGap="0" /><fieldType name="tdouble" class="solr.TrieDoubleField"precisionStep="8" positionIncrementGap="0" /><fieldType name="date" class="solr.TrieDateField"precisionStep="0" positionIncrementGap="0" /><fieldType name="tdate" class="solr.TrieDateField"precisionStep="6" positionIncrementGap="0" /><fieldtype name="binary" class="solr.BinaryField" /><fieldType name="pint" class="solr.IntField" /><fieldType name="plong" class="solr.LongField" /><fieldType name="pfloat" class="solr.FloatField" /><fieldType name="pdouble" class="solr.DoubleField" /><fieldType name="pdate" class="solr.DateField"sortMissingLast="true" /><fieldType name="random" class="solr.RandomSortField"indexed="true" /><fieldType name="text_ws" class="solr.TextField"positionIncrementGap="100"><analyzer><tokenizer class="solr.WhitespaceTokenizerFactory" /></analyzer></fieldType><fieldType name="text_general" 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><!-- CJK bigram (see text_ja for a Japanese configuration using morphological analysis) --><fieldType name="text_cjk" class="solr.TextField"positionIncrementGap="100"><analyzer><tokenizer class="solr.StandardTokenizerFactory" /><!-- normalize width before bigram, as e.g. half-width dakuten combine --><filter class="solr.CJKWidthFilterFactory" /><!-- for any non-CJK --><filter class="solr.LowerCaseFilterFactory" /><filter class="solr.CJKBigramFilterFactory" /></analyzer></fieldType></types></schema>
配置:solrconfig.xml
<!-- 增加xml 資料匯入 --> <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">xml-data-config.xml</str> </lst> </requestHandler> <requestHandler name="/browse" class="solr.SearchHandler"> <lst name="defaults"> <str name="echoParams">explicit</str> <!-- VelocityResponseWriter settings --> <str name="wt">velocity</str> <str name="v.properties">velocity.properties</str> <str name="v.contentType">text/html;charset=utf-8</str> <str name="v.template">browse</str> <str name="v.layout">layout</str> <str name="title">團購網站demo</str> <!-- Query settings --> <str name="defType">edismax</str> <str name="df">text</str> <str name="mm">100%</str> <str name="q.alt">*:*</str> <str name="rows">32</str> <str name="fl">*,score</str> <!-- Faceting defaults --> <str name="facet">on</str> <str name="facet.field">city</str> <str name="facet.field">sort</str> <str name="facet.range">price</str> <int name="f.price.facet.range.start">100</int> <int name="f.price.facet.range.end">1500</int> <int name="f.price.facet.range.gap">200</int> <!-- Highlighting defaults --> <str name="hl">on</str> <str name="hl.fl">title</str> <str name="hl.encoder">html</str> <str name="hl.simple.pre"><font colr='red' ></str> <str name="hl.simple.post"></font></str> <str name="f.title.hl.fragsize">0</str> <str name="f.title.hl.alternateField">title</str> <!-- Spell checking defaults --> <str name="spellcheck">on</str> <str name="spellcheck.extendedResults">false</str> <str name="spellcheck.count">5</str> <str name="spellcheck.alternativeTermCount">2</str> <str name="spellcheck.maxResultsForSuggest">5</str> <str name="spellcheck.collate">true</str> <str name="spellcheck.collateExtendedResults">true</str> <str name="spellcheck.maxCollationTries">5</str> <str name="spellcheck.maxCollations">3</str> </lst> <!-- append spellchecking to our list of components --> <arr name="last-components"> <str>spellcheck</str> </arr> </requestHandler>
配置:xml-data-import.xml
<dataConfig><script><![CDATA[ function ReplaceLocAddId(row) { var loc_1 = row.get('loc').split('/deal/'); var loc_2 = loc_1[1].split('.html'); var id = loc_2[0]; row.put('id', id); //格式化時間. var sdf = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss'); //開始時間. row.put('startTime', com.demo.tuan.DateUtils.format(row.get('startTime'))); //結束時間. row.put('endTime', com.demo.tuan.DateUtils.format(row.get('endTime'))); //去掉折扣漢字. row.put('rebate', row.get('rebate').replace('折','')); return row; } ]]></script><dataSource type="FileDataSource" encoding="utf-8" /><document><entity name="tuan" pk="loc"url="/data/workspace.freewebsys/solr4_demo/doc/meituan_hao123.xml"processor="XPathEntityProcessor" forEach="/urlset/url"transformer="script:ReplaceLocAddId,DateFormatTransformer"><field column="loc" xpath="/urlset/url/loc" commonField="true" /><field column="city" xpath="/urlset/url/data/display/city"commonField="true" /><field column="sort" xpath="/urlset/url/data/display/sort"commonField="true" /><field column="title" xpath="/urlset/url/data/display/title"commonField="true" /><field column="image" xpath="/urlset/url/data/display/image"commonField="true" /><field column="value" xpath="/urlset/url/data/display/value"commonField="true" /><field column="price" xpath="/urlset/url/data/display/price"commonField="true" /><field column="rebate" xpath="/urlset/url/data/display/rebate"commonField="true" /><field column="bought" xpath="/urlset/url/data/display/bought"commonField="true" /><field column="startTime" xpath="/urlset/url/data/display/startTime"dateTimeFormat="yyyy-MM-dd HH:mm:ss" commonField="true" /><field column="endTime" xpath="/urlset/url/data/display/endTime"dateTimeFormat="yyyy-MM-dd HH:mm:ss" commonField="true" /></entity></document></dataConfig>
配置好了,開始匯入資料:管理後台,使用超級方便。介面也非常漂亮。
倒入資料成功。
3,修改頁面顯示修改樣式:將原始頁面進行修改,套用別的團購網站樣式。資料裡面直接可顯示圖片等資訊。可以使用nginx 直接將 /tuan/browse 代理到線上部署,前端最好再加上一層varnish做頁面緩衝,效能更好。
solr 已經將很多東西整合進來了,比如分類資料,區間分類資料,資料推薦,相關推薦等,非常強大,越研究越有味道。
4,代碼提交:
代碼和配置已經上傳到github上面了:
https://github.com/freewebsys/solr4_demo
包括全部代碼,設定檔,本地就可以直接使用jetty運行。
使用最新 solr4.10 快速開發團購網站等垂直搜尋網站