solrj索引操作


添加索引

Solr添加文檔至索引: http://www.cnblogs.com/dennisit/p/3621717.html

刪除索引:

每天索引記錄有一個唯一標識,索引的刪除通過唯一標識操作,如下實例展示索引的刪除.

刪除單個索引

    /** * 根據id從索引中刪除記錄[測試通過] * @param server * @param idName 主鍵名 * @param id 主鍵值 */
    public static void deleteById(SolrServer server, String idName, Object id){ try { server.deleteByQuery(idName + ":" + id.toString()); server.commit(false, false); LOG.info("Delete from index by id" + id + " finished . operate param is:" + idName + ":" + id.toString()); } catch (Exception e) { LOG.error("Delete from index by id" + id + " error, " + e.getMessage(), e); } }

批量刪除索引

    
    /** * 根據id集合從索引中刪除記錄[測試通過] * @param server * @param ids */
    public static <T> void deleteByIds(SolrServer server, String idName,List<T> ids){ try { if (ids.size() > 0) { StringBuffer query = new StringBuffer(idName + ":" + ids.get(0)); for (int i = 1; i < ids.size(); i++) { if (null != ids.get(i)) { query.append(" OR " + idName + ":" + ids.get(i).toString()); } } server.deleteByQuery(query.toString()); server.commit(false, false); LOG.info("Delete from index by id list" + ids + " finished ."); }else{ LOG.info("Delete ids list is null."); } } catch (Exception e) { LOG.error("Delete from index by id list" + ids + " error, " + e.getMessage(), e); e.printStackTrace(); } }

根據查詢刪除索引

    /** * 根據查詢從索引中刪除[測試通過] * @param server * @param queryString */
    public static void deleteByQuery(SolrServer server,String query){ try { server.deleteByQuery(query); server.commit(false, false); LOG.info("Delete from index by query string " + query + "finished ."); } catch (Exception e) { LOG.error("Delete from index by query Strng " + query + "error, " + e.getMessage(), e); e.printStackTrace(); } }

根據對象刪除

    /** * 根據對象刪除,實質是根據Id刪除 * * @param server solr客戶端 * @param object 刪除的對象 * @param idName 對象的主鍵名 */
    public static void deleteBean(SolrServer server,Object object,String idName){ Class<?> cls = object.getClass(); try { Method method = cls.getMethod(EntityConvert.dynamicMethodName(idName, "get")); Object o = method.invoke(object); if (o != null) { deleteById(server,idName,method.invoke(object)); } LOG.info("Delete from index by object" + object); } catch (Exception e) { LOG.error("Delete from index by object error, " + e.getMessage() ,e); e.printStackTrace(); } }

刪除所有索引

    
    /** * 刪除所有索引 [測試通過] * @param server */
    public static void deleteAllIndex(SolrServer server){ try { server.deleteByQuery("*:*"); server.commit(false, false); LOG.info("All index delete finished."); } catch (Exception e) { LOG.error("Delete all index error " + e.getMessage(), e); e.printStackTrace(); } }

修改索引

    /** * 更新單個記錄 [測試通過] * @author pudongping * * @param server * Solr客戶端 * @param object * 要更新成的對象 * @param idName * 主鍵id名 */
    public static void updateBean(SolrServer server,Object object,String idName){ if(null!=object && StringUtils.isNotBlank(idName)){ Class<?> clzz = object.getClass(); try { Method method = clzz.getMethod(EntityConvert.dynamicMethodName(idName, "get")); Object o = method.invoke(object); if(null != o){ SolrQuery query = new SolrQuery(); query.setQuery(idName + ":" + o.toString()); query.setStart(0); query.setRows(1); QueryResponse response = server.query(query); SolrDocument document = response.getResults().get(0); LOG.info("更新一個記錄" + EntityConvert.solrDocument2Entity(document, clzz)); System.out.println("更新一個記錄" + EntityConvert.solrDocument2Entity(document, clzz)); UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false); updateRequest.add(solrDocument2SolrInputDocument(document, object)); updateRequest.process(server); } } catch (NoSuchMethodException e) { LOG.error("Update bean error"  + object); e.printStackTrace(); } catch (IllegalAccessException e) { LOG.error("Update bean error"  + object); e.printStackTrace(); } catch (IllegalArgumentException e) { LOG.error("Update bean error"  + object); e.printStackTrace(); } catch (Exception e) { LOG.error("Update bean error"  + object); e.printStackTrace(); } } } 

