SpringData-Solr部分字段修改

Solr 部分字段修改的需求原因:

  1. 性能优化:Solr 的索引是基于文档的,而文档通常包含多个字段。如果需要更新整个文档,Solr 将重新索引整个文档,这可能需要耗费大量时间和资源。
  2. 数据一致性:在某些情况下,您可能只需要更新文档中的一部分字段,而不是整个文档。例如,您可能需要更新商品库存或价格,而不需要更新其他商品信息。
  3. 实时索引:Solr 支持实时索引,这意味着当文档被更新时,索引会立即更新,而不需要等待索引重建。如果需要更新整个文档,实时索引可能会导致性能问题。

部分字段修改url请求

要对 Solr 中的部分字段进行更新,可以使用 Solr 的 /update 请求处理程序,具体可以使用 POST 请求。以下是一个示例 URL:

http://localhost:8983/solr/<collection>/update?commit=true 

其中,<collection> 是 Solr 中的一个集合(collection)名称,例如 mycollection

在请求体中,需要指定要更新的文档 ID 和要更新的字段。例如,以下是一个要更新 id12345 的文档中的 title 字段的示例请求体:

[
  {
    "id": "12345",
    "title": { "set": "New Title" }
  }
]

在请求体中,使用 set 操作符来设置要更新的字段的新值。

最后,需要将 commit 参数设置为 true,以便将更新提交到 Solr 索引中。如果不设置 commit 参数,Solr 将不会立即将更新提交到索引中。

完整的示例 URL 和请求体如下:

POST http://localhost:8983/solr/mycollection/update?commit=true
[
  {
    "id": "12345",
    "title": { "set": "New Title" }
  }
]

SpringData-Solr部分字段修改

    public void partialFieldUpdate(){
    
    
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "123456");
        doc.addField("title", JSONObject.of("set", "New Title"));

        solrTemplate.saveDocument("mycollection", doc);
        solrTemplate.commit("mycollection");
    }

猜你喜欢

转载自blog.csdn.net/qq_45584746/article/details/131416817