本文详细解析了在使用Spring Data Solr进行数据导入时遇到的常见错误:文档缺少唯一标识符id。通过分析错误堆栈,指出问题源于SolrTemplate的saveBean方法误用,正确的方法应为saveBeans。文章强调了代码审查的重要性,提醒开发者注意细节,避免类似错误。
报错
Exception in thread "main" org.springframework.data.solr.UncategorizedSolrException: Document is missing mandatory uniqueKey field: id; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Document is missing mandatory uniqueKey field: id
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:145)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:184)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:178)
at com.pinyougou.solrutil.SolrUtil.importItemData(SolrUtil.java:35)
at com.pinyougou.solrutil.SolrUtil.main(SolrUtil.java:43)
Caused by: org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Document is missing mandatory uniqueKey field: id
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:552)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116)
at org.springframework.data.solr.core.SolrTemplate$4.doInSolr(SolrTemplate.java:187)
at org.springframework.data.solr.core.SolrTemplate$4.doInSolr(SolrTemplate.java:184)
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:141)
... 4 more