SolrJ中缺少必填的唯一键字段错误 [英] Missing mandatory unique key field error in SolrJ

查看:427
本文介绍了SolrJ中缺少必填的唯一键字段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有这个问题.我使用Apache Poi读取了.xlsx excel文件,并且希望在Solr核心中为它们建立索引.我使用SolrInputDocument索引读取文件.这是我的Java代码

I've this issue in my project. I read my .xlsx excel file using Apache Poi and I want to index them in my Solr core. I use SolrInputDocument to index reading file. Here is my java codes

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;
    }
}

因此,当我运行我的项目时,它会给我这个错误.

So when I'm running my project it gives me this error.

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为文件建立索引时,不会出现类似的错误,但是我想更新我的网页中的核心.

When I'm indexing files using SimplePostTool there is not error like that but I want to update my core in my web page.

推荐答案

您可能在模式中将字段设置为唯一键,如下所示:

You probably have in your schema a field set as the unique key like this:

<uniqueKey>id</uniqueKey>

问题在于,当您上载文档(在这种情况下是通过Apache POI)时,您没有为该唯一字段发送值.

The problem is that when you upload a doc, in this case via Apache POI, you are not sending a value for that unique field.

您有两种选择:

  1. 如果您确实有一个唯一的字段,请使用它.例如,使用copyField选项,例如:

<copyField source="excel_guaranteed_unique" dest="id"/>

  1. 有了实际的文档,您只需在"id"字段中添加一个UUID.

  1. As you have the actual document, you could just add a UUID to the "id" field.

创建一个像UUID这样的唯一字段来更新您的RequestHandlder,如下所示:

Create a unique field like a UUID updating your RequestHandlder, like this:

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

您还需要更新提取处理程序:

You also need to update the extract handler:

 <requestHandler name="/update/extract"
              startup="lazy"
              class="solr.extraction.ExtractingRequestHandler" >
<lst name="defaults">
  ...
  <str name="update.chain">uuid</str>
</lst>

这篇关于SolrJ中缺少必填的唯一键字段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