solr java代码
Posted alive
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solr java代码相关的知识,希望对你有一定的参考价值。
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
1、controller
@PostMapping("/solrSearchDemand") @ResponseBody public JsonResponse solrSearchDemand(@RequestBody SolrDemandSerachPageListRequest request) { PageListResponse<DemandSolrModel> responseBody = new PageListResponse<>(); Pageable pageable = null; if (StringUtils.isNotBlank(request.getSort()) && StringUtils.isNotBlank(request.getDirection())) { Sort.Order demandSort = new Order(Sort.Direction.valueOf(request.getDirection()), request.getSort()); Sort sort = new Sort(demandSort); pageable = request.getPageable(sort); } else { pageable = request.getPageable("createTime", Sort.Direction.DESC); } final Page<DemandSolrModel> page = demandSolrService.filterTitleUsernameContent(request.getTerm(), request.getTagId(), request.getAppId(),request.getHasHot(), pageable); if (!page.hasContent()) { responseBody.setRecords(Collections.emptyList()); } responseBody.setRecords(page.getContent()); responseBody.setPage(request.getPage()); responseBody.setPageSize(request.getPageSize()); responseBody.setTotalRecords(page.getTotalElements()); return JsonResponse.success(responseBody); }
2、SolrDemandSerachPageListRequest 类如下
public class SolrDemandSerachPageListRequest extends PageListRequest { /** * 标签名查找 */ private Integer tagId; private Integer appId; private Boolean hasHot; public Boolean getHasHot() { return hasHot; } public void setHasHot(Boolean hasHot) { this.hasHot = hasHot; } public Integer getTagId() { return tagId; } public void setTagId(Integer tagId) { this.tagId = tagId; } public Integer getAppId() { return appId; } public void setAppId(Integer appId) { this.appId = appId; } }
PageListRequest类如下:
import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; public class PageListRequest { /** * 搜索的关键字 */ private String term; /** * 排序的字段 */ private String sort; /** * 排序的方向 */ private String direction; /** * 页码从1开始 */ private int page; /** * 条数 */ private int pageSize; public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String getTerm() { return term; } public void setTerm(String term) { this.term = term; } public Sort.Direction getDirection(Sort.Direction defaultDirection) { if (StringUtils.isBlank(direction)) { return defaultDirection; } return Sort.Direction.valueOf(direction); } public Sort getSort(String defaultSortField, Sort.Direction defaultDirection) { if (StringUtils.isNotBlank(sort)) { Sort sort = new Sort(getDirection(defaultDirection), this.sort); return sort; } else { Sort sort = new Sort(getDirection(defaultDirection), defaultSortField); return sort; } } public Pageable getPageable(String defaultSortField, Sort.Direction defaultDirection) { // Pageable 页码是从0开始的,所以要减1操作 PageRequest pageRequest = new PageRequest(this.page - 1, this.pageSize, getSort(defaultSortField, defaultDirection)); return pageRequest; } public Pageable getPageable(Sort defaultSort) { // Pageable 页码是从0开始的,所以要减1操作 PageRequest pageRequest = new PageRequest(this.page - 1, this.pageSize, defaultSort); return pageRequest; } }
3、DemandSolrModel如下:
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;
/*
solrCoreName = "demand"
对应服务器的名字,这里的一定要看我上一篇文章的solr文章里面的managed-schema配置文件里的第(1)点*/
@SolrDocument(solrCoreName = "demand")
public class DemandSolrModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Field
private int id;
@Field
private int userId;
@Field
private int appId;
@Field
private String content;
@Field
private Boolean hasHot;
@Field
private Date createTime;
@Field
private String title;
@Field
private String hyperlink;
@Field
private String fileUrl;
@Field
private String avatarImg;
@Field
private String username;
@Field
private List<String> tagName;
@Field
private List<Integer> tagId;
get set 省
3、PageListResponse如下
import java.util.List;
public class PageListResponse<E> {
/**
* 列表数据
*/
private List<E> records;
/**
* 总的数量
*/
private long totalRecords;
private int page;
private int pageSize;
public List<E> getRecords() {
return records;
}
public void setRecords(List<E> records) {
this.records = records;
}
public long getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(long totalRecords) {
this.totalRecords = totalRecords;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getTotalPages()
*/
public int getTotalPages() {
return pageSize == 0 ? 1 : (int) Math.ceil((double) totalRecords / (double) pageSize);
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
4、service如下:
import com.messcat.imgrnt.solr.model.DemandSolrModel; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface DemandSolrService { Page<DemandSolrModel> filterTitleUsernameContent(String term, Integer tagId, Integer appId,Boolean hasHot, Pageable pageable); void indexDemand(int id); }
5、service实现类如下:
import com.messcat.common.Constants; import com.messcat.framework.common.util.CommonsUtils; import com.messcat.imgrnt.custom.dao.DemandSolrCustomDao; import com.messcat.imgrnt.service.DemandSolrService; import com.messcat.imgrnt.solr.model.DemandSolrModel; import com.messcat.imgrnt.solr.repo.DemandSolrRepository; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.AnyCriteria; import org.springframework.data.solr.core.query.Criteria; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.stereotype.Component; @Component public class DemandSolrServiceImpl implements DemandSolrService { @Resource private SolrTemplate solrTemplate; @Autowired private DemandSolrCustomDao demandSolrCustomDao; @Autowired private DemandSolrRepository demandSolrRepository; @Override public Page<DemandSolrModel> filterTitleUsernameContent(String term, Integer tagId, Integer appId,Boolean hasHot, Pageable pageable) { SimpleQuery simpleQuery = new SimpleQuery(); /** * 如果输入框的搜索条件不为空,则把条件加进去 */ if (StringUtils.isNotBlank(term)) { /*复制域*/ Criteria termCriteria = new Criteria(Constants.FIELD_TEXT).expression(CommonsUtils.quote(term)); simpleQuery.addCriteria(termCriteria); } if (tagId != null) { Criteria contentCriteria = new Criteria("tagId").in(tagId); simpleQuery.addCriteria(contentCriteria); } if (appId != null) { Criteria contentCriteria = new Criteria("appId").is(appId); simpleQuery.addCriteria(contentCriteria); } if (hasHot != null) { Criteria contentCriteria = new Criteria("hasHot").is(hasHot); simpleQuery.addCriteria(contentCriteria); } if (simpleQuery.getCriteria() == null) { simpleQuery.addCriteria(AnyCriteria.any()); } return solrTemplate.queryForPage(simpleQuery.setPageRequest(pageable), DemandSolrModel.class); } @Override public void indexDemand(int id) { /*1:根据新增的demand 的id ,从demang,demand_rel,demand_tag_rel,demand_tag,user表获取数据*/ DemandSolrModel demandSolrModel = demandSolrCustomDao.selectDemandSolrByDemandId(id); /*2:创建索引*/ if (demandSolrModel == null) { return; } demandSolrRepository.save(demandSolrModel); } }
6、SolrTemplate 配置文件如下:
import com.messcat.common.solr.MyHttpSolrClient; import org.apache.http.client.HttpClient; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.impl.BinaryResponseParser; import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.springframework.boot.autoconfigure.solr.SolrProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.repository.config.EnableSolrRepositories; import org.springframework.util.StringUtils;
@SolrDocument(solrCoreName = "demand")
/*basePackages是指solr的实体类model所在的文件夹,因为要映射服务器 @SolrDocument=(solrCoreName="demand")*/ @Configuration @EnableSolrRepositories(basePackages = {"com.messcat.imgrnt.solr"}) @EnableConfigurationProperties(SolrProperties.class) public class SolrConfiguration { private final SolrProperties properties; public SolrConfiguration(SolrProperties properties) { this.properties = properties; } @Bean public SolrClient solrClient(HttpClient httpClient) { return createSolrClient(httpClient); } private SolrClient createSolrClient(HttpClient httpClient) { if (StringUtils.hasText(this.properties.getZkHost())) { return new CloudSolrClient(this.properties.getZkHost()); } return new MyHttpSolrClient(properties.getHost(), httpClient, new BinaryResponseParser()); } @Bean public SolrTemplate solrTemplate(SolrClient server) throws Exception { return new SolrTemplate(server); } }
注:有些包我没弄出来,所以有些代码是导包道不了,是可以用其它替代的,自己自行替换,要看自己的学习能力了
以上是关于solr java代码的主要内容,如果未能解决你的问题,请参考以下文章