Solr使用SolrDocumentList获取数据库的数据,常见异常

最近初学Solr,使用SolrDocumentList获取数据库的数据,遇到了一下几个常见异常,在这里记录下来,以便日后查找

一、配置文件data-config.xml

这个文件MySql连接的配置,现在放上我此次项目的文件内容

<dataConfig>
    <!-- 这是mysql的配置,学会jdbc的都应该看得懂 -->
    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mytest" user="king" password="123456"/>
    <document>
        <!-- name属性,就代表着一个文档,可以随便命名 -->
        <!-- query是一条sql,代表在数据库查找出来的数据 -->
        <entity name="artical" query="select * from artical">
            <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) -->
			
            <field column="artical_id" name="id"/>
            <field column="img" name="img"/>
            <field column="date" name="date"/>
            <field column="title" name="title"/>
            <field column="author" name="author"/>
            <field column="type" name="type"/>
            <field column="look" name="look"/>
        </entity>
    </document>
</dataConfig>

底下所有的<field>都是关于数据库中列与Solr文档中的域,column是数据库列的名称,name是solr的域名称

1、name配置不相符合

          如果data-config.xml中间有那个域的名称在另外一个文件managed-schema中的配置不相符,就会出现错误。

2、数据库的连接问题

          url="jdbc:mysql://localhost:3306/mytest"

          user="king"

          password="123456"

         这几个参数每个都要与自己数据库的对应,不然数据库连接不成功,就会出现异常。

二、 配置中文分词器异常

managed-schema 文件主要相关 配置中文分词器。

一般来说,如果按照网上的Solr教程一步步尽心配置的,都会有以下代码:

<fieldType name="text_smartcn" class="solr.TextField" positionIncrementGap="100"> 
   <analyzer type="index"> 
       <tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
	   <uniqueKey>id</uniqueKey>
	   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
	  <field name="img" type="string" indexed="false" stored="true"  /> 
	  <field name="date" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="title" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="author" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="type" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="look" type="int" indexed="false" stored="true"  /> 
	  
	  
   </analyzer> 
   <analyzer type="query">
         <tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
		 <uniqueKey>id</uniqueKey>
	   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
	  <field name="img" type="string" indexed="false" stored="true"  /> 
	  <field name="date" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="title" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="author" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="type" type="text_smartcn" indexed="true" stored="true"  /> 
	  <field name="look" type="int" indexed="false" stored="true"  /> 
   </analyzer> 
 </fieldType> 

   这个文件需要注重一下问题:

1、<fieldType>合格标签的属性name是 在Solr选择的中文分词器名称

   我这里选择的是Solr官网推荐的smartcn,其他的属性基本上不需要变

2、analyzer有两个type,一个是index,一个是query,所有的<field>都要存在

    index的配置是关于索引,query的配置的关于查询,Solr在新创建一个Core的时候,会首先遍历执行index的配置,之后在进行查询的时候,就要涉及到query的配置。

  3、Document is missing mandatory uniqueKey field: id

这个异常我看到网上都有解析,但是说的和我的问题有点不对应,最后在自己研究之后发现了错误,希望这个记录可以帮助到同样是在网上查找Solr教程的人。

这个问题主要是因为Solr 的solrconfig配置文件中定义了<uniqueKey>id</uniqueKey>,默认了ID 是唯一的。

但是如果在配置文件中没有配置到id,就会出现这个异常。

我当时在managed-schema 文件中想当然的写了

<uniqueKey>arcicle_id</uniqueKey>

我以为这个id就是我在数据库里面描述的id,为了对应,我写的一模一样,当出现这个问题之后,我还在怀疑,是不是哪里还有一个关于id的描述,与我这个配置出现了冲突。

最后只要把这个id,设置成默认就可以了。

<uniqueKey>id</uniqueKey>

但是要注意的一点是本文提到的常见异常一中的name配置不相符合问题

三、Max Doc等于0

1、fectch 6,Process 0,说明没有建立索引成功; 

2、fectch 6,process 6,也不一定说明建立索引成功;

查看

collection1下 Max Doc:6,才代表成功。

这个参数说明Solr从数据库中获取或者修改的记录条数,如果为0,说明就没有执行。

这个异常要注意看solrconfig.xml文件下面

<requestHandler name="/dataimport" class="solr.DataImportHandler">
      <lst name="defaults">
            <str name="config">data-config.xml</str>
      </lst>
  </requestHandler>

底下的文件名称和自己设定的是不是一样,不然系统会找不到,SolrDocumentList获取不到值

或者就是之前哪里的配置不对,可以直接定位到Solr的配置问题,不许需要思考从数据库获取值的过程。

 


最后在放置一下我在客户端操作Solr的代码,希望有帮助。

package com.frist.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;

public class SolrUtils {

	// solr url
	private final static String BASE_URL = "http://localhost:8080/solr";

	public static HttpSolrClient getSolrClient() {
		/*
		 * 设置超时时间 .withConnectionTimeout(10000) .withSocketTimeout(60000)
		 */
		return new HttpSolrClient.Builder(BASE_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
	}

	// 查询
	public void testQuery() throws IOException, SolrServerException {
		System.out.println("准备进入查询。。。");
		HttpSolrClient solrClient = getSolrClient();
		// 定义查询条件
		Map<String, String> params = new HashMap<String, String>();
		params.put("q", "*:*");
		SolrParams mapSolrParams = new MapSolrParams(params);
		// 执行查询 第一个参数是collection,就是我们在solr中创建的core
		QueryResponse response = solrClient.query("new_core", mapSolrParams);
		// 获取结果集
		SolrDocumentList results = response.getResults();
		System.out.println(results.size());
		for (SolrDocument result : results) {

			System.out.println(result);
		}
	}

	// 添加
	public void testAdd() throws IOException, SolrServerException {
		HttpSolrClient solrClient = getSolrClient();
		// 定义数据 solr输入文档 数据结构为Map
		SolrInputDocument inputDocument = new SolrInputDocument();
		inputDocument.addField("id", "123");
		inputDocument.addField("roleName", "角色名称");
		// 执行添加 ps:如果id相同,则执行更新操作
		// 要指定操作的collection 就是solr-home下定义的core
		UpdateResponse add = solrClient.add("new_core", inputDocument);
		// 提交添加/更新
		solrClient.commit("RoleVO");
	}

	// 删除
	public void testDelete() throws IOException, SolrServerException {
		HttpSolrClient solrClient = getSolrClient();
		// 通过id删除 执行要删除的collection(core)
		solrClient.deleteById("new_core", "123");
		// 还可以通过查询条件删除
		// solrClient.deleteByQuery("RoleVO", "查询条件");
		// 提交删除
		solrClient.commit("new_core");
	}
}

---------------------------------------------------------------------------end---------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值