java - Solr 中缺少强制 uniquekey 字段错误

标签 java solr apache-poi solrj

我的项目中遇到了这个问题。我使用 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 索引文件时,没有出现类似的错误,但我想更新网页中的核心。

最佳答案

您的架构中可能有一个字段设置为唯一键,如下所示:

<uniqueKey>id</uniqueKey>

问题是,当您上传文档时(在本例中是通过 Apache POI),您没有发送该唯一字段的值。

您有几个选择:

  1. 如果您确实有一个唯一的字段,请使用它。例如,使用 copyField 选项,例如:
<copyField source="excel_guaranteed_unique" dest="id"/>
  • 当您拥有实际文档时,您只需将 UUID 添加到“id”字段即可。

  • 创建一个独特的字段,例如更新您的 RequestHandlder 的 UUID,如下所示:

  • <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>
    

    关于java - Solr 中缺少强制 uniquekey 字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55275348/

    相关文章:

    java - 为什么简单的代码会加载很多类?

    java - 测试用例问题

    java - 暴力破解java递归密码

    java - 通过将其导出为 jar 文件,在不同的项目中重用我的 java 代码

    mysql - SOLR 多个数据源上的索引和搜索

    solr - 避免由于词干而在Solr上缓慢突出显示

    ruby-on-rails - 在 rails 3 中太阳黑子搜索中的命名范围

    Apache POI : easy way to compare worksheets?

    java - 如何在 R 中加载 apache poi 库?

    java - 使用Java从Excel读取数据时如何仅提取整数值而不是Double值