所謂更新,無非是根據唯一標識查詢出來,然后重新賦值.在solr中我們查詢出來的結果為SolrDocument對象,而updateRequest.add(..)的對象為SolrInputDocument對象.所以我們需要編寫這兩個對象的轉換

    
    /** * [測試通過] * * 更新數據時用到,給出要更新的對象,Id為必須給出的屬性,然后加上要更新的屬性 * 如果對應的屬性的值為空或者為0,這不需要更新 * * @param sd 查詢到得SolrDocument * @param object * @return SolrInputDocument */
    public static SolrInputDocument solrDocument2SolrInputDocument(SolrDocument sd, Object object) { if (object != null && sd != null) { SolrInputDocument sid = new SolrInputDocument(); Collection<String> fieldNameCollection = sd.getFieldNames();            // 得到所有的屬性名
            Class<?> cls = object.getClass(); Object o = null; for (String fieldName : fieldNameCollection) { try { //需要說明的是返回的結果集中的FieldNames()比類屬性多
                    Field[] filedArrays = cls.getDeclaredFields();                        //獲取類中所有屬性
                    for (Field f : filedArrays) { //如果實體屬性名和查詢返回集中的字段名一致,填充對應的set方法
                        if(f.getName().equals(fieldName)){ // 如果對應的屬性的值為空或者為0,這不需要更新
                                o = cls.getMethod(EntityConvert.dynamicMethodName(fieldName, "get")).invoke(object); Class<?> fieldType = cls.getDeclaredField(fieldName).getType(); if (fieldType.equals(Integer.TYPE)) { Integer fieldValue = Integer.class.cast(o); if (fieldValue != null && fieldValue.compareTo(0) != 0) { sid.addField(fieldName, fieldValue); } } else if (fieldType.equals(Float.TYPE)) { Float fieldValue = Float.class.cast(o); if (fieldValue != null && fieldValue.compareTo(0f) != 0) { sid.addField(fieldName, fieldValue); } } else if (fieldType.equals(Double.TYPE)) { Double fieldValue = Double.class.cast(o); if (fieldValue != null && fieldValue.compareTo(0d) != 0) { sid.addField(fieldName, fieldValue); } } else if (fieldType.equals(Short.TYPE)) { Short fieldValue = Short.class.cast(o); if (fieldValue != null && fieldValue.compareTo((short)0) != 0) { sid.addField(fieldName, fieldValue); } } else if (fieldType.equals(Long.TYPE)) { Long fieldValue = Long.class.cast(o); if (fieldValue != null && fieldValue.compareTo((long)0) != 0) { sid.addField(fieldName, fieldValue); } } else if(fieldType.equals(List.class)){ List fieldValue = List.class.cast(o); if(fieldValue != null){ sid.addField(fieldName, fieldValue); } }else { if (o != null) { sid.addField(fieldName, o.toString()); } } } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { LOG.error("請檢查PO類中的field對應的各個setter和getter是否存在!"); e.printStackTrace(); } catch (NoSuchFieldException e) { LOG.error("請檢查schema中的field是否不存在於PO類中!"); e.printStackTrace(); } } return sid; } LOG.warn("即將要轉換的SolrDocument或者要更新的Object為null"); return null; }

查詢索引

    /** * 根據關鍵字查詢 [測試通過 - 使用 solr內部轉換機制] * @param <T> * @param server solr客戶端 * @param solrql sql查詢串 * @param pageNum 當前頁碼 * @param pageSize 每頁顯示的大小 * @param clzz 對象類型 * @return
     */
    public static <T>Page<T> query(SolrServer server,String solrql,int pageNum,int pageSize, Class<T> clzz){ SolrQuery query = new SolrQuery(); query.setQuery(solrql); query.setStart((pageNum-1)*pageSize); query.setRows(pageSize); QueryResponse response = null; try { response = server.query(query); } catch (SolrServerException e) { e.printStackTrace(); return null; } //查詢到的記錄總數
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue(); //查詢結果集
        List<T> items = response.getBeans(clzz); //填充page對象
        return new Page<T>(pageNum, pageSize, totalRow, items); }

查詢說明,參看這篇文章: http://www.cnblogs.com/huangfox/archive/2012/02/13/2348949.html

轉載請注明出處:[http://www.cnblogs.com/dennisit/p/3623974.html]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM