開篇
在《solr配置中文分析器》博客中說到document時solr進行搜索的數據源,這個數據源是我們上傳到solr中,上傳方法才博客中也介紹到有多種,本篇博客介紹在java中使用solrj編程將數據庫中的字段上傳到solr中作爲數據源。
正文
項目使用技術:spring+springMVC+mysql+mybites
整體思路:從數據庫中根據sql語句查詢數據,遍歷數據創建文檔對象,把文檔對象寫入索引庫。
首先在項目中添加solrj的jar包,然後需要根據自己系統的業務需求進行分析。本實例中的業務如下:
電商項目,根據商品的商品名、賣點描述、所屬類型名、價格等進行搜索,最終返回結果列表在頁面顯示。
數據庫表: tb_item 商品信息表,tb_item_cat商品分類表,tb_item_desc商品賣點描述。
第一步:修改配置文件,重啓tomcat
在schema.xml中添加如下配置,在博客《solr配置文件簡單學習》中解釋了這倆個配置的含義,在結合數據庫使用時每個filed配置對應於數據庫表中的某一列,配置如下:
<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="string" indexed="true" stored="true" />
<field name="item_desc" type="text_ik" indexed="true" stored="false" />
<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"/>
注意:其中type="text_ik",text_ik是我們配置的中文分析器的name。
第二步:編寫java程序,向solr中導入數據
一、建立SearchItem類,包含要使用到的字段。
public class SearchItem {
private String id;
private String title;//名稱
private String sell_point;//賣點
private Long price;//價格
private String image;//圖片
private String category_name;//類別名稱
private String item_desc;//商品描述
}
二、建立mapper,添加查詢所需數據的方法
接口:
package com.tt.search.mapper;
public interface ItemMapper {
List<SearchItem> getItemList();
}
Xml:
<mapper namespace="com.tt.search.mapper.ItemMapper" >
<select id="getItemList" resultType="com.tt.search.pojo.SearchItem">
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.`name` category_name,
c.item_desc
FROM
tb_item a
LEFT JOIN tb_item_cat b ON a.cid = b.id
LEFT JOIN tb_item_desc c ON a.id = c.item_id
WHERE
a.`status` = 1
</select>
</mapper>
三、在spring的配置文件中添加如下配置
<!--單機版的sole客戶端 -->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.169.30:8080/solr"/>
</bean>
四、編寫導入數據的service
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private SolrServer solrServer;
@Autowired
private ItemMapper itemMapper;
@Override
public TaotaoResult importItems() throws Exception {
//查詢數據庫獲得商品列表
List<SearchItem> itemList = itemMapper.getItemList();
//遍歷列表
for (SearchItem item : itemList) {
//創建文檔對象
SolrInputDocument document = new SolrInputDocument();
//添加域
document.addField("id", item.getId());
document.addField("item_title", item.getTitle());
document.addField("item_sell_point", item.getSell_point());
document.addField("item_price", item.getPrice());
document.addField("item_image", item.getImage());
document.addField("item_category_name", item.getCategory_name());
document.addField("item_desc", item.getItem_desc());
//寫入索引庫
solrServer.add(document);
}
//提交
solrServer.commit();
return TaotaoResult.ok();
}
}
注意:在配置文件中配置的是HttpSolrServer,它是SolrServer的子類,配置好後使用SolrServer時它就會自動的注入到SolrServer對象屬性中裏。
五、編寫Controller層
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/importall")
@ResponseBody
public TaotaoResult importAll() {
try {
TaotaoResult result = itemService.importItems();
return result;
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
}
}
六、啓動程序調用Controller向solr中導入數據。
未導入之前:
成功導入後:
上圖中數據左側是solr查詢時會使用的一些參數,下面是一些見解,方便理解操作:
qt: query type,指定查詢使用的QueryHandler,默認是standard。
q: query,查詢字符串(必須)。如果查詢所有*:* ,根據指定字段查詢(item_title:蘋果手機 AND item_sell_point:北京)
fq: filter query,作用:在q查詢符合結果中同時是fq查詢符合的,例如:q= item_title:蘋果手機&fq=date_time:[20081001TO 20091031],找關鍵字title包含蘋果手機,並且date_time是20081001到20091031之間的。
Sort:排序,格式:sort=<field name>+<desc|asc>[,<fieldname>+<desc|asc>] 。示例:(score desc, price asc)表示先“score”降序, 再“price”升序,默認是相關性降序。
Start:用於分頁,定義結果起始記錄數。默認值爲 0。start=15,從第 15 個返回結果。
rows:用於分頁,定義結果每頁返回記錄數,默認爲10
fl: 是逗號分隔的列表,用來指定文檔結果中應返回的 Field 集。默認爲“*”,指所有的字段。
df:默認的查詢字段,一般默認指定。
wt: writer type,指定輸出格式,可以有xml, json, php, phps。
小結
本篇博客在java中使用solrj將數據庫中的數據導入到了solr中當做以後搜索的源數據,簡單的對solrj進行了應用,有了數據就可以進行搜索了,下篇博客通過實例看一下在程序中如何使用solrj完成搜索。