欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Solr客户端自定义开发

程序员文章站 2022-06-14 16:09:22
...

@Service
@Qualifier("searchService")
public class SearchService {
	@Value("${solr.url}")
	private String searchurl;
	@Value("${solr.q.fields}")
	private String qfields;
	@Value("${solr.s.fields}")
	private String sfields;
	@Value("${solr.h.fields}")
	private String hfields;
	/**
	 * 分页查询Position
	 * @param key 关键字
	 * @param page 分页
	 * @param pagesize 分页大小
	 * @return 分页信息
	 */
	public PageInfo<Position> query(String key, int page, int pagesize) {
		PageInfo<Position> ps = new PageInfo<Position>(page, pagesize);
		SolrServer server = new HttpSolrServer(searchurl);
		SolrQuery params = new SolrQuery();
		params.setQuery(setQueryFields(key, qfields));
		params.setStart(ps.getStart());
		params.setRows(ps.getPagesize());
		boolean ish = setHighlight(params,hfields);
		setSortFields(params, sfields);
		SolrDocumentList list = null;
		//第一个Map的键是文档的ID,第二个Map的键是高亮显示的字段名
		Map<String,Map<String,List<String>>> high= null;
		try {
			QueryResponse response = server.query(params);
			list = response.getResults();
			if(ish){
				high = response.getHighlighting();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(ish){
			
		}
		ps.setItems(mappingVo(list,high,ish));
		ps.setTotal((int) list.getNumFound());
		return ps;
	}
	/**
	 * 设置高亮字段
	 * @param params SolrQuery对象
	 * @param hfields2 高亮字段,以逗号分隔
	 * @return 是否需要高亮
	 */
	private boolean setHighlight(SolrQuery params, String hfields2) {		
		if(StringUtils.hasText(hfields2)){
			String[] arr = hfields2.split(",");
			for (String s : arr) {
				params.addHighlightField(s);
			}
			params.setHighlight(true);
			params.setHighlightSimplePre("<font color='red'><em>");
			params.setHighlightSimplePost("</em></font>");
			return true;
		}
		return false;
	}
	/**
	 * 将查询结果返回映射为Position对象列表
	 * @param list 返回的document列表
	 * @param high 高亮信息
	 * @param ish 是否需要高亮
	 * @return Position对象列表
	 */
	private List<Position> mappingVo(SolrDocumentList list,Map<String,Map<String,List<String>>> high,boolean ish) {
		List<Position> poses = new ArrayList<Position>();
		for (SolrDocument doc : list) {
			Position p = new Position();
			p.setPid(doc.getFieldValue("CP_ID").toString());
			if(ish && high.get(p.getPid()).containsKey("COMPANY_NAME")){
				p.setCompany(high.get(p.getPid()).get(("COMPANY_NAME")).toString());
			}else{
				p.setCompany(doc.getFieldValue("COMPANY_NAME").toString());
			}	
			p.setStamp((Date)doc.getFieldValue("ADD_AT"));
			poses.add(p);
		}
		return poses;
	}
	/**
	 * 设置排序字段
	 * @param params SolrQuery对象
	 * @param sfields2 排序字段,都是按降序排
	 */
	private void setSortFields(SolrQuery params, String sfields2) {
		String[] arr = sfields2.split(",");
		for (String s : arr) {
			params.addSortField(s, ORDER.desc);
		}

	}
	/**
	 * 定义查询字段
	 * @param key 关键字
	 * @param qfields2 以,分隔的字段列表,以或条件
	 * @return
	 */
	private String setQueryFields(String key, String qfields2) {
		StringBuilder sb = new StringBuilder();
		String[] arr = qfields2.split(",");
		for (String s : arr) {
			sb.append(s).append(":").append(key).append(" OR ");
		}
		String q = sb.toString();
		return q.substring(0, q.length() - 4);
	}
}

另外在浏览器中清除所有的索引可以这样
http://localhost:8080/solr/update?stream.body=%3Cdelete%3E%3Cquery%3E*:*%3C/query%3E%3C/delete%3E&commit=true

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.nbrc.gqh</groupId>
	<artifactId>search</artifactId>
	<packaging>war</packaging>
	<version>1.0.0-SNAPSHOT</version>
	<name>search Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.solr</groupId>
			<artifactId>solr-solrj</artifactId>
			<version>3.6.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-lgpl</artifactId>
			<version>1.8.1</version>
		</dependency>
		<dependency>
			<groupId> org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.5</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-lgpl</artifactId>
			<version>1.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.2.1</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>search</finalName>
	</build>
</project>

相关标签: solr java client