一、前言
今天基于solr的solr.UpdateRequestHandler提交http://127.0.0.1:8080/solr/core/update?commit=true的url接口提交xml进行指定id删除或者全量删除,报“java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:8080/solr/core/update?commit=true”异常,详细日志如下
java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:8080/solr/core/update?commit=true@b@ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)@b@ at com.iss.pms.core.search.SolrIndexUpdater.update(SolrIndexUpdater.java:85)@b@ at com.iss.pms.core.search.SolrIndexUpdater.deleteAll(SolrIndexUpdater.java:68)@b@ at com.iss.pms.core.search.SolrIndexUpdater.main(SolrIndexUpdater.java:111)
二、解决方法
原来删除xml的方法如下,提交连接是“http://127.0.0.1:8080/solr/core/update?commit=true”
public static void deleteAll(){@b@ update(" <delete><query>*:*</query></delete><commit/> ", "update?commit=true"); @b@}
修改为如下,去除“<commit/>”
public static void deleteAll(){ @b@ update(" <delete><query>*:*</query></delete> ", "update?commit=true"); @b@}
三、相关代码
public class SolrIndexUpdater { @b@@b@ private static String indexUrl = null;@b@ @b@ private static String dataimportUrlAppender="subject/dataimport?command=full-import&commit=true&clean=true";@b@ @b@ private static String updateUrlAppender="subject/update?commit=true";@b@ @b@ static {@b@ if(indexUrl == null){@b@ indexUrl = PropertieFileReader.getString("solr.subject.commit.url");@b@ }@b@ }@b@ @b@ public static void dataimport(){@b@ HttpURLConnection conn = null;@b@ try {@b@ conn = (HttpURLConnection) new URL(indexUrl+dataimportUrlAppender).openConnection();@b@ conn.setRequestMethod("GET");@b@ conn.setRequestProperty("Content-type", "text/xml");@b@ conn.setDoOutput(true);@b@ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));@b@ String lines="";@b@ String ss="";@b@ while((lines = in.readLine()) != null){@b@ ss+=lines;@b@ }@b@ System.out.println("subject index add success-------------------------------------:"+ss);@b@ conn.disconnect();@b@ } catch (MalformedURLException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } finally {@b@ if(conn != null) conn.disconnect();@b@ }@b@ }@b@ @b@ public static void delete(String... ids){@b@ if(ids == null || ids.length < 1){@b@ return;@b@ }@b@ StringBuffer bf = new StringBuffer("<delete>");@b@ for(String id : ids)@b@ bf.append("<id>").append(id).append("</id>");@b@ bf.append("</delete>");@b@ update(bf.toString()); @b@ }@b@ @b@ public static void deleteAll(){ @b@ update(" <delete><query>*:*</query></delete> "); @b@ }@b@ @b@ @b@ @b@ public static void update(String doc){@b@ InputStream is = null;@b@ HttpURLConnection conn = null;@b@ try {@b@ String tmp = indexUrl + updateUrlAppender;@b@ conn = (HttpURLConnection) new URL(tmp).openConnection();@b@ conn.setRequestMethod("POST");@b@ conn.setRequestProperty("Content-type", "text/xml");@b@ conn.setDoOutput(true);@b@ OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");@b@ os.write(doc);@b@ os.close();@b@ is = conn.getInputStream();@b@ while (is.available() > 0) {@b@ byte[] b = new byte[is.available()];@b@ is.read(b);@b@ log.debug(new String(b)); @b@ }@b@ is.close();@b@ conn.disconnect();@b@ } catch (MalformedURLException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } finally {@b@ if(is != null)@b@ try {@b@ is.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ if(conn != null) conn.disconnect();@b@ }@b@ } @b@ @b@ public static void main(String[] arg){@b@ deleteAll();@b@ }@b@ @b@@b@@b@}