如何在 Solr 中 Add/Update
How to Add/Update in Solr
我正在尝试使用 java 程序访问 solr 并使用它 add/update 记录(这实际上创建了一个 HttpUrlConnection 和
使用 curl 发布 json 数据)。
虽然我应该能够在我的 java 程序中收到响应,但我收到 找不到文件错误。作为一个新手,这个错误对我来说似乎没有意义。
这是我的部分代码
String url = "http://localhost:8983/solr/jcg/update?stream.body";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type:", "application/json");
conn.setRequestProperty("Accept", "*/*");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data ="{\"id\":\"978-0641723445\",\"apple_t\":\"set\":\"abc\"}";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
System.out.println(url+data);
new InputStreamReader(conn.getInputStream());
System.out.println("input"+conn.getInputStream());
System.out.println(conn.getContent());
} catch (Exception e) {
e.printStackTrace();
}
欢迎任何其他 approaches/inputs。有人可以帮助破译错误吗?
HttpURLConnection 版本
try{
String url = "http://localhost:8983/solr/jcg/update/json?commit=true";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "*/*");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data ="[{\"id\":\"978-0641723445\",\"apple_t\":{\"set\":\"abc\"}}]";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
System.out.println("input: "+conn.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
使用solrj。我用的是5.3.0版本。
添加新文档
String urlString = "http://localhost:8983/solr/jcg";
SolrClient client = new HttpSolrClient(urlString);
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "new-document-id" );
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
client.add( docs );
client.commit();
client.close();
更新现有文档
String urlString = "http://localhost:8983/solr/jcg";
SolrClient client = new HttpSolrClient(urlString);
SolrInputDocument doc1 = new SolrInputDocument();
Map<String, String> operations = new HashMap<String, String>();
operations.put("set", "abc");
doc1.addField("id", "978-0641723445");
doc1.addField("apple_t", operations);
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
client.add( docs );
client.commit();
client.close();
我正在尝试使用 java 程序访问 solr 并使用它 add/update 记录(这实际上创建了一个 HttpUrlConnection 和 使用 curl 发布 json 数据)。
虽然我应该能够在我的 java 程序中收到响应,但我收到 找不到文件错误。作为一个新手,这个错误对我来说似乎没有意义。
这是我的部分代码
String url = "http://localhost:8983/solr/jcg/update?stream.body";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type:", "application/json");
conn.setRequestProperty("Accept", "*/*");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data ="{\"id\":\"978-0641723445\",\"apple_t\":\"set\":\"abc\"}";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
System.out.println(url+data);
new InputStreamReader(conn.getInputStream());
System.out.println("input"+conn.getInputStream());
System.out.println(conn.getContent());
} catch (Exception e) {
e.printStackTrace();
}
欢迎任何其他 approaches/inputs。有人可以帮助破译错误吗?
HttpURLConnection 版本
try{
String url = "http://localhost:8983/solr/jcg/update/json?commit=true";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "*/*");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data ="[{\"id\":\"978-0641723445\",\"apple_t\":{\"set\":\"abc\"}}]";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
System.out.println("input: "+conn.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
使用solrj。我用的是5.3.0版本。
添加新文档
String urlString = "http://localhost:8983/solr/jcg";
SolrClient client = new HttpSolrClient(urlString);
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "new-document-id" );
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
client.add( docs );
client.commit();
client.close();
更新现有文档
String urlString = "http://localhost:8983/solr/jcg";
SolrClient client = new HttpSolrClient(urlString);
SolrInputDocument doc1 = new SolrInputDocument();
Map<String, String> operations = new HashMap<String, String>();
operations.put("set", "abc");
doc1.addField("id", "978-0641723445");
doc1.addField("apple_t", operations);
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
client.add( docs );
client.commit();
client.close();