本文介绍solr的基本数据操作,基于solr 8.2。solr支持多种数据格式,包括XML,JSON,CSV等,并提供多种脚本和工具来操作数据。本文讲解curl请求和JSON数据格式的处理方式。

本文使用单solr服务来演示数据操作,创建名为 my_core 的solr core, 文档schema如下:

<schema name="my" version="1.0">   <uniqueKey>id</uniqueKey>   <fieldType name="string" class="solr.StrField" sortMissingLast="true"/>   <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>   <field name="author" type="string" multiValued="false" indexed="true" required="true" stored="true"/>   <field name="title" type="string" indexed="true" required="true" stored="true" multiValued="false"/>   <field name="tags" type="string" multiValued="true" indexed="true" stored="true"/> </schema>

插入文档

插入单个文档

最基本的数据操作就是插入单个文档:

curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_core/update/json/docs?commit=true' --data-binary ' {   "id": "0132350882",   "author": "Robert C. Martin",   "title": "Clean Code: A Handbook of Agile Software Craftsmanship",   "tags": [     "Computer Programming Languages",     "Software Testing",     "Software Design & Engineering"   ] }'

参数说明:

  1. -X POST : 设置HTTP请求类型为POST,后续所有操作都为POST请求
  2. -H 'Content-Type: application/json' : 设置请求格式为JSON
  3. http://localhost:8983/solr/my_core/update/json/docs?commit=true: 请求地址URL和查询参数。其中 http://localhost:8983 为solr服务的地址和端口, my_core 为文档插入的core名称,可以根据自己的实际情况调整。 comment=true 参数表示此操作要进行同步提交。如果不设置此参数, solr则会根据 solrconfig.xml 文件 UpdateHandler 配置荐中的 autoCommit 配置延迟提交修改。
  4. --data-binary :此参数指定操作所需的JSON数据,这里是新插入的文档。

以上curl命令的几个参数在后续的操作中都会用到,用法也基本相同。

错误信息

当操作有误时,solr的响应体会包含错误信息。比如,当执行以下操作时: