java连接solr并建立索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dopamy_BusyMonkey/article/details/51242272

1、在tomcat上配置solr:(以后补充)

2、新建一个实体类@Field下面的key要与solr对应索引目录的配置文件schema中的key相对应,代码demo如下,其中构造函数只是方便整体赋值:

package com.vo;

import org.apache.solr.client.solrj.beans.Field;

public class ClassName {

	@Field
	private String key1;
	@Field
	private String key2;
	@Field
	private String key3;	
	
	public ClassName(String key1) {
		super();
		this.key1 = key1;
	}
	
	public String getkey1() {
		return key1;
	}
	public void setkey1(String key1) {
		this.key1 = key1;
	}
	public String getkey2() {
		return key2;
	}
	public void setkey2(String key2) {
		this.key2 = key2;
	}
	public String getkey3() {
		return key3;
	}
	public void setkey3(String key3) {
		this.key3 = key3;
	}



3、连接solr:

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

public class SolrContext {

	private static String url_video;
	private static String url_picture;
	private static String url_videoUpload;
	private static HttpSolrServer server_video;
	private static HttpSolrServer server_picture;
	private static HttpSolrServer server_videoUpload;
	
	static
	{
		url_video = PropertiesUtil.getValueByKey("storage.solr.url.video");
		url_picture = PropertiesUtil.getValueByKey("storage.solr.url.picture");
		url_videoUpload = PropertiesUtil.getValueByKey("storage.solr.url.videoupload");
	}
	
	private SolrContext() {
		
	}
	
	public static SolrServer getVideoServer() throws Exception{
		
		if(server_video==null)
		{
			synchronized(SolrContext.class){
				if(server_video==null)
				{
					server_video=new HttpSolrServer(url_video);
					server_video.setConnectionTimeout(5000);	//solr连接超时时间
					server_video.setSoTimeout(5000);			//solr查询超时时间
					server_video.setMaxTotalConnections(100);	//solr所有最大连接数
					server_video.setDefaultMaxConnectionsPerHost(100);//solr最大连接数
					server_video.setAllowCompression(false);	//solr是否允许压缩
					server_video.setMaxRetries(1);			//solr最大重试次数
					server_video.setFollowRedirects(true);
				}
			}
		}
		return server_video;
	}
	
	public static SolrServer getPictureServer() throws Exception{
		
		if(server_picture==null)
		{
			synchronized(SolrContext.class){
				if(server_picture==null)
				{
					server_picture=new HttpSolrServer(url_picture);
					server_picture.setConnectionTimeout(5000);	//solr连接超时时间
					server_picture.setSoTimeout(5000);			//solr查询超时时间
					server_picture.setMaxTotalConnections(100);	//solr所有最大连接数
					server_picture.setDefaultMaxConnectionsPerHost(100);//solr最大连接数
					server_picture.setAllowCompression(false);	//solr是否允许压缩
					server_picture.setMaxRetries(1);			//solr最大重试次数
					server_picture.setFollowRedirects(true);
				}
			}
		}
		return server_picture;
	}
	
	public static SolrServer getVideoUploadServer() throws Exception{
		
		if(server_videoUpload==null)
		{
			synchronized(SolrContext.class){
				if(server_videoUpload==null)
				{
					server_videoUpload=new HttpSolrServer(url_videoUpload);
					server_videoUpload.setConnectionTimeout(5000);	//solr连接超时时间
					server_videoUpload.setSoTimeout(5000);			//solr查询超时时间
					server_videoUpload.setMaxTotalConnections(100);	//solr所有最大连接数
					server_videoUpload.setDefaultMaxConnectionsPerHost(100);//solr最大连接数
					server_videoUpload.setAllowCompression(false);	//solr是否允许压缩
					server_videoUpload.setMaxRetries(1);			//solr最大重试次数
					server_videoUpload.setFollowRedirects(true);
				}
			}
		}
		return server_videoUpload;
	}
}


4、建立索引,其中UUID码是起到独特的标识作用,不用也行:

import java.util.UUID;

import org.apache.log4j.Logger;

import com.eshore.storage.utils.SolrContext;
import com.eshore.storage.vo.VideoUpload;

public class VideoUploadServiceImpl {
	
	private static final Logger log = Logger.getLogger(VideoUploadServiceImpl.class);

	public boolean generateIndex() {
		try {
			VideoUpload field = new VideoUpload(UUID.randomUUID().toString().replaceAll("-", ""));
			field.setVideo_id("1");
			field.setVideo_unique("1");
			field.setVideo_name("1");
			field.setStatus("1");
			field.setVideo_desc("1");
			field.setTag("1");
			field.setIs_pay("1");
			field.setImg("1");
			field.setVideo_duration("1");
			field.setInitial_size("1");
			field.setError_desc("1");
			field.setAdd_time("1");
			field.setComplete_time("1");
			SolrContext.getVideoUploadServer().addBean(field);
			SolrContext.getVideoUploadServer().commit();
			System.out.println("success!");
			return true;
		} catch (Exception e) {
			log.error("生成索引失败>>>>>>>>>>>", e);
			return false;
		}
	}
}


阅读更多

扫码向博主提问

BusyMonkey

没人能阻挡你,除了你自己
  • 擅长领域:
  • java
  • 图像处理
  • js
  • ios
  • python
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页