首页 > 解决方案 > 使用 ContentStreamUpdateRequest 在 SolrJ 中文档缺少强制唯一键错误

问题描述

运行项目时出现此错误。我正在使用 Apache POI 读取 .xlsx 文件,并且我想在我的 Solr 核心上索引读取文件。我使用 ContentStreamUpdateRequest 来做到这一点。但是有一个错误,我无法修复它。我的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 static org.junit.Assert.assertNotNull;

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.Date;
import java.util.List;
import java.util.UUID;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;

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.request.AbstractUpdateRequest.ACTION;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.UpdateResponse;
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, IOException {
        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();
            final String urlString="http://localhost:8983/solr/archiveCore";
            SolrClient solr=new HttpSolrClient.Builder(urlString).build();
            SolrQuery query = new SolrQuery();
            //SolrInputDocument document=new SolrInputDocument();
            ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update"); 
            solr.deleteByQuery("*");
            solr.commit();
            req.addFile(new File("C:\\\\Users\\\\FTK1187\\\\Desktop\\\\E-Archive - Copy\\\\TableArchive.xlsx"),"text/csv");
            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.NUMERIC) {
                        //System.out.println(currentCell.getNumericCellValue());
                        for(int k=0;k<currentRow.getLastCellNum();k++)
                        {
                            if(currentCell.getColumnIndex()==0)
                            {
                                req.setParam("literal.id", String.valueOf(currentCell.getNumericCellValue()));
                            }

                        }

                    }
                    else if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        //System.out.println(currentCell.getStringCellValue());
                        for(int i=0;i<currentRow.getLastCellNum();i++)
                        {
                            if(currentCell.getColumnIndex()==1)
                            {
                                req.setParam("literal.NameAdded", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==2)
                            {
                                req.setParam("literal.DateAdded", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==3)
                            {
                                req.setParam("literal.NameModified", "");
                            }
                            else if(currentCell.getColumnIndex()==4)
                            {
                                req.setParam("literal.DateModified", "");
                            }
                            else if(currentCell.getColumnIndex()==5)
                            {
                                req.setParam("literal.strSO", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==6)
                            {
                                req.setParam("literal.strCust", "");
                            }
                            else if(currentCell.getColumnIndex()==7)
                            {
                                req.setParam("literal.strOperator", "");
                            }
                            else if(currentCell.getColumnIndex()==8)
                            {
                                req.setParam("literal.PackName", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==9)
                            {
                                req.setParam("literal.DocName", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==10)
                            {
                                req.setParam("literal.DocType", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==11)
                            {
                                req.setParam("literal.extType", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==12)
                            {
                                req.setParam("literal.FileName", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==13)
                            {
                                req.setParam("literal.Filepath", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==14)
                            {
                                req.setParam("literal.NameDeleted", "");
                            }
                            else if(currentCell.getColumnIndex()==15)
                            {
                                req.setParam("literal.DateDeleted", currentCell.getStringCellValue());
                            }
                            else if(currentCell.getColumnIndex()==16)
                            {
                                req.setParam("literal.intRev", currentCell.getStringCellValue());
                            }

                        }


                    } 


                }
                //System.out.println();
                //updateRequest.add(document);
                req.setAction(ACTION.COMMIT, true, true);
                solr.request(req);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }

    }

}

当我运行这段代码时,它给了我这个错误

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
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.SolrClient.request(SolrClient.java:1219)
    at org.solr.PoiJava.main(PoiJava.java:155)

我对代码进行了一些更改,所以我编辑了我的问题。它现在给了我这个错误。

标签: javasolrapache-poisolrj

解决方案


推荐阅读