<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<bean id="orderInfoSolrServer" class="com.xxxx.SolrCloudServerFactoryBean">
<property name="zkHost" value="${solr.zkHost}"/>
<property name="defaultCollection" value="orderInfo"/>
<property name="zkClientTimeout" value="6000"/>
</bean>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate" scope="singleton">
<constructor-arg ref="orderInfoSolrServer" />
</bean>
<bean id="solrService" class="com.xxxx.SolrServiceImpl">
<property name="solrOperations" ref="solrTemplate" />
</bean>
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.7.2</version> </dependency>
<bean id="orderInfoSolrServer" class="com.xxxx.SolrCloudServerFactoryBean">
<property name="zkHost" value="${solr.zkHost}"/>
<property name="defaultCollection" value="orderInfo"/>
<property name="zkClientTimeout" value="6000"/>
</bean>
package com.jd.fms.prism.solr.service;
import org.apache.http.client.HttpClient;
/**
* solrj spring integration
*
* @author bjchenrui
*/
public class SolrCloudServerFactoryBean implements FactoryBean<SolrServer>, InitializingBean {
private CloudSolrServer cloudSolrServer;
private String zkHost;
private String defaultCollection;
private int maxConnections = 1000;
private int maxConnectionsPerHost = 500;
private int zkClientTimeout = 10000;
private int zkConnectTimeout = 10000;
private Lock lock = new ReentrantLock();
public SolrServer getObject() throws Exception {
return cloudSolrServer;
}
public Class<SolrServer> getObjectType() {
return SolrServer.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
ModifiableSolrParams params = new ModifiableSolrParams();
params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, maxConnections);
params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, maxConnectionsPerHost);
HttpClient client = HttpClientUtil.createClient(params);
LBHttpSolrServer lbServer = new LBHttpSolrServer(client);
lock.lock();
try {
if(cloudSolrServer == null) {
cloudSolrServer = new CloudSolrServer(zkHost, lbServer);
}
} finally {
lock.unlock();
}
cloudSolrServer.setDefaultCollection(defaultCollection);
cloudSolrServer.setZkClientTimeout(zkClientTimeout);
cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
}
public void setCloudSolrServer(CloudSolrServer cloudSolrServer) {
this.cloudSolrServer = cloudSolrServer;
}
public void setZkHost(String zkHost) {
this.zkHost = zkHost;
}
public void setDefaultCollection(String defaultCollection) {
this.defaultCollection = defaultCollection;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public void setMaxConnectionsPerHost(int maxConnectionsPerHost) {
this.maxConnectionsPerHost = maxConnectionsPerHost;
}
public void setZkClientTimeout(int zkClientTimeout) {
this.zkClientTimeout = zkClientTimeout;
}
public void setZkConnectTimeout(int zkConnectTimeout) {
this.zkConnectTimeout = zkConnectTimeout;
}
}
package com.jd.fms.prism.solr.service.impl;
import com.jd.fms.prism.common.utils.DateUtil;
@Service("orderInfoSolrService")
public class OrderInfoNativeSolrServiceImpl {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.FORMATER11);
private static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(DateUtil.FORMATER4);
@Resource(name = "orderInfoSolrServer")
private SolrServer solrServer;
/**
* 创建索引
*
* @param orderInfo
*/
public void creatIndex(OrderInfo orderInfo) throws IOException, SolrServerException {
solrServer.addBean(orderInfo);
solrServer.commit();
}
/**
* 查询条件的生成。支持字段的精确查询,模糊查询,范围查询。
* @param orderIdfilter
* @param queryObj
* @param queryTimeList
* @param sorts
* @return
* @throws Exception
*/
public SolrQuery iniFilter(String orderIdfilter,OrderInfo queryObj,List<QueryTime> queryTimeList, Sort... sorts) throws Exception {
SolrQuery sQuery = new SolrQuery();
String queryQ = "validTag:1";
sQuery.setQuery(queryQ);
StringBuilder filter = new StringBuilder();
if(null != orderIdfilter){
filter.append(orderIdfilter);
queryObj.setOrderId(null);
}
//添加过滤条件
Field[] fields = queryObj.getClass().getDeclaredFields();
String fieldName = "";
String fieldValue = "";
for (Field field:fields){
if(field.isAnnotationPresent(org.apache.solr.client.solrj.beans.Field.class)){
field.setAccessible(true);
fieldName = field.getName();
fieldValue = String.valueOf(field.get(queryObj));
if (null != fieldValue && !"null".equals(fieldValue) && !"".equals(fieldValue) && !"0.0".equals(fieldValue)){
//如果是会员类型,则添加模糊查询
if(fieldName.equals("memberId") || fieldName.equals("orderType")){
fieldValue = "*" + fieldValue + "*";
}
filter.append(fieldName + ":" + fieldValue).append(" AND ");
}
}
}
if(queryTimeList!=null && queryTimeList.size() > 0) {
Iterator<QueryTime> iterator = queryTimeList.iterator();
while(iterator.hasNext()) {
QueryTime queryTime = iterator.next();
String beginDate = simpleDateFormat.format(queryTime.getBeginTime().getTime());
String endDate = simpleDateFormat.format(queryTime.getEndTime().getTime());
filter.append(queryTime.getFieldName() + ":" + "[" + beginDate + " TO " + endDate + "] AND ");
}
}
if(filter.length()>0){
filter.delete(filter.length()-5, filter.length());
}
sQuery.addFilterQuery(filter.toString());
if(sQuery.toString().equals("")){
sQuery.setQuery("*:*");
}
return sQuery;
}
/**
* 查询代码,可以看到我们可以在solr中做聚合,做排序。而且整个过程都是秒级的。
* @param map
* @param queryObj
* @param queryTimeList
* @param page
* @param sorts
* @return
* @throws Exception
*/
public Page<OrderInfo> query(Map map,OrderInfo queryObj, List<QueryTime> queryTimeList, Pageable page, Sort... sorts) throws Exception {
SolrQuery sQuery = iniFilter(null,queryObj,queryTimeList);
//添加分页
if(page != null){
sQuery.setStart(page.getPageNumber()*page.getPageSize());
sQuery.setRows(page.getPageSize());
}
//添加排序
/*if (null != sorts){
sQuery.setSort("orderId",SolrQuery.ORDER.asc);
}*/
QueryResponse response = null;
sQuery.setGetFieldStatistics("orderPrice");
sQuery.setGetFieldStatistics("duePrice");
sQuery.setGetFieldStatistics("diffPrice");
try {
response = solrServer.query(sQuery);
} catch (SolrServerException e) {
e.printStackTrace();
}
SolrDocumentList list = response.getResults();
Map<String, FieldStatsInfo> mapSum = response.getFieldStatsInfo();
String orderPriceSum = null;
if(mapSum.get("orderPrice") != null && !mapSum.get("orderPrice").toString().equals("") ){
orderPriceSum = mapSum.get("orderPrice").getSum().toString();
}
String duePriceSum = null;
if(mapSum.get("duePrice") != null && !mapSum.get("duePrice").toString().equals("") ){
duePriceSum = mapSum.get("duePrice").getSum().toString();
}
String diffPriceSum = null;
if(mapSum.get("diffPrice") != null && !mapSum.get("diffPrice").toString().equals("") ){
diffPriceSum = mapSum.get("diffPrice").getSum().toString();
}
List<OrderInfo> list1 = new ArrayList<OrderInfo>();
DocumentObjectBinder binder = new DocumentObjectBinder();
Iterator iterator = list.iterator();
while(iterator.hasNext()){
OrderInfo orderInfo = binder.getBean(OrderInfo.class, (SolrDocument) iterator.next());
list1.add(orderInfo);
}
map.put("orderPriceSum", orderPriceSum);
map.put("duePriceSum", duePriceSum);
map.put("diffPriceSum", diffPriceSum);
Page<OrderInfo> pageList = new PageImpl<OrderInfo>(list1,page,list.getNumFound());
return pageList;
}
/**
* 我们可以按照key值进行主键查询。
* @param id
* @return
* @throws Exception
*/
public List<OrderInfo> queryByOrderId(String id) throws Exception {
SolrQuery sQuery = new SolrQuery();
String filter = "orderId" + ":" + id;
sQuery.setQuery(filter);
QueryResponse response = null;
try {
response = solrServer.query(sQuery);
} catch (SolrServerException e) {
e.printStackTrace();
}
SolrDocumentList list = response.getResults();
List<OrderInfo> list1 = new ArrayList<OrderInfo>();
DocumentObjectBinder binder = new DocumentObjectBinder();
Iterator iterator = list.iterator();
while(iterator.hasNext()){
OrderInfo orderInfo = binder.getBean(OrderInfo.class, (SolrDocument) iterator.next());
list1.add(orderInfo);
}
return list1;
}
public void deleteAll(OrderInfo orderInfo) throws IOException, SolrServerException {
String sQuery = "*:*";
solrServer.deleteByQuery(sQuery);
}
public void deleteById(String id) {
}
public void createIndexBatch(List<OrderInfo> orderInfoList) throws IOException, SolrServerException {
solrServer.addBeans(orderInfoList);
solrServer.commit();
}
public void deleteBySolrQuery(String solrQuery) throws IOException, SolrServerException {
solrServer.deleteByQuery(solrQuery);
solrServer.commit();
}
public SolrServer getSolrServer() {
return solrServer;
}
public void setSolrServer(SolrServer solrServer) {
this.solrServer = solrServer;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有