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

本文介绍如何使用Solr进行大规模文档的索引与提交操作,包括引入必要的依赖、设置Solr服务器、创建索引文档及优化索引的过程。详细展示了通过Java代码实现批量添加文档并优化索引的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

新建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截图了部分。。。。。。。。。。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值