gpt4 book ai didi

java - 如何在 Solr 中添加/更新

转载 作者:行者123 更新时间:2023-12-01 09:03:56 26 4
gpt4 key购买 nike

我正在尝试使用java程序访问solr并使用它添加/更新记录(这实际上创建了一个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();
}

欢迎任何其他方法/意见。有人可以帮忙破译这个错误吗?

最佳答案

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 中添加/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com