全文搜索引擎技術詳解之Apache Solr的使用

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Solr","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Solr是一個可擴展的,可部署,搜索,存儲引擎,優化搜索大量以文本爲中心的數據庫","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Solr是開源搜索平臺,用於構建搜索應用程序","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"建立在Lucene(全文搜索引擎)之上","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Solr是企業級的,快速的和高度可擴展的,使用Solr構建的應用程序可以提供高性能,但是非常複雜","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Solr可以和Hadoop一起使用:由於Hadoop處理大量數據,Solr可以從大的數據源中找到所需信息.","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Solr不僅限於搜索,也可以用於存儲.和其它NoSQL數據庫一樣,是一種非關係數據存儲和處理技術","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Apache Solr特點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Solr是Lucene的Java API包裝,使用Solr,就可以使用Lucene的所有功能","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"RESTful API:","attrs":{}},{"type":"text","text":" 要與Solr通信,可以使用RESTful服務與Solr通信,可以使用XML,JSON,CSV等格式的文件作爲輸入文檔,並以相同的文件格式獲取結果","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"全文搜索:","attrs":{}},{"type":"text","text":" Solr提供了全文搜索所需的所有功能:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"令牌,短語,拼寫檢查,通配符,自動完成","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"企業準備:","attrs":{}},{"type":"text","text":" 根據企業或組織的需要,Solr可以部署在任何類型的系統:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"獨立,分佈式,雲","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"靈活可擴展:","attrs":{}},{"type":"text","text":" 通過擴展Java類並進行相關配置,可以定製Solr組件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NoSQL數據庫:","attrs":{}},{"type":"text","text":" Solr可以用作大數量級的NoSQL數據庫,可以沿着集羣分佈搜索任務","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"搜索引擎","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"搜索引擎:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索引擎是龐大的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"互聯網資源數據庫","attrs":{}},{"type":"text","text":",如網頁,新聞組,程序,圖像等","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有助於","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在網上定位信息","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"用戶可以通過以關鍵字或短語的形式將查詢傳遞到搜索引擎中來搜索信息,然後搜索引擎搜索其數據庫並向用戶返回相關鏈接","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"搜索引擎組件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索引擎有三個組件:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Web爬蟲:","attrs":{}},{"type":"text","text":" 一個收集網絡信息的軟件組件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫:","attrs":{}},{"type":"text","text":" Web上的所有信息都存儲在數據庫中,包含大量的Web資源","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"搜索接口:","attrs":{}},{"type":"text","text":" 這個組件是用戶和數據庫之間的接口,幫助用戶搜索數據庫","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"搜索引擎工作流程","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"獲取原始內容:","attrs":{}},{"type":"text","text":" 任何搜索應用程序的第一步是收集要進行搜索的目標內容","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"構建文檔:","attrs":{}},{"type":"text","text":" 從原始內容構建文檔,讓搜索應用程序可以很容易的理解和解釋","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分析文檔:","attrs":{}},{"type":"text","text":" 在索引開始之前,將對文檔進行分析","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"索引文檔:","attrs":{}},{"type":"text","text":" 當文檔被構建和分析後,下一步是對文檔建立索引,以便可以基於特定鍵而不是文檔的全部內容來檢索該文檔.索引類似於在書開始頁或末尾處的目錄索引,其中常見單詞以頁碼顯示,使得這些單詞可以快速追蹤,而不是搜索整本書","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"用於搜索的用戶接口:","attrs":{}},{"type":"text","text":" 當索引數據庫就緒,應用程序就可以執行搜索操作.爲了幫助用戶進行搜索,應用必須提供用戶接口,用戶可以在用戶接口中輸入文本並啓動搜索過程","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"構建查詢:","attrs":{}},{"type":"text","text":" 當用戶做出搜索文本的請求,應用程序應該使用該文本準備查詢對象,然後可以使該查詢對象來查詢索引數據庫以獲得相關細節","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"搜索查詢:","attrs":{}},{"type":"text","text":" 使用查詢對象,檢查索引數據庫以獲取相關詳細信息和內容文檔","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"渲染結果:","attrs":{}},{"type":"text","text":" 當收到所需結果,應用程序應決定如何使用用戶界面向用戶顯示搜索結果","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"分詞技術","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分詞技術:","attrs":{}},{"type":"text","text":" 搜索引擎針對用戶提交查詢的關鍵詞串進行的查詢處理後,根據用戶的關鍵詞串用各種匹配方法進行分詞的一種技術","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"中文分詞算法","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"基於字符串匹配","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於字符串匹配:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即掃描字符串,如果發現","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"字符串的子串和詞相同,就算匹配","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這類分詞通常會加入一些啓發式規則:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"正向/反向最大匹配","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"長詞優先","attrs":{}},{"type":"text","text":"等","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於字符串匹配算法優點:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"速度快","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"都是O(n)時間複雜度","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實現簡單","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"效果尚可","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於字符串匹配算法缺點:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對歧義和未登錄詞處理不好","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ikanalyzer,paoding等就是基於字符串匹配的分詞","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"基於統計及機器學習的分詞方式","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於統計及機器學習的分詞方式:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於人工標註的詞性和統計特徵,對中文進行建模.","attrs":{}},{"type":"text","text":" 即根據觀測到的數據(標註好的語料)對模型參數進行估計.即 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"訓練","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在分詞階段再","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"通過模型計算各種分詞出現的概率,將概率最大的分詞結果作爲最終結果","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"常見的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"序列標註模型:HMM,CRF","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於統計及機器學習的分詞方式優點:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以很好地處理歧義和未登錄問題","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"效果比基於字符串匹配算法更好","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基於統計及機器學習的分詞方式缺點:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要大量的人工標註數據","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"較慢的分詞速度","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"IKAnalyzer","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IKAnalyzer是一個開源的,基於Java語言開發的輕量級中文分詞工具包","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基於文本匹配,不需要投入大量的人力進行訓練和標註","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以自定詞典,方便加入特定領域的詞語,能分出多粒度的結果","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"部署Solr並安裝IKAnalyzer","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建/usr/local/docker/solr/ikanalyzer目錄","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"/usr/local/docker/solr 用於存放docker-compose.yml配置文件\n/usr/local/docker/solr/ikanalyzer 用於存放Dockerfile鏡像配置文件\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"docker-compose.yml","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yml"},"content":[{"type":"text","text":"version: '3.1'\nservices:\n solr:\n build: ikanalyzer\n restart: always\n container_name: solr\n ports:\n - 8983:8983\n volumes:\n - ./solrdata:/opt/solrdata\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Dockerfile","attrs":{}},{"type":"text","text":"(在/usr/local/docker/solr/ikanalyzer中需要有文件:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ik-analyzer-solr5-5.x.jar,solr-analyzer-ik-5.1.0.jar,ext.dic,stopword.dic,IKAnalyzer.cfg.xml,managed-schema","attrs":{}},{"type":"text","text":")","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"dockerfile"},"content":[{"type":"text","text":"FROM solr\n\n# 創建Core\nWORKDIR /opt/solr/server/solr\nRUN mkdir ik_core\nWORKDIR /opt/solr/server/solr/ik_core\nRUN echo 'name=ik_core' > core.properties\nRUN mkdir data\nRUN cp -r ../configsets/sample_techproducts_configs/conf/ .\n\n# 安裝中文分詞\nWORKDIR /opt/solr/server/solr-webapp/webapp/WEB-INF/lib\nADD ik-analyzer-solr5-5.x.jar .\nADD solr-analyzer-ik-5.1.0.jar .\nWORKDIR /opt/solr/server/solr-webapp/webapp/WEB-INF\nADD ext.dic .\nADD stopword.dic .\nADD IKAnalyzer.cfg.xml .\n\n# 增加分詞配置\nCOPY managed-schema /opt/solr/server/solr/ik_core/conf\n\nWORKDIR /opt/solr\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"構建鏡像:","attrs":{}},{"type":"text","text":" 在/usr/local/docker/solr中執行命令","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"docker-compose up -d\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"Solr分析功能","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"修改managed-schema配置業務系統字段","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Solr中自帶的相同字段無需再添加,其它字段需要手動添加Solr字段","attrs":{}},{"type":"text","text":"(通過編輯managed-schema配置文件來手動添加Solr字段)","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n\n\n\n\n\n\n\n\n\n\n\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"複製配置到容器","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"docker cp managed-schema solr:/opt/solr/server/solr/ik_core/conf\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"重啓容器","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"docker-compose restart\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Solr的Web界面可以進行CRUD操作","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"SpringBoot整合Solr","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建搜索服務接口","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建myshop-service-search-api項目,該項目只負責定義定義接口","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"pom.xml","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n 4.0.0\n\n \n com.funtl\n myshop-dependencies\n 1.0.0-SNAPSHOT\n ../myshop-dependencies/pom.xml\n \n\n myshop-service-search-api\n jar\n\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在項目中創建SearchService接口","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.api;\n\npublic interface SearchService {\n List search(String query,int page,int rows);\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建TbItemResult用於返回Solr結果集","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.domain;\n\nimport java.io.Serializable;\n\npublic class TbItemResult implements Serializable {\n private long id;\n private long tbTtemCid;\n private String tbItemCname;\n private String tbItemTitle;\n private String tbItemSellPoint;\n private String tbItemDesc;\n\n public long getId(){\n return id;\n }\n\n public void setId(long id){\n this.id=id;\n }\n \n public long getTbTtemCid(){\n return tbTtemCid;\n }\n\n public void setTbTtemCid(long tbTtemCid){\n this.tbTtemCid=tbTtemCid;\n }\n\n public String getTbItemCname(){\n return tbItemCname;\n }\n\n public void setTbItemCname(String tbItemCname){\n this.tbItemCname=tbItemCname;\n }\n\n public String getTbItemTitle(){\n return tbItemTitle;\n }\n\n public void setTbItemTitle(String tbItemTitle){\n this.tbItemTitle=tbItemTitle;\n }\n\n public String getTbItemSellPoint(){\n return tbItemSellPoint;\n }\n\n public void setTbItemSellPoint(String tbItemSellPoint){\n this.tbItemSellPoint=tbItemSellPoint;\n }\n \n public String getTbItemDesc(){\n return tbItemDesc;\n }\n\n public void setTbItemDesc(String tbItemDesc){\n this.tbItemDesc=tbItemDesc;\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建搜索服務提供者","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"myshop-service-search-provider","attrs":{}},{"type":"text","text":"服務提供者項目","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MyShopServiceSearchProviderApplication","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.provider;\n\n@EnableHystrix\n@EnableHystrixDashboard\n@SpringBootApplication(scanBasePackages=\"com.oxfrod.myshop\")\n@MapperScan(basePackages=\"com.oxford.myshop.service.search.provider.mapper\")\npublic class MyShopServiceSearchProviderApplication {\n public static void main(String[] args) {\n SpringApplication.run(MyShopServiceSearchProviderApplication.class,args);\n Main.main(args);\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在項目中創建","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TbItemResultMapper","attrs":{}},{"type":"text","text":"接口用於查詢MySQL中的數據,用於插入到Solr數據庫中","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.provider.mapper;\n\n@Respository\npublic interface TbItemResultMapper {\n List selectAll();\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Spring的四大註解:\n1. @Controller\n2. @Service\n3. @Component\n4. @Repository\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在resource中創建mapper包用於創建","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TbContentCategoryMapper.xml","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n\n \n \n \n \n \n \n \n \n\n \n\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"初始化Solr:\n\npublic void initSolr() {\n List tbItemResult=tbItemResultMapper.selectAll();\n\n try{\n SolrInputDocument document=null;\n for(TbItemResult tbItemResult:tbItemResults){\n document=new SolrInputDocument();\n document.addFiled(\"id\",tbItemResult.getId());\n document.addFiled(\"tb_item_cid\",tbItemResult.getTbItemCid());\n document.addFiled(\"tb_item_cname\",tbItemResult.getTbItemCname());\n document.addFiled(\"tb_item_title\",tbItemResult.getTbItemTitle());\n document.addFiled(\"tb_item_sell_point\",tbItemResult.getTbItemSellPoint());\n document.addFiled(\"tb_item_desc\",tbItemResult.getTbItemDesc());\n \n solrClient.add(document);\n solrClient.commit();\n }\n }catch(SolrServerException e){\n e.printStackTrace();\n }catch(IOException e){\n e.printStackTrace();\n } \n}\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n搜索Solr:\n\npublic void searchSolr(){\n SolrQuery query=new SolrQuery();\n // 設置查詢條件\n query.setQuery(\"手機\");\n // 分頁查詢\n query.setStart(0);\n query.setRows(10);\n // 設置查詢的默認域\n query.set(\"df\",\"tb_item_keywords\");\n // 設置高亮顯示\n query.setHighlight(true);\n query.addHighlightField(\"tb_item_title\");\n query.setHighlightSimplePre(\"\");\n query.setHighlightSimplePost(\"\");\n\n // 開始查詢\n try{\n QueryResponse queryResponse=solrClient.query(query);\n SolrDocumentList results=queryResponse.getResults();\n // 獲取高亮\n Map>> highlighting=queryResponse.getHighlighting();\n for(SolrDocument result:results){\n List strings=highlighting.get(result.get(\"id\")).get(result.get(\"tb_item_title\"))\n if(strings!=null&&strings.size()>0){\n String title=strings.get(0);\n System.out.println(title);\n }\n }\n }catch(SolrServerException e){\n e.printStackTrace();\n }catch(IOException e){\n e.printStackTrace();\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建SearchServiceImpl實現SearchService接口","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.provider.api.impl;\n\n@Service(version=\"${services.versions.search.v1}\")\npublic class SearchServiceImpl implements SearchService{\n\n @Autowired\n private SolrClient solrClient;\n \n @Override\n public List search(String query,int page,int rows){\n List searchResults=Lists.newArrayList();\n SolrQuery query=new SolrQuery();\n // 設置查詢條件\n query.setQuery(\"手機\");\n // 分頁查詢\n query.setStart((page-1)*rows);\n query.setRows(rows);\n // 設置查詢的默認域\n query.set(\"df\",\"tb_item_keywords\");\n // 設置高亮顯示\n query.setHighlight(true);\n query.addHighlightField(\"tb_item_title\");\n query.setHighlightSimplePre(\"\");\n query.setHighlightSimplePost(\"\");\n\n // 開始查詢\n try{\n QueryResponse queryResponse=solrClient.query(query);\n SolrDocumentList results=queryResponse.getResults();\n // 獲取高亮\n Map>> highlighting=queryResponse.getHighlighting();\n for(SolrDocument solrDocument:solrDocuments){\n TbItemResult result=new TbResult();\n result.setId(Long.parseLong(String.valueOf(solrDocument.get(\"id\"))));\n result.setTbItemCid(Long.parseLong(String.valueOf(solrDocument.get(\"tb_item_cid\"))));\n result.setTbItemCname((String)solrDocument.get(\"tb_item_cname\"));\n result.setTbItemTitle((String)solrDocument.get(\"tb_item_title\"));\n result.setTbItemSellPoint((String)solrDocument.get(\"tb_item_sell_point\"));\n result.setTbItemDesc((String)solrDocument.get(\"tb_item_desc\"));\n\n String tbItemTitle=\"\";\n List list=highlighting.get(result.get(\"id\")).get(result.get(\"tb_item_title\"))\n if(list!=null&&lsit.size()>0){\n String title=list.get(0);\n }else{\n tbItemTitle=(String)solrDocument.get(\"tb_item_title\");\n }\n result.setTbItemTitle(tbItemTitle);\n searchResults.add(result);\n }\n }catch(SolrServerException e){\n e.printStackTrace();\n }catch(IOException e){\n e.printStackTrace();\n }\n return searchResults;\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建搜索服務消費者","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建搜索服務消費者","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"myshop-service-search-consumer","attrs":{}},{"type":"text","text":"對Solr數據庫中的數據進行檢索","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MyShopServiceSearchConsumerApplication","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.consumer;\n\n@EnableHystrix\n@EnableHystrixDashboard\n@SpringBootApplication(scanBasePackages=\"com.oxford.myshop\",exclude=DataSourceAutoConfiguration.class)\npublic class MyShopServiceSearchConsumerApplication{\n public static void main(String[] args){\n SpringApplication.run(MyShopServiceSearchConsumerApplication.class,args);\n Main.main(args);\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SearchController","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.search.consumer.controller;\n\n@RestController\npublic class SearchController{\n @Reference(version=\"${services.versions.search.v1}\")\n private SearchService searchService;\n\n @RequestMapping(value=\"search/{query}/{page}/{rows}\",method=RequestMethod.GET)\n public List search(\n @PathVariable(required=true) String query,\n @PathVariable(required=true) int page,\n @PathVariable(required=true) int rows\n ){\n return searchService.search(query,page,rows)\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章