青野 发表于 2016-12-15 11:21:11

利用SOLR搭建企业搜索平台 利用solrj提交索引

  新建java项目,使用的jar 有
  下载的solr下面的dist下面的solr-solrj-xxx.jar包和下面的solrj-lib下面的所有包
  代码如下

package s;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
public class T {
public static final String SOLR_URL = "http://localhost:8080/solr/core0";
public static void main(String[] args) {
commit();
}
public static void commit(){
HttpSolrServer solr=null;
try{
solr = new HttpSolrServer(SOLR_URL);
}catch(Exception e){
}
solr.setSoTimeout(1000);
for(int i=0;i<1000;i++){
SolrInputDocument sid = new SolrInputDocument();
sid.addField("id", i);
sid.addField("name", "struts+hibernate+spring 开发大全" + i);
sid.addField("summary", "三种框架的综合应用" + i);
sid.addField("author", "xxxx"+i);
//sid.addField("date", new Date());
sid.addField("content", "高级应用类书籍" + i);
sid.addField("keywords","SSH" + i);
try {
solr.add(sid);
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}
try {
//优化索引
solr.optimize();
solr.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
System.out.println("--------提交完成-------");
}
}

  schema.xml配置如下:

<fields>   
<!-- general -->
<field name="id"      type="string"   indexed="true"stored="true"multiValued="false" required="true"/>
<field name="type"      type="string"   indexed="true"stored="true"multiValued="false" />
<field name="name"      type="string"   indexed="true"stored="true"multiValued="false" />
<field name="core0"   type="string"   indexed="true"stored="true"multiValued="false" />
<field name="_version_" type="long"   indexed="true"stored="true"/>
<field name="summary" type="string"   indexed="true"stored="true"/>
<field name="author" type="string"   indexed="true"stored="true"/>
<field name="content" type="string"   indexed="true"stored="true"/>
<field name="keywords" type="string"   indexed="true"stored="true"/>
</fields>
  xml截图了部分。。。。。。。。。。
页: [1]
查看完整版本: 利用SOLR搭建企业搜索平台 利用solrj提交索引