Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SolrJ更新Solr中的文档_Java_Solr_Solrj - Fatal编程技术网

Java 使用SolrJ更新Solr中的文档

Java 使用SolrJ更新Solr中的文档,java,solr,solrj,Java,Solr,Solrj,我在Solr中有以下索引数据。我想使用SolrJ在这个索引数据中添加一个额外的字段“Location”。我该怎么做 "response":{"numFound":3061,"start":3059,"docs":[ { "Id":12345, "Name":"Rajeev Kumar", "_version_":1223434645768768687}, { "Id":67890, "Name

我在Solr中有以下索引数据。我想使用SolrJ在这个索引数据中添加一个额外的字段“Location”。我该怎么做

"response":{"numFound":3061,"start":3059,"docs":[
      {
        "Id":12345,
        "Name":"Rajeev Kumar",
        "_version_":1223434645768768687},
      {
        "Id":67890,
        "Name":"Rohit Kumar",
        "_version_":1246457656544545434}]
  }}
更新后,它应该是这样的-

"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}]
  }}
  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();
    }
“响应”:{“numFound”:3061,“开始”:3059,“文档”:[
{
“Id”:12345,
“姓名”:“拉吉耶夫·库马尔”,
“地点”:,
“u版本:1223434645768768687}”,
{
“Id”:67890,
“名称”:“Rohit Kumar”,
“地点”:,
“[版本:124645765445434}]
}}
我试过这样的东西-

"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}]
  }}
  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();
    }
publicstaticvoidmain(字符串[]args)抛出SolrServerException、IOException{
字符串URL字符串=”http://localhost:8983/solr/gettingstarted";
HttpSolrClient solr=新的HttpSolrClient.Builder(urlString.build();
setParser(新的XMLResponseParser());
SolrInputDocument=新的SolrInputDocument();
HashMap值=新的HashMap();
价值。卖出(“集合”、“德里”);
document.addField(“位置”,值);
solr.commit();
}

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

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

document.addField("Id", 12345); 

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

Ok。我已经添加了唯一的密钥,但是错误出现在未定义的字段“Location”中。更新前是否需要在schema.xml中添加“Location”?是的,除非您使用的是无模式模式,否则该字段必须存在于schema中。您能否提供一个来源,说明您“无法在一个请求中对多个文档执行更新”?提供一个重载,该重载接受一系列
SolrInputDocument
对象。同样,文档根本没有提到文档更新。您可以提交多个新文档(或所述文档的多个原子更新指令,但它们都必须由ID标识),这就是您要链接的内容。这与“按查询更新”不同,后者类似于
updatefooseta=1,其中field='bar'
。在您链接的API方法中,您已经有了可用的文档,而不是要求Solr更新由查询(
字段:bar
)标识的一组文档。