如何在Solr中添加/更新

pu82cl6c  于 2022-09-27  发布在  Solr
关注(0)|答案(2)|浏览(188)

我试图使用java程序访问solr并使用它添加/更新记录(这实际上是使用curl创建HttpUrlConnection和Posts-json数据)。
虽然我应该能够在我的java程序中收到响应,但我收到一个File not found error。作为一个新手,这个错误对我来说似乎没有意义。
这是我的部分代码

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();
}

欢迎任何其他方法/投入。有人能帮忙破译这个错误吗?

5fjcxozz

5fjcxozz1#

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();
k3bvogb1

k3bvogb12#

尝试使用solrj将solrInputDocument添加到solr sore,但在URI中添加了“/update”…无法理解如何处理它。

相关问题