使用SolrJ在Solr中更新文档

use*_*570 2 java solr solrj

我在Solr中有以下索引数据。我想使用SolrJ在此索引数据中添加一个额外的字段“位置”。我怎样才能做到这一点?

"response":{"numFound":3061,"start":3059,"docs":[
      {
        "Id":12345,
        "Name":"Rajeev Kumar",
        "_version_":1223434645768768687},
      {
        "Id":67890,
        "Name":"Rohit Kumar",
        "_version_":1246457656544545434}]
  }}
Run Code Online (Sandbox Code Playgroud)

更新后,它应如下所示-

"response":{"numFound":3061,"start":3059,"docs":[
      {
        "Id":12345,
        "Name":"Rajeev Kumar",
        "Location" : <some value>,
        "_version_":1223434645768768687},
      {
        "Id":67890,
        "Name":"Rohit Kumar",
        "Location": <some value>,
        "_version_":1246457656544545434}]
  }}
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情-

  public static void main(String[] args) throws SolrServerException, IOException {

            String urlString = "http://localhost:8983/solr/gettingstarted";
            HttpSolrClient solr = new HttpSolrClient.Builder(urlString).build();
            solr.setParser(new XMLResponseParser());
            SolrInputDocument document = new SolrInputDocument();
            HashMap<String, Object> value = new HashMap<String, Object>(); 
            value.put("set","Delhi"); 
            document.addField("Location", value); 
             solr.commit();
    }
Run Code Online (Sandbox Code Playgroud)

但是问题在于,这是在创建新文档而不是更新旧文档。预先感谢您的帮助。

Mat*_*ndh 5

您必须在文档值中包含id部分,否则Solr无法找到要更新的原始文档。这应该是一个uniqueKey字段。

document.addField("Id", 12345); 
Run Code Online (Sandbox Code Playgroud)

您不能在一个请求中对多个文档执行更新,因此每个文档都必须自己更新。