环境:linux & java6
启动solr
下载solr-4.7.2.zip,解压后,
cd solr-4.7.2/example
java -jar start.jar
此时浏览器访问http://localhost:8983/solr/,可以看到
已经自带了一个core(core的概念可以理解为数据表)
我们要自己建立一个core怎么办?
很简单:
cd solr-4.7.2/example/solr
cp -r collection1 optcollection # 这里的optcollection就你的表名
vim optcollection/core.properties ,将其中的collection1改成optcollection
此时按前面的方法重启solr,再次访问http://localhost:8983/solr/ 看到两个core了
core的建立看来没有问题。
写入数据
但是通过solr的java客户端往其中写入数据的时候出现的问题,
我是这样写入的
SolrServer solrServer = new HttpSolrServer(solrServerUrl);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", taskInfo.getId());
document.addField("name", taskInfo.getName());
document.addField("task_type", taskInfo.getTask_type());
document.addField("platform", taskInfo.getPlatform());
document.addField("opt_type", taskInfo.getOpt_type());
document.addField("opt_content", taskInfo.getOpt_content());
document.addField("device_type", taskInfo.getDevice_type());
document.addField("start_time", taskInfo.getStart_time());
document.addField("status", taskInfo.getStatus());
错误类似如下
2144848 [qtp563929184-75] INFO org.apache.solr.core.SolrCore – [optcollection] webapp=/solr path=/select params={indent=true&q=name:*&_=1399715876159&wt=json} hits=0 status=0 QTime=1
2145130 [qtp563929184-76] INFO org.apache.solr.update.processor.LogUpdateProcessor – [optcollection] webapp=/solr path=/update params={wt=javabin&version=2} {} 0 0
2145131 [qtp563929184-76] ERROR org.apache.solr.core.SolrCore – org.apache.solr.common.SolrException: ERROR: [doc=610] unknown field 'task_type'
at org.apache.solr.update.DocumentBuilder.toDocument(DocumentBuilder.java:174)
at org.apache.solr.update.AddUpdateCommand.getLuceneDocument(AddUpdateCommand.java:77)
at org.apache.solr.update.DirectUpdateHandler2.addDoc0(DirectUpdateHandler2.java:234)
at org.apache.solr.update.DirectUpdateHandler2.addDoc(DirectUpdateHandler2.java:160)
说明task_type没有定义,于是vim solr-4.7.2/example/solr/optcollection/conf/schema.xml
修改约从126行的位置,改后的效果如下:
<!-- Only remove the "id" field if you have a very good reason to. While not strictly
required, it is highly recommended. A <uniqueKey> is present in almost all Solr
installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".
-->
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="task_type" type="int" indexed="true" stored="true" />
<field name="platform" type="int" indexed="true" stored="true" />
<field name="opt_type" type="int" indexed="true" stored="true" />
<field name="opt_content" type="text_general" indexed="true" stored="true" omitNorms="true"/>
<field name="device_type" type="int" indexed="true" stored="true" />
<field name="start_time" type="text_general" indexed="true" stored="true" omitNorms="true"/>
<field name="status" type="int" indexed="true" stored="true" />
<field name="operator" type="string" indexed="true" stored="true" multiValued="true"/>
此时重启solr可能会提示
Caused by: org.apache.solr.common.SolrException: copyField source :'cat' is not a glob and doesn't match any explicit field or dynamicField.. Schema file is /opt/meituan/service/mobile/solr-4.7.2/example/solr/optcollection/schema.xml
at org.apache.solr.schema.IndexSchema.readSchema(IndexSchema.java:614)
at org.apache.solr.schema.IndexSchema.<init>(IndexSchema.java:166)
at org.apache.solr.schema.IndexSchemaFactory.create(IndexSchemaFactory.java:55)
那是因为上一步对schema.xml的修改删除了name='cat'的field,于是在schema.xml搜索,删除以下的内容
<copyField source="cat" dest="text"/>
<copyField source="name" dest="text"/>
<copyField source="manu" dest="text"/>
<copyField source="features" dest="text"/>
<copyField source="includes" dest="text"/>
<copyField source="manu" dest="manu_exact"/>
<!-- Copy the price into a currency enabled field (default USD) -->
<copyField source="price" dest="price_c"/>
保存重启solr,客户端提交数据给solr,一切ok了。