solr創建索引報錯

一開始不清楚的狀況下,就測試創建索引,而後發現每次提交第二條索引數據時就報錯:apache

Exception in thread "main" org.apache.solr.client.solrj.impl.CloudSolrServer$RouteException: Document contains multiple values for uniqueKey field: id=[8b0d6c35-975e-4943-b1e0-2f0eca7cfee0, 2c904eee-fdf4-448c-a3f5-98cfb29daef9]測試

經過錯誤提示,就是id是個惟一值,可是提交時出現的id是多值,下面是我第一次測試的代碼:spa

public static void solrBatchInsertIndex(List<Map<String, String>> dataMap,
            SolrServer solrServer) {調試

        Collection<SolrInputDocument> solrDocs = new ArrayList<SolrInputDocument>();
        SolrInputDocument solrInputDoc = new SolrInputDocument();
        try {
            // 遍歷多條記錄(多個rowkey)
            for (int i = 0; i < dataMap.size(); i++) {
                Set<Entry<String, String>> docMaps = dataMap.get(i).entrySet();
                if (!docMaps.isEmpty()) {
                    for (Entry<String, String> entry : docMaps) {
                        String column = entry.getKey();
                        String values = entry.getValue();
                        solrInputDoc.addField(column, values);
                    }
                    solrServer.add(solrInputDoc);
                    solrServer.commit(waitFlush, waitSearcher, softCommit);
                }
            }
        } catch (SolrServerException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }對象

    }索引

後來跟蹤調試發現,solrInputDoc對象在上一次提交完沒有清空致使的。調整後的正確代碼以下:ip

public static void solrBatchInsertIndex(List<Map<String, String>> dataMap,
            SolrServer solrServer) {get

        Collection<SolrInputDocument> solrDocs = new ArrayList<SolrInputDocument>();
        SolrInputDocument solrInputDoc = new SolrInputDocument();
        try {
            // 遍歷多條記錄(多個rowkey)
            for (int i = 0; i < dataMap.size(); i++) {
                Set<Entry<String, String>> docMaps = dataMap.get(i).entrySet();
                if (!docMaps.isEmpty()) {
                    for (Entry<String, String> entry : docMaps) {
                        String column = entry.getKey();
                        String values = entry.getValue();
                        solrInputDoc.addField(column, values);
                    }
                    solrServer.add(solrInputDoc);
                    solrServer.commit(waitFlush, waitSearcher, softCommit);
                    solrInputDoc.clear();
                }
            }
        } catch (SolrServerException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }it

    }io

相關文章
相關標籤/搜索