我的项目中有这个问题。我使用Apache Poi读取了.xlsx excel文件,并且希望在Solr核心中为它们建立索引。我使用SolrInputDocument索引读取文件。这是我的Java代码
package org.solr;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
public class PoiJava {
private static final String fileName="C:\\Users\\FTK1187\\Desktop\\E-Archive - Copy\\TableArchive.xlsx";
public static void main(String Args[]) throws SolrServerException {
List dataList=getArchiveData();
}
private static List getArchiveData() throws SolrServerException {
List dataList =new ArrayList();
FileInputStream excelFile=null;
try {
excelFile = new FileInputStream(new File(fileName));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
String urlString="http://localhost:8983/solr/archiveCore";
SolrClient solr=new HttpSolrClient.Builder(urlString).build();
SolrInputDocument document=new SolrInputDocument();
if(!document.isEmpty())
{
solr.deleteByQuery("*");
solr.commit();
}
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
//System.out.println(currentCell.getStringCellValue());
for(int i=0;i<currentRow.getLastCellNum();i++)
{
if(currentCell.getColumnIndex()==1)
{
document.addField("NameAdded", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==2)
{
document.addField("DateAdded", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==3)
{
document.addField("NameModified", "");
}
else if(currentCell.getColumnIndex()==4)
{
document.addField("DateModified", "");
}
else if(currentCell.getColumnIndex()==5)
{
document.addField("strSO", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==6)
{
document.addField("strCust", "");
}
else if(currentCell.getColumnIndex()==7)
{
document.addField("strOperator", "");
}
else if(currentCell.getColumnIndex()==8)
{
document.addField("PackName", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==9)
{
document.addField("DocName", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==10)
{
document.addField("DocType", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==11)
{
document.addField("extType", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==12)
{
document.addField("FileName", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==13)
{
document.addField("FilePath", currentCell.getStringCellValue());
}
else if(currentCell.getColumnIndex()==14)
{
document.addField("NameDeleted", "");
}
else if(currentCell.getColumnIndex()==15)
{
document.addField("DateDeleted", "");
}
else if(currentCell.getColumnIndex()==16)
{
document.addField("intRev", currentCell.getStringCellValue());
}
}
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
//System.out.println(currentCell.getNumericCellValue());
for(int k=0;k<currentRow.getLastCellNum();k++)
{
if(currentCell.getColumnIndex()==0)
{
document.addField("id", currentCell.getNumericCellValue());
}
}
}
UpdateResponse response=solr.add(document);
solr.commit();
}
//System.out.println();
System.out.println(document.getField("id"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
return dataList;
}
}
因此,当我运行我的项目时,它会给我这个错误。
Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/archiveCore: Document is missing mandatory uniqueKey field: id
at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:610)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:279)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:268)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:173)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:138)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:152)
at org.solr.PoiJava.getArchiveData(PoiJava.java:148)
at org.solr.PoiJava.main(PoiJava.java:33)
当我使用SimplePostTool为文件编制索引时,不会出现类似的错误,但是我想更新我的网页中的核心。
答案 0 :(得分:2)
您可能在架构中将字段设置为唯一键,如下所示:
<uniqueKey>id</uniqueKey>
问题在于,当您通过Apache POI上传文档时(在这种情况下),您没有为该唯一字段发送值。
您有两种选择:
<copyField source="excel_guaranteed_unique" dest="id"/>
有了实际的文档,您只需在“ id”字段中添加一个UUID。
创建一个像UUID这样的唯一字段来更新您的RequestHandlder,如下所示:
<updateRequestProcessorChain name="uuid" >
<processor class="solr.UUIDUpdateProcessorFactory">
<str name="fieldName">id</str>
</processor>
...
</updateRequestProcessorChain>
...
<requestHandler name="/update" class="solr.UpdateRequestHandler">
<lst name="defaults">
<str name="update.chain">uuid</str>
</lst>
</requestHandler>
您还需要更新提取处理程序:
<requestHandler name="/update/extract"
startup="lazy"
class="solr.extraction.ExtractingRequestHandler" >
<lst name="defaults">
...
<str name="update.chain">uuid</str>
</lst>