solr的多条件组合查询

今天,再工作中遇到了solr的多条件组合查询,于是在网上查到一些博客--多条件查询,这篇写的不错,但是我也想把我自己的记录下来。话不多说,直接放代码。注意:,在接收的参数由于都是String类型的,所以顺序要保持一致,否则得到的结果可能会错可能没有值。

public SearchResult search(String title,String category_name, String prod_status,String brand,String type, int start, int rows) throws Exception{
        SolrQuery query = new SolrQuery();

        query.setStart((start - 1) * rows);
        query.setRows(rows);

        StringBuilder params = new StringBuilder();  //创建一个条件组合的字符串

        if (title != null){     //第一个条件无法确定是否会有值,所以需要判断
            params.append("product_title:" + title);
        }else {
            params.append("product_title:*");   //如果title为null,这个条件是查询所有,这样后面的AND就不会报错
        }
        //下面是我当时所需要的多个条件
        if (category_name != null){
            params.append(" AND product_category_name:" + category_name);
        }

        if(brand != null){
            params.append(" AND product_brand:" + brand);
        }

        if (prod_status != null){
            params.append(" AND product_status:" + prod_status);
        }

        if(type != null){
            params.append(" AND product_type:" + type);
        }

        query.setQuery(params.toString());     //将条件组合字符串set进去
        query.set("df","product_title");

        //执行查询
        SearchResult result = searchDao.search(query);

        return result;
    }
下面是得到的结果
{"code":1,"footer":null,"message":"成功","pageCount":28,"pageSize":10,"rows":[{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://img30.360buyimg.com/popWaterMark/jfs/t2878/308/1846689128/360704/a56351fa/574cfbcfN57939256.jpg\"> \n <img src=\"http://img30.360buyimg.com/popWaterMark/jfs/t2785/344/1856885691/337011/b09ca93d/574cfbcfN2b3f6e01.jpg\"> \n","id":14,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100011041/b733b3f1-9461-498d-b1f0-83eb2b269f59.jpg;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/568550?searchType=1","show_price":"5.51","source_site":"ZCY","title":"晨光(M&G)聘书 16K聘书内芯10张(ASC99324) (不含聘书)","type":"无","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <div style=\"width: 800px\"> \n  <p><img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100036020/a4e7290f-3428-4e11-adfe-50df5be28355.jpg\" title=\"42-4.jpg\"></p> \n </div> \n","id":93,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100036020/a0a99a5c-ce6b-4c0e-85d9-60826dc2c4a8.gif;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100036020/8f26c289-cbce-483b-bf84-fe41c1b96cf0.jpg;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100036020/2a394cfa-8902-46b3-9cbb-ea29194cba6e.jpg;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/10206675?searchType=1","show_price":"48.50","source_site":"ZCY","title":"晨光 M&G 优事贴PET十分条自粘指示贴 AS26OA0102 2*10 48mm*12mm*10 (混色) 20张/条 10条/包","type":"CG-AS26OA0102 ","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2018032713032323149583.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2018032713032323149583.jpg\"> \n","id":250,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com//2018032713021433414238.png;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/10170495?searchType=1","show_price":"3.00","source_site":"ZCY","title":"晨光Y-03便利贴","type":"晨光Y-03","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2018032713045678218003.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2018032713045678218003.jpg\"> \n","id":256,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com//2018032713040943697494.png;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/10170504?searchType=1","show_price":"2.00","source_site":"ZCY","title":"晨光Y-45便利贴","type":"晨光Y-45","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564129361389.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564129361389.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564389114614.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564389114614.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564682345395.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564682345395.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564919922219.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313564919922219.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565158477525.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565413258016.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565413258016.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565668867593.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565883775004.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313565883775004.jpg\"> \n <img src=\"http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313570124415455.jpg\" title=\"Image: http://zcy-item.img-cn-hangzhou.aliyuncs.com/2017102313570124415455.jpg\"> \n","id":460,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017102313541896715558.jpg;http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017102313541849848069.jpg;http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017102313542037877792.jpg;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/2880127?searchType=1","show_price":"32.25","source_site":"ZCY","title":"5本装晨光荣誉证书加厚绒面烫金证书封面封皮奖状外壳内芯纸大12K","type":"ASC99308","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365564842770235389988.jpg\" style=\"\" title=\"1.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365565055274316774121.jpg\" style=\"\" title=\"2.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365565259965741415685.jpg\" style=\"\" title=\"3.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365565447469346342862.jpg\" style=\"\" title=\"4.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365565709973009640909.jpg\" style=\"\" title=\"5.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365565933413361524276.jpg\" style=\"\" title=\"6.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365566177166487580520.jpg\" style=\"\" title=\"7.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365566567796483920015.jpg\" style=\"\" title=\"8.jpg\"></p> \n <p><img src=\"http://www.qijijie.com/ueditor/net/upload/image/20160709/6360365566783424247060812.jpg\" style=\"\" title=\"9.jpg\"></p> \n <p><br></p> \n","id":498,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100010950/outer/62a939be-73ac-4773-965f-e5e69105c895.png;http://www.qijijie.com/Upload/Shop/Images/Product/20160709/201607091007166462261.png;http://www.qijijie.com/Upload/Shop/Images/Product/20160709/201607091007189118946.png;http://www.qijijie.com/Upload/Shop/Images/Product/20160709/201607091007204275487.png;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/555359?searchType=1","show_price":"4.27","source_site":"ZCY","title":"晨光 /M&G A5时光集缝线本 48页时尚记事本彩色唯美笔记本APYFP737","type":"APYFP737","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/e5e792f5330c4e59b51599dabc26aaff.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/eb51a8fab1c448f99d7ab4b81c30dad9.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/60a91b88e99b41f09154cf278baa03fe.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/713deb7c67634679aed2e65452a7f6bc.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/f5b455288e7847a18c9fa0d59ef738f6.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/40c0eaad404b4eb0acac7b2468ab5326.jpg\" alt=\"\"> \n","id":2080,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/102ff429-6077-4e9a-a722-360e16ea3975;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/54520de6-8ffe-4229-832c-eba9998d4dc7;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/b6a5fbed-13b8-42a9-a600-dfc41f6b6970;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/2bf28bfc-ca19-4f1a-9444-abbc503eacec;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/07308fd4-674a-45a6-8ebc-d4a15ae38d7b;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/ff7f15c9-1da3-45e1-a002-cbb3e7340bde;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/3019382?searchType=1","show_price":"4.45","source_site":"ZCY","title":"晨光/M&G APYJR411 无线装订本60页商务必备 B5","type":"APYJR411","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/5d24784628934cd8a5e5d2a70ba26cd1.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/1412a87e58d741a1aadcd0886931f03c.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/33a4a0735b494078aacbb7d740b6a2cf.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/a8aafbbb66b4491a98b5b7690d5d6b3a.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/4aff458a72754ec191854f3015791ce7.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/5ea671a673724e2cb5d18706d2a7620a.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/d1f50c3cd00f4cfba57b11991a3092d3.jpg\"> \n <img alt=\"\" src=\"http://f.dainif.com/upfile/20170627/2af8f5d5b05d48448cba9d271fd5dda0.jpg\"> \n","id":2077,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/26d18c48-6a39-438a-aeb0-bce16a5ff7fb;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/f273e588-08d1-44e9-832a-91835c0afea5;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/088b6a7f-ccd6-488c-bf90-c530a869ef99;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/cf0adba3-ac1a-49ad-9a65-abba409ef613;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/9abf7ba2-4ed4-47de-923f-7171e3cd93e8;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/1389254?searchType=1","show_price":"5.91","source_site":"ZCY","title":"晨光/M&G ASC99315 尊贵特种纸荣誉证书12K ","type":"ASC99315","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://f.dainif.com/upfile/20170627/59c7cae5b417459a9c35930eae73c202.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/bf512efe42764267b73b5ef003edf709.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/dbb8041cf8604a409a354c27c35483de.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/4ef77c53288a4a819d2924625eca579d.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/b00f7db1e7464c2ab92f3fa72b0dccb1.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/a87c6f12ac36415396c143631f3b571c.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/b44c57f4c91c4dfdbc867923073b8d8a.jpg\" alt=\"\"> \n <img src=\"http://f.dainif.com/upfile/20170627/acbdbd677d70485786d034400cbb2a68.jpg\" alt=\"\"> \n","id":2076,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/ac876d8e-67f7-4edf-8585-5ced83265e21;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/18962f93-d022-4c06-80b9-b1dd072f9e53;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/86e3aaa8-3d16-44ee-a524-1e9ec9e7461e;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/20659fcf-f819-4bfb-8572-0d06bc8fb177;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/27dbc7ac-e7df-4605-98bf-81b7fc6ac23a;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/1389382?searchType=1","show_price":"14.37","source_site":"ZCY","title":"晨光/M&G ASC99323 聘书内芯纸12K ","type":"ASC99323","view_video":""},{"attributes":"","brand":"晨光/M&G","category_name":"便签本/便条纸/N次贴","description":" \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/7bede66138ea43ea9ec236d300e99c93.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/97c44cb4987f4db2823d368f22ce6920.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/b02d1c80444548758540a9b626fc018f.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/cc2e09b582554abfbb3c8b04db779061.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/56a5df333f874984bb2ad989a2db833c.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/d8e0c998f4a14ad2bd641efeb351e8be.jpg\" alt=\"\"> \n <img src=\"http://static.jvheip.com/upload/oldfile/upfile/20170627/3a328e952e87452aa82863c9e3151443.jpg\" alt=\"\"> \n","id":2083,"image_list":"http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/a3ebcd6d-0f8d-4341-91ee-55f97de46ce1;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/732af96e-733b-46a4-8def-61633cf396ff;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/710ec98d-10e6-4276-9774-c14ec0362b8b;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/e965be4b-8224-4ac7-91bf-58851824a422;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/35890834-2d57-472b-a0c2-4871510f32af;http://zcy-item.img-cn-hangzhou.aliyuncs.com/100017237/fa2efe9d-b324-46bd-94c5-f4692f263f1a;","images":[],"prod_status":"0","prod_url":"https://www.zcy.gov.cn/items/3019388?searchType=1","show_price":"7.55","source_site":"ZCY","title":"晨光/M&G APYJZ411 A4无线装订本80页商务必备","type":"APYJZ411","view_video":""}],"total":278}

下面是在solr的图形界面上进行展示的操作


product_title:* 与*:*所查到的数据是一样多的,域是可以随便换


图形界面也适合多条件查询,代码用debug所得到的跟这个是一样的q

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值