solr集成springboot并向solr推送索引数据更新和查询

虽然说是很简单的功能,但是在此记录一下

首先导入maven映射

	
		org.apache.solr
		solr-solrj
	

引入solr的jar镜像之后就可以开始开发了

Junit测试用例:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.xx.module.constant.CommonConstants;
import com.xx.module.request.PushDataIntoSolrRequest;
import com.xx.module.request.QuerySolrIndexRequest;  
@SpringBootTest(classes=DcExternalApiApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class SolrTests {
	
	private MockMvc mockMvc; 
	@Autowired
    	protected WebApplicationContext wac;
	@Before()  
    	public void setup() {
        	mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //初始化MockMvc对象
    	}
	@Test
	public void pushDataIntoSolr() { //推送solr测试方法
		Map map = new HashMap<>();  
		map.put("id", 2);
        	map.put("title", 2);
        	map.put("content", 2);
        	PushDataIntoSolrRequest request = new PushDataIntoSolrRequest();
        	ObjectMapper mapper = new ObjectMapper();
        	ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        	request.setInput(map);
        	request.setCoreName("test_core");
        	MvcResult result;
		try {
			java.lang.String requestJson = ow.writeValueAsString(request);
			result = mockMvc.perform(post("/solr/pushDataIntoSolr").contentType(MediaType.APPLICATION_JSON_VALUE).content(requestJson))
			        .andExpect(status().isOk())   
			        .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
			        .andReturn();
			System.out.println(result.getResponse().getContentAsString());   
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//查询solr方法
	@Test
	public void querySolrIndex(){
		MvcResult result;
		ObjectMapper mapper = new ObjectMapper();
        	ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        	QuerySolrIndexRequest request = new QuerySolrIndexRequest();
		try {
			request.setCoreName("test_core");
			request.setQuery("analyzeTime:2");
			java.lang.String requestJson = ow.writeValueAsString(request);
			result = mockMvc.perform(get("/solr/querySolrIndex").contentType(MediaType.APPLICATION_JSON_VALUE).content(requestJson))
			        .andExpect(status().isOk())
			        .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))// 预期返回值的媒体类型text/plain;charset=UTF-8    
			        .andReturn();
			System.out.println(result.getResponse().getContentAsString());   
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

solr插入数据的controller:

import java.util.Map;

import javax.annotation.Resource;

import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xx.external.api.service.solr.SolrService;
import com.xx.module.base.BaseResponse;
import com.xx.module.request.PushDataIntoSolrRequest;
import com.xx.module.request.QuerySolrIndexRequest;

@Controller
@RequestMapping(value = "/solr")
public class SolrController {
	
	@Resource
	private SolrService solrService;
	/**
	 * 添加入solr索引 
	 */
	@RequestMapping(value = "/pushDataIntoSolr" , method = RequestMethod.POST)
	public BaseResponse pushDataIntoSolr(@RequestBody PushDataIntoSolrRequest request){
		BaseResponse ress = new BaseResponse();
		SolrInputDocument input = new SolrInputDocument();
		for (Map.Entry entry : request.getInput().entrySet()) {
			input.addField(entry.getKey(), entry.getValue());
		}
		if (!solrService.pushDataIntoSolr(request.getCoreName(), input)){
			ress.setErrorCode("500");
			ress.setErrorMsg("插入失败,请稍候重试。");
		} else {
			ress.setSubMsg("请求成功,数据插入成功。");
		}
		return ress;
	}
	
	/**
	 * 按条件查询搜索引擎
	 */ 
	@RequestMapping(value = "/querySolrIndex" , method = RequestMethod.GET)
	public SolrDocumentList querySolrIndex(@RequestBody QuerySolrIndexRequest request){
		return solrService.querySolrIndex(request.getCoreName(), request.getQuery());
	}
}

封装的请求类和响应类

import java.util.Map;

import javax.validation.constraints.NotNull;

/**
 *@ClassName:PushDataIntoSolrRequest.java
 *@ClassDescribe:solr存储请求类
 *@author:chenxi
 *@createDate:2017年9月29日 上午9:44:20
 *@version
 */
public class PushDataIntoSolrRequest implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 核心名称
	 */
	@NotNull
	private String coreName ;
	
	/**
	 * 存储实体
	 */
	@NotNull
	private Map input;

	public String getCoreName() {
		return coreName;
	}

	public void setCoreName(String coreName) {
		this.coreName = coreName;
	}

	public Map getInput() {
		return input;
	}

	public void setInput(Map input) {
		this.input = input;
	}
}

	
	
public class QuerySolrIndexRequest implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 核心名称
	 */
	private String coreName ;
	
	/**
	 * 查询条件
	 */
	private String query;

	public String getCoreName() {
		return coreName;
	}

	public void setCoreName(String coreName) {
		this.coreName = coreName;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
}
public class BaseResponse implements Serializable {
	
	private static final long serialVersionUID = -1864470087731542406L;

	private String errorCode;

	private String errorMsg;

	private String subCode;

	private String subMsg;
	
	/**
	 * @return the errorCode
	 */
	public String getErrorCode() {
		return errorCode;
	}

	/**
	 * @param errorCode
	 *            the errorCode to set
	 */
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	/**
	 * @return the errorMsg
	 */
	public String getErrorMsg() {
		return errorMsg;
	}

	/**
	 * @param errorMsg
	 *            the errorMsg to set
	 */
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	/**
	 * @return the subCode
	 */
	public String getSubCode() {
		return subCode;
	}

	/**
	 * @param subCode
	 *            the subCode to set
	 */
	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	/**
	 * @return the subMsg
	 */
	public String getSubMsg() {
		return subMsg;
	}

	/**
	 * @param subMsg
	 *            the subMsg to set
	 */
	public void setSubMsg(String subMsg) {
		this.subMsg = subMsg;
	}

	public boolean isSuccess() {
		return this.errorCode == null && this.subCode == null && this.errorMsg == null && this.subMsg == null;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}
服务层接口和具体实现

import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

/**
 *@ClassName:SolrService.java
 *@ClassDescribe:操作solr
 *@author:chenxi
 *@createDate:2017年9月28日 下午2:25:53
 *@version
 */
public interface SolrService {
	
	/**
	 *@ClassDescribe:向solr插入数据
	 *@author:chenxi
	 *@createDate:2017年9月28日 下午2:26:58
	 *@param coreName 核心名称
	 *@param input 数据封装
	 *@return
	 */
	public boolean pushDataIntoSolr(String coreName ,SolrInputDocument input);
	
	/**
	 *@ClassDescribe:按条件查询搜索引擎
	 *@author:chenxi
	 *@createDate:2017年9月28日 下午4:37:12
	 *@param query solr查询条件
	 *@return 返回查询集合
	 */
	public SolrDocumentList querySolrIndex(String coreName, String query);
	
}
import java.io.IOException;  
  
import org.apache.solr.client.solrj.SolrClient;  
import org.apache.solr.client.solrj.SolrQuery;  
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.common.SolrDocumentList;  
import org.apache.solr.common.SolrInputDocument;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Service;  
  
import com.xx.xx.xx.service.solr.SolrService;  
  
@Service  
@ConfigurationProperties(prefix = "solr")  
public class SolrServiceImpl implements SolrService {  
  
    private Logger logger = LoggerFactory.getLogger(SolrServiceImpl.class);  
  
    private String httpSolrClient;  
      
    private static SolrClient solr;  
      
    public String getHttpSolrClient() {  
        return httpSolrClient;  
    }  
  
    public void setHttpSolrClient(String httpSolrClient) {  
        this.httpSolrClient = httpSolrClient;  
    }  
      
    private HttpSolrClient connetHttpSolrClientServer(String coreName){  
        HttpSolrClient server = new HttpSolrClient(httpSolrClient + coreName);  
        server.setSoTimeout(5000);   
        server.setConnectionTimeout(1000);   
        server.setDefaultMaxConnectionsPerHost(1000);   
        server.setMaxTotalConnections(5000);  
        return server;  
    }  
      
    /** 
     *@ClassDescribe:向solr插入数据 
     *@author:chenxi 
     *@param coreName 核心名称 
     *@param input 数据封装 
     * @return 
     */  
    @Override  
    public boolean pushDataIntoSolr(String coreName, SolrInputDocument input) {  
        boolean flag = false;  
        try {  
            solr = connetHttpSolrClientServer(coreName);  
            solr.add(input);  
            solr.commit();  
            flag = true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            logger.error(e.getMessage());  
        } finally {  
            try {  
                solr.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.error(e.getMessage());  
            }  
        }  
        return flag;  
    }  
  
      
    /** 
     *@ClassDescribe:按条件查询搜索引擎 
     *@author:chenxi 
     *@param query solr查询条件 
     *@return 返回查询集合 
     */  
    @Override  
    public SolrDocumentList querySolrIndex(String coreName, String query) {  
        SolrDocumentList list = null;  
        try {  
            solr = connetHttpSolrClientServer(coreName);  
            QueryResponse rsp = null;  
            SolrQuery queryStr = new SolrQuery("*:*");  
            queryStr.addFilterQuery(query);  
            rsp = solr.query(queryStr);  
            list = rsp.getResults();  
        } catch (IOException | SolrServerException e) {  
            e.printStackTrace();  
            logger.error(e.getMessage());  
        } catch (Exception e) {  
            e.printStackTrace();  
            logger.error(e.getMessage());  
        } finally {  
            try {  
                solr.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.error(e.getMessage());  
            }  
        }  
        return list;  
    }  
}
最后在application.xml指定solr的url
solr.httpSolrClient=http://127.0.0.1:8983/solr/
如果solr是集群的 请参考: http://blog.csdn.net/qq_20261343/article/details/50706705,具体没测试。
至此,solr集成springboot推送数据和查询数据完成!

你可能感兴趣的:(springboot)