solr add documents

 

使用solrj调用api接口 向solr中添加数据创建索引

在向solr中提交长文本的时候引起报错的问题,那么如何向solr提交长文本?


    public boolean importDocs(long projectId){
        int userId = collectProjectService.getCollectProject(projectId).getCreateUser();
        String url="http://localhost:8080/solr/search_node";
        HttpSolrClient httpSolrClient = new HttpSolrClient(url);
        httpSolrClient.setParser(new XMLResponseParser()); // 设置响应解析器

        httpSolrClient.setConnectionTimeout(500); // 建立连接的最长时间

//创建文本集合

        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();

//创建单个文本 

SolrInputDocument doc = new SolrInputDocument();

//向文本中添加字段和内容

doc.addField("id",1);

doc.addField("name","china");

  SolrInputDocument doc2 = new SolrInputDocument();

doc.addField("id",1);

//在这里输入一个超长文本,导致字符串长度过长,详情看下面报错

doc.addField("name","qwroqf...");

 docs.add(doc);

docs.add(doc2);

try {
                //添加并提交文本
                httpSolrClient.add(docs);

}catch (Exception e){
                System.out.println(e);
                try {
                    httpSolrClient.close();
                }catch (Exception e2){


                }
                return false;
            }
        }
        try {
httpSolrClient.commit();
            httpSolrClient.close();
 }catch (Exception e){
            return false;

}
 boolean bol = true;
        return bol;

    }


报错:

Exception writing document id 56_10 to the index; possible analysis error: Document contains at least one immense term in field="content" (whose UTF8 encoding is longer than the max length 32766), all of which were skipped.  Please correct the analyzer to not produce such terms.  The prefix of the first immense term is: '[-29, -128, -128, -29, -128, -128, -28, -68, -92, -27, -65, -125, -27, -92, -86, -27, -71, -77, -26, -76, -117, -17, -68, -116, -26, -100, -84, -26, -99, -91]...', original message: bytes can be at most 32766 in length; got 43768. Perhaps the document has an indexed string field (solr.StrField) which is too large


原因:

添加documents时,会向schema.xml中写入document中的字段field,根据solrconfig.xml中的default来判断使用的字段类型fieldType

我的配置中默认的使用strings,导致字符串长度过长报错


解决办法:

将 <str name="defaultFieldType">strings</str>标签中的strings改为text_general

如下:

    <processor class="solr.AddSchemaFieldsUpdateProcessorFactory">
      <str name="defaultFieldType">text_general</str>
      <lst name="typeMapping">
        <str name="valueClass">java.lang.Boolean</str>
        <str name="fieldType">booleans</str>
      </lst>
      <lst name="typeMapping">
        <str name="valueClass">java.util.Date</str>
        <str name="fieldType">tdates</str>
      </lst>
      <lst name="typeMapping">
        <str name="valueClass">java.lang.Long</str>
        <str name="valueClass">java.lang.Integer</str>
        <str name="fieldType">tlongs</str>
      </lst>
      <lst name="typeMapping">
        <str name="valueClass">java.lang.Number</str>
        <str name="fieldType">tdoubles</str>
      </lst>
    </processor>




                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值