使用solr完成搜尋功能
環境:linux系統
solr版本:solr-4.10.3.tar.gz
其他外掛: IK中文分詞器: IK Analyzer 2012FF_hf1
1、解壓solr-4.10.3.tar.gz
2、ll bin
3、在/usr/local下,mkdir solr
4、mv tomcat /solr/tomcat (將tomcat移動到solr目錄下)
5、cp /root/solr-4.10.3/dist/solr-4.10.3.war tomcat/webapps/solr.war (將solr的war包拷貝到tomcat/webapps)
6、cd …
bin/startup.sh (啟動tomcat解壓war包)
7、tail -f logs/catalina.out (檢視伺服器啟動狀態)
8、cd webapps
cd solr
cd WEB-INF
cd ~
cd solr-4.10.3
cd example
cd lib
cd ext
cp *.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib
cd /root/solr-4.10.3/example
cp -r solr /usr/local/solr/solrhome
cd /usr/local/tomcat/webapps/solr/WEB-INF
vi web.xml
做如下修改,將註釋開啟,將value地方改成solrhome的目錄
solr/home
/usr/local/solr/solrhome/
java.lang.String
cd ../../../
bin/startup.sh
tail -f logs/catalina.out
啟動之後可以通過192.168.25.130:8080/solr 可以訪問solr了
匯入ik分詞器:
solr是不支援中文分詞的,需要匯入ik中文分詞器
將ik分詞器的資料夾拷貝到linux裡
cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
在WEB-INF下 mkdir classes
cd classes
pwd
拷貝路徑
進到IK目錄裡
cp ext_stopword.dic IKAnalyzer.cfg.xml mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
cd solrhome
cd collection1
cd conf
vi schema.xml
設定業務域
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!--自定義業務域-->
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="long" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true"/>
<field name="item_category_name" type="text_ik" indexed="true" stored="true"/>
<field name="item_desc" type="text_ik" indexed="true" stored="true"/>
<!--總的業務域:搜尋一個關鍵字的時候只要是出現在標題或賣點或分類或描述都查詢它-->
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>
<copyField source="item_desc" dest="item_keywords"/>
儲存退出
啟動tomcat
瀏覽器輸入地址,可以進行相關測試
我這裡是http://192.168.25.130:8080/solr
eclipse中測試:
加入依賴solr:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
需要通過spring注入一個SolrServer
<!-- solrService -->
<bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.130:8080/solr/collection1"></constructor-arg>
</bean>
@Autowired
private SolrServer solrServer;
public JDResult getAllItems() {
try {
List<SearchResult> list = searchMapper.getAllItems();
//新增到索引庫中
for(SearchResult item:list){
SolrInputDocument doc=new SolrInputDocument();
doc.setField("id", item.getId());
doc.setField("item_title", item.getTitle());
doc.setField("item_sell_point", item.getSellPoint());
doc.setField("item_price", item.getPrice());
doc.setField("item_image", item.getImage());
doc.setField("item_category_name", item.getCategoryName());
doc.setField("item_desc", item.getDescription());
solrServer.add(doc);
}
//提交事務
solrServer.commit();
return JDResult.ok();
} catch (Exception e) {
e.printStackTrace();
return JDResult.bind(400, "匯入失敗");
}
}
匯出查詢結果:
public Search getSearchResult(String queryString, Integer pageNow, Integer pageSize) throws Exception {
Search search=new Search();
//建立solrQuery物件
SolrQuery query=new SolrQuery();
//設定查詢條件
query.setQuery(queryString);
//分頁
if(pageNow==null||pageNow<1){
pageNow=1;
}
if(pageSize==null){
pageSize=8;
}
query.setStart((pageNow-1)*pageSize);
query.setRows(pageSize);
//設定預設域
query.set("df", "item_keywords");
//設定高亮
query.setHighlight(true);
//設定高亮顯示的域
query.addHighlightField("item_title");
//設定字首
query.setHighlightSimplePre("<span style='color:red;'>");
//設定字尾
query.setHighlightSimplePost("</span>");
//執行查詢,得到response物件
QueryResponse response = solrServer.query(query);
//獲取到集合
SolrDocumentList results = response.getResults();
//獲取總資訊條數
long numFound = results.getNumFound();
//封裝到search中
search.setCount(numFound);
//計算總頁數
long totalPage=numFound/pageSize;
if(numFound%pageSize!=0){
totalPage++;
}
search.setTotalPages(totalPage);
//建立SearchResult的集合
List<SearchResult> list=new ArrayList<SearchResult>();
for(SolrDocument solrDocument:results){
SearchResult searchResult=new SearchResult();
//獲取到高亮
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list2 = highlighting.get(solrDocument.get("id")).get("item_title");
//考慮到高亮不在title中
String title="";
if(list2!=null&&list2.size()>0){
title=list2.get(0);
}else{
title=(String)solrDocument.get("item_title");
}
searchResult.setTitle(title);
searchResult.setId((String)solrDocument.get("id"));
searchResult.setSellPoint((String)solrDocument.get("item_sell_point"));
searchResult.setPrice(solrDocument.get("item_price")+"");
//如果上傳了多張圖片,那麼傳到solr查詢後,預設展示第一張圖片
String image=(String)solrDocument.get("item_image");
if(StringUtils.isNotBlank(image)){
//將圖片分割成陣列
image= image.split(",")[0];
}else{
//如果為空,放一張預設的圖片的地址
image="http://192.168.25.133/group1/M00/00/00/wKgZhVqwrM-AfymiAADXbj4VXok563.jpg";
}
searchResult.setImage(image);
//新增到集合
list.add(searchResult);
}
search.setItemList(list);
return search;
}
匯出成功