從solr結果集中取數據,取到告終果集,可是結果集是一個map,而我想要獲得的是一個對象,怎麼處理呢?我前後嘗試了三種方式:
java
如下是獲取的源數據:json
SolrDocument[{id=25157561, productId=000000000108292291, product={"name":"鴨鴨 2013新潮90絨短款輕薄羽絨服女正品清倉 時尚春秋外套B-360 檸檬黃 170/92A","oldId":"19999729","type":"0","brandId":"000153467","brandName":"鴨鴨","labelPaste":"null","imageCount":5,"firstCategoryId":"361003","secondCategoryId":"161007","thirdCategoryId":"346882","firstCategoryName":"服裝鞋帽","secondCategoryName":"女裝","thirdCategoryName":"羽絨服","purchaseCategory":"R9001206","id":"000000000108292291","groupId":"284049","color":"檸檬黃","version":"170/92A"}, showOrderFlag=0, userId=6000654740, user={"province":"","birthday":"","constellation":"","id":"6000654740","nickName":"112***1","gender":"","typeId":"1","imageUrl":"http://image.suning.cn/uimg/cmf/cust_headpic/0000000000_01_60x60.jpg","levelId":"161000000020","levelName":"銀卡會員"}, storeFlag=0, store={}, againReviewFlag=0, title=, content=骨灰盒江景房, contentLength=6, labels=[], score=1, anonymousFlag=0, bestFlag=0, sort=0, status=0, createDate=Tue Aug 27 17:02:43 CST 2013, publishIp=10.19.95.189, publishDeviceId=1, usefulVoteCount=0, replyCount=0}]
方案一:solrDocument中提供了一個獲取每一個field對應值的方法,使用此方法獲取全部的field對應的值,set到類中:工具
private List<ReviewFieldDTO> setReviewData(SolrDocumentList list) { List<ReviewFieldDTO> datas = new ArrayList<ReviewFieldDTO>(); String jsonStr = ""; ReviewFieldDTO review = null; for (SolrDocument solrDocument : list) { review = new ReviewFieldDTO(); review.setId((Long)solrDocument.getFieldValue("id")); review.setProduct(solrDocument.getFieldValue("product").toString()); review.setOrderDate((Date)solrDocument.getFieldValue("orderDate")); ... datas.add(review); } return datas; }
這樣作的缺點:ui
一、個人類中有20-30個屬性,就要須要二三十此,代碼至關低級,可讀性不好設計
二、若是solr中沒有一個field對應的值爲空,就會報異常,這樣每個solrDocument.getFieldValue方法都要try-catch,我想這樣寫確定不行,能不能直接將solrDocument變成我要的ReviewFieldDTO類呢,通過代碼編寫,我使用了第二種方案:code
方案二:我使用了BeanUtils工具+反射,經過反射,獲取solrDocument中的全部key和value,而後利用BeanUtils.setProperty(bean, name, value);方法,給ReviewFieldDTO設置屬性,這樣也有缺點,就是反射過程當中容易出現異常,另外更嚴重的是此方法對Date類型的屬性不能處理,而ReviewFieldDTO中有Date類型的屬性,若是非要使用這種方案,就要寫date類型的轉換類,無疑增長了代碼量,我只能放棄這種方案。對象
方案三:這是最後想到的一種方案,由於拿到上面的map值後,感受跟json很像,可是又不是json格式,一直在作的是將map直接轉換爲對像,忘記了能夠使用json做爲中間變量,因此方案三大家應該是想到了:get
private List<ReviewFieldDTO> setReviewData(SolrDocumentList list) { List<ReviewFieldDTO> datas = new ArrayList<ReviewFieldDTO>(); String jsonStr = ""; ReviewFieldDTO review = null; for (SolrDocument solrDocument : list) { jsonStr = getJson(solrDocument); review = new ReviewFieldDTO(); review = gson.fromJson(jsonStr, ReviewFieldDTO.class); datas.add(review); } return datas; }
就是先將solrDocument類轉換爲json,而後再將此json轉換爲我要的ReviewFieldDTO類,太爽了吧,以前太逗比了吧 ,因此有時候設計思路很重要,不必定非要直接將map轉成對象,能夠使用中間變量。OK,先這麼多吧it
下面一樓一位大哥給的評論太爽了,以前工做都是廢的啊,原來solr中提供了將solrDcoument轉換爲類的API,哎,道行差太遠啊。。。io
private List<ReviewFieldDTO> setReviewData(SolrDocumentList list) { DocumentObjectBinder binder = new DocumentObjectBinder(); List<ReviewFieldDTO> reviews = binder.getBeans(ReviewFieldDTO.class, list); return reviews; }
這下好了,感謝那位大拿!!!