插入数据时solr丢失必填字段错误

时间:2016-05-18 06:07:40

标签: java solr lucene solrj

我在solr v6.0.0

的schema.xml文件中创建了2个不同的字段
<field name="company_id" type="anand_custom_definition" indexed="false" stored="true" required="true" multiValued="false" />
<field name="project_name" type="anand_custom_definition" indexed="true" stored="true" required="true" multiValued="false" /> 

当我尝试使用solrj向solr添加数据时,我得到了 缺少必填字段:company_id

我的插入数据的代码是

public class JDBC {
private static final String DB_URL = "jdbc:mysql://192.168.0.237:3306/anandDatabase?autoReconnect=true";
private static final String USER_NAME = "anand";
private static final String PASSWORD = "anand";
private static final String SOLR_URL = "http://192.168.0.237:8983/solr/anandSampleCollection";
private static SolrClient solrClient;

private static final Logger lOGGER = Logger.getLogger(JDBC.class);

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;

    try {
        Class.forName(MYSQL_DRIVER);
        conn = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
        stmt = conn.createStatement();
        String query = "SELECT id,public_id,name,description,code,company_id FROM project p";
        ResultSet rs = stmt.executeQuery(query);
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        int numColumns = resultSetMetaData.getColumnCount();
        String[] colNames = new String[numColumns + 1];
        int[] colNo = new int[numColumns + 1];

        for (int i = 1; i < (numColumns + 1); i++) {
            if (resultSetMetaData.getColumnName(i) != "null") {
                colNames[i] = resultSetMetaData.getColumnName(i);
            }
        }
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        solrClient = new HttpSolrClient(SOLR_URL.trim());            
        System.out.println(solrClient.ping());                                                
        while (rs.next()) {                
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("project_company_id", rs.getInt("company_id"));
            solrClient.add(doc);                
            docs.add(doc);
            doc = null;
        }
        UpdateResponse updateResponse = solrClient.commit();
        System.out.println(updateResponse.getStatus());
        rs.close();
        stmt.close();
        conn.close();
    } catch (Exception ex) {
        System.out.println("Error OCcurred"+ex.getMessage());
        ex.printStackTrace();
    }
}

}

异常的堆栈跟踪是

{responseHeader={zkConnected=null,status=0,QTime=68,params={q={!lucene}*:*,distrib=false,df=_text_,echoParams=all,rows=10,wt=javabin,version=2}},status=OK}
public id--809ACB41-1C22-3932-6584-1CD2CD649063
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.0.237:8983/solr/anandSampleCollection: [doc=3d586476-b5d0-4136-83f4-acf8145dfa99] missing required field: company_id
Error OCcurredError from server at http://192.168.0.237:8983/solr/anandSampleCollection: [doc=3d586476-b5d0-4136-83f4-acf8145dfa99] missing required field: company_id
    at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:575)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:241)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:230)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:150)
    at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:174)
    at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:139)
    at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:153)
    at com.am.solr.jdbc.JDBC.main(JDBC.java:84)

当我尝试插入project_name值时,任何人都可以通知我为什么我要获取company_id错误

1 个答案:

答案 0 :(得分:0)

嗯,错误是由company_id字段没有任何值造成的。查看代码,您永远不会为company_id字段添加值:

doc.addField("project_company_id", rs.getInt("company_id"));

project_company_idcompany_id不同。