设为首页 收藏本站
查看: 1057|回复: 0

[经验分享] solrj 实现多字段分组统计查询(facet)

[复制链接]

尚未签到

发表于 2017-12-20 13:41:12 | 显示全部楼层 |阅读模式
import java.io.IOException;  

import java.io.InputStream;  

import java.text.SimpleDateFormat;  

import java.util.Calendar;  

import java.util.Date;  

import java.util.HashMap;  

import java.util.LinkedHashMap;  

import java.util.List;  

import java.util.Map;  

  
import org.apache.ibatis.io.Resources;
  
import org.apache.ibatis.session.SqlSession;
  
import org.apache.ibatis.session.SqlSessionFactory;
  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  
import org.apache.solr.client.solrj.SolrQuery;
  
import org.apache.solr.client.solrj.SolrQuery.ORDER;
  
import org.apache.solr.client.solrj.SolrServerException;
  
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient;
  
import org.apache.solr.client.solrj.response.Group;
  
import org.apache.solr.client.solrj.response.GroupCommand;
  
import org.apache.solr.client.solrj.response.GroupResponse;
  
import org.apache.solr.client.solrj.response.PivotField;
  
import org.apache.solr.client.solrj.response.QueryResponse;
  
import org.apache.solr.common.SolrDocument;
  
import org.apache.solr.common.SolrDocumentList;
  
import org.apache.solr.common.SolrInputDocument;
  
import org.apache.solr.common.SolrInputField;
  
import org.apache.solr.common.params.GroupParams;
  
import org.apache.solr.common.util.NamedList;
  

  
import com.daan.sql2solr.domain.ParModule;
  
import com.daan.sql2solr.domain.ParModuleMapper;
  
import com.daan.sql2solr.domain.SpecimenInfo;
  
import com.daan.sql2solr.domain.SpecimenInfoMapper;
  


  
public>  

  
     static SqlSessionFactory sqlSessionFactory;
  
     // solr url
  
     static final String URL = "http://192.168.14.59:8983/solr";
  
     // solr应用
  
     static final String SERVER = "khlis_report_info";
  

  
     static ConcurrentUpdateSolrClient client = new ConcurrentUpdateSolrClient(URL + "/" + SERVER, 10000, 2);
  

  
     public static ConcurrentUpdateSolrClient getSolrClient() {
  
         // if (client == null) client = new ConcurrentUpdateSolrClient(URL + "/"
  
         // + SERVER, 10000, 2);
  
         return client;
  
     }
  

  
     public static void main(String[] args) throws IOException, SolrServerException {
  
         // TODO Auto-generated method stub
  
         deleteIndex();
  
         SqlSession session = getSqlSession().openSession();
  
         try {
  
             ParModuleMapper mapper = session.getMapper(ParModuleMapper.class);
  
             List<ParModule> lstModule = mapper.selectParModuleLst();
  
//             createIndexBatch(lstModule);
  
             // for(ParModule item:lstModule){ createIndex(item); }
  
//             deleteIndex();
  
//             createIndexRandom();
  
             // query();
  
              group();

  
//            >  
         } finally {
  
             session.close();
  
             getSolrClient().close();
  
         }
  
     }
  

  
     private static SqlSessionFactory getSqlSession() throws IOException {
  
         if (sqlSessionFactory == null) {
  
             String resource = "mybatis-config.xml";
  
             InputStream inputStream = Resources.getResourceAsStream(resource);
  
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  
             // sqlSessionFactory.getConfiguration().addMapper(ParModuleMapper.class);
  
         }
  
         return sqlSessionFactory;
  
     }
  

  
     public static void insertSpecimenBatch(List<SpecimenInfo> items) throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         Map<String, SolrInputField> fields = new LinkedHashMap<>();
  
         SolrInputField field_testing_forms_id = new SolrInputField("testing_forms_id");
  
         SolrInputField field_test_time = new SolrInputField("test_time");
  
         SolrInputField field_barcode = new SolrInputField("barcode");
  
         SolrInputField field_subject_name = new SolrInputField("subject_name");
  
         SolrInputField field_sex_name = new SolrInputField("sex_name");
  
         fields.put(field_testing_forms_id.getName(), field_testing_forms_id);
  
         fields.put(field_test_time.getName(), field_test_time);
  
         fields.put(field_barcode.getName(), field_barcode);
  
         fields.put(field_subject_name.getName(), field_subject_name);
  
         fields.put(field_sex_name.getName(), field_sex_name);
  
         SolrInputDocument docSource = new SolrInputDocument(fields);
  
         for (SpecimenInfo item : items) {
  
             SolrInputDocument doc = docSource.deepCopy();
  
             doc.getField(field_testing_forms_id.getName()).setValue(item.getTesting_forms_id(), 1.0f);
  
             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
             String dateString = formatter.format(item.getTest_time());
  
             doc.getField(field_test_time.getName()).setValue(dateString, 1.0f);
  
             // doc.getField(field_test_time.getName()).setValue(item.getTest_time(),
  
             // 1.0f);
  
             doc.getField(field_barcode.getName()).setValue(item.getBarcode(), 1.0f);
  
             doc.getField(field_subject_name.getName()).setValue(item.getSubject_name(), 1.0f);
  
             doc.getField(field_sex_name.getName()).setValue(item.getSex_name(), 1.0f);
  
             client.add(doc);
  
         }
  
         client.commit();
  
     };
  

  
     /**
  
      * 新建索引
  
      *
  
      * @throws IOException
  
      * @throws SolrServerException
  
      */
  
     public static void createIndex(ParModule item) throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         Map<String, SolrInputField> fields = new LinkedHashMap<>();
  
         SolrInputField field_id = new SolrInputField("id");
  
         SolrInputField field_module_name = new SolrInputField("module_name");
  
         SolrInputField field_module_code = new SolrInputField("module_code");
  
         fields.put(field_id.getName(), field_id);
  
         fields.put(field_module_name.getName(), field_module_name);
  
         fields.put(field_module_code.getName(), field_module_code);
  
         SolrInputDocument docSource = new SolrInputDocument(fields);
  

  
         SolrInputDocument doc = docSource.deepCopy();
  
         doc.getField(field_id.getName()).setValue(item.getId(), 1.0f);
  
         doc.getField(field_module_name.getName()).setValue(item.getModule_name(), 1.0f);
  
         doc.getField(field_module_code.getName()).setValue(item.getModule_code(), 1.0f);
  
         client.add(doc);
  
     };
  

  
     public static void createIndexBatch(List<ParModule> items) throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         Map<String, SolrInputField> fields = new LinkedHashMap<>();
  
         SolrInputField field_id = new SolrInputField("id");
  
         SolrInputField field_module_name = new SolrInputField("module_name");
  
         SolrInputField field_module_code = new SolrInputField("module_code");
  
         SolrInputField field_module_level = new SolrInputField("module_level");
  
         SolrInputField field_function_type = new SolrInputField("function_type");
  

  
         fields.put(field_id.getName(), field_id);
  
         fields.put(field_module_name.getName(), field_module_name);
  
         fields.put(field_module_code.getName(), field_module_code);
  
         fields.put(field_module_level.getName(), field_module_level);
  
         fields.put(field_function_type.getName(), field_function_type);
  
         SolrInputDocument docSource = new SolrInputDocument(fields);
  
         for (ParModule item : items) {
  
             SolrInputDocument doc = docSource.deepCopy();
  
             doc.getField(field_id.getName()).setValue(item.getId(), 1.0f);
  
             doc.getField(field_module_name.getName()).setValue(item.getModule_name(), 1.0f);
  
             doc.getField(field_module_code.getName()).setValue(item.getModule_code(), 1.0f);
  
             doc.getField(field_module_level.getName()).setValue(item.getModule_level(), 1.0f);
  
             doc.getField(field_function_type.getName()).setValue(item.getFunction_type(), 1.0f);
  
             client.add(doc);
  
         }
  
         client.commit();
  
     };
  

  
     public static void createIndexRandom() throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         Map<String, SolrInputField> fields = new LinkedHashMap<>();
  
         SolrInputField field_id = new SolrInputField("id");
  
         SolrInputField field_module_name = new SolrInputField("module_name");
  
         SolrInputField field_module_code = new SolrInputField("module_code");
  
         fields.put(field_id.getName(), field_id);
  
         fields.put(field_module_name.getName(), field_module_name);
  
         fields.put(field_module_code.getName(), field_module_code);
  
         SolrInputDocument docSource = new SolrInputDocument(fields);
  
         int max = 100 * 10000;
  
         for (int i = 1; i < max; i++) {
  
             SolrInputDocument doc = docSource.deepCopy();
  
             doc.getField(field_id.getName()).setValue(i, 1.0f);
  
             doc.getField(field_module_name.getName()).setValue("module_name" + i, 1.0f);
  
             doc.getField(field_module_code.getName()).setValue("module_code" + i, 1.0f);
  
             client.add(doc);
  
         }
  
         client.commit();
  
     };
  

  
     public static void deleteIndex() throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         client.deleteByQuery("*:*");
  
         client.commit();
  
     }
  

  
     public static void query() throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         SolrQuery query = new SolrQuery();
  
         query.setQuery("module_name:*日志*");
  
         query.setFilterQueries("id:1*");
  
         query.setStart(0);
  
         query.setRows(10);
  
         query.addSort("id", ORDER.desc);
  
         SolrDocumentList docs = client.query(query).getResults();
  
         for (SolrDocument sd : docs) {
  
             System.out.println(sd.getFieldValue("module_name"));
  
         }
  
     }
  

  
     public static void group() throws SolrServerException, IOException {
  
         client = getSolrClient();
  
         SolrQuery query = new SolrQuery();
  
         query.setQuery("module_name:*");
  
         query.setParam(GroupParams.GROUP, true);
  
         // query.setParam(GroupParams.GROUP_FIELD, new
  
         // String[]{"module_level","function_type"});
  
         query.setParam(GroupParams.GROUP_FIELD, "function_type", "module_level");
  
         query.setParam(GroupParams.GROUP_LIMIT, "0");
  
         query.setParam(GroupParams.GROUP_MAIN, false);
  
         query.setParam(GroupParams.GROUP_QUERY, "module_level:1", "module_level:2");
  
         query.setParam(GroupParams.GROUP_LIMIT, "1");
  
         query.setParam("fq", "id:1*", "function_type:*");
  
         QueryResponse response = null;
  
         response = client.query(query);
  
         Map<String, Integer> info = new HashMap<String, Integer>();
  
         GroupResponse groupResponse = response.getGroupResponse();
  
         if (groupResponse != null) {
  
             List<GroupCommand> groupList = groupResponse.getValues();
  
             for (GroupCommand groupCommand : groupList) {
  
                 List<Group> groups = groupCommand.getValues();
  
                 for (Group group : groups) {
  
                     System.out.println(group.getGroupValue() + ":" + group.getResult().getNumFound());
  
                 }
  
             }
  
         }
  
     }
  


  
     public static void>  
         client = getSolrClient();
  
         SolrQuery query = new SolrQuery();
  

  
         query.set("q", "*");
  
         query.setRows(0);
  
         query.addGetFieldStatistics("{!tag=dosum sum=true}_version_");
  
         query.addFacetPivotField("{!stats=dosum}function_type,module_level");
  
         QueryResponse response = client.query(query);
  
         NamedList<List<PivotField>> lstFacet = response.getFacetPivot();
  
         List<PivotField> pfs = lstFacet.get("function_type,module_level");
  
         for (PivotField pf : pfs) {
  
             System.out.println("function_type: " + pf.getValue() + " --- count:" + pf.getCount());
  
             List<PivotField> pfs2 = pf.getPivot();
  
             for (PivotField pf2 : pfs2) {
  
                 System.out.print(" module_level:" + pf2.getValue() + " --- count " + pf2.getCount());
  
                 System.out.println("  _version_  sum:" + pf2.getFieldStatsInfo().get("_version_").getSum());
  
             }
  
         }
  
         System.out.println(response.toString());
  
     }
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426057-1-1.html 上篇帖子: zookeeper中Error contacting service. It is probably not running. 下篇帖子: solr6.3与MySQL结合使用的简明教程(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表