首页 > 代码库 > solr 基本命令二(权重查找)

solr 基本命令二(权重查找)

package zr.com.solr.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.Query;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.FacetField;
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.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.GroupParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.junit.Test;

/**
 * 
 * @author lf
 *
 */
public class SolrUtils {
    
    public static void main(String[] args) {
        try {
//            SolrUtils.addData();
//            SolrUtils.queryData();
//            System.out.println("===================");
//            SolrUtils.weightQuery();
//            SolrUtils.deleteByID("0");
//            SolrUtils.queryData();
//            SolrUtils.countGroupQuery();
            SolrUtils.groupQuery();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    
      private static HttpSolrServer server = null;
      
      static{
          server = new HttpSolrServer("http://localhost:28080");
      }
      
      
      
     /**
      * 添加数据
      */
      public  static void addData() throws SolrServerException, IOException{
          for (int i = 500; i < 600; i++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("name", "wr");
            doc.addField("id", ""+i);
            doc.addField("age", i);
            doc.addField("team", "Team-15");
            doc.addField("hoby", "swimming");
            doc.addField("content", "www"+i);
            server.add(doc);
        }
          server.commit();
          System.out.println("server:"+server);
      }
    
      /**
       * 查询
       */
      public static void queryData() throws SolrServerException{
          /**
           * 第一种方式查找
           */
          /*ModifiableSolrParams params = new ModifiableSolrParams();
          // 设置查询的条件
          params.set("q", "name:www");
          // 设置获取的字段
          params.set("fl", "name age hoby id content");
          // 过滤查询(年龄在5到8之间)
//          params.set("fq", "age:[5 TO 8]");
          // 不包含(首尾)的范围查询
//          params.set("fq", "age:{5 TO 8}");
          // 设置查询的起始位置
          params.set("start", "0");
//          params.set("wt", "json");
          
          QueryResponse response = server.query(params);
          SolrDocumentList result = response.getResults();
          System.out.println("数量:"+result.size());
          for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
          }*/
          
          /**
           * 第二种方式查找
           */
          SolrQuery query = new SolrQuery();
          // 查询条件
          String queryStr = "name:*";
          // 添加查询条件
          query.setQuery(queryStr);
          // 设置起始值
          query.setStart(0);
          // 设置每一页显示的数量
          query.setRows(500);
          // 执行查询
          QueryResponse response = server.query(query);
          
          SolrDocumentList list = response.getResults();
          System.out.println("数量:"+list.size());
          for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
          }
          
          
      }
      
      /**
       * 权重查找
     * @throws SolrServerException 
       */
      public static void weightQuery() throws SolrServerException{
          
          SolrQuery query = new SolrQuery();
          /*********权重查找**********/
          /* qf对默认查询增加权重比值,
           * 比如:fieldOne^1.9 fieldTwo fieldThree^5 ,
           * 值越大权重越大
          */
          String queryStr = "name:www^2 OR content:www*^15";  
          query.setQuery(queryStr);
          query.setStart(0);
          query.setRows(10);
          QueryResponse response = server.query(query);
          
          SolrDocumentList list = response.getResults();
          System.out.println("数量:"+list.size());
          for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
          }
      }
      
      /**
       * 删除
       * @throws SolrServerException
       * @throws IOException
       */
      public static void deleteByID(String id) throws SolrServerException, IOException{
          UpdateResponse uResponse = server.deleteById(id);
          server.commit();
      }
      
      /**
       *  分组统计
       *  
       */
    public static void countGroupQuery() throws SolrServerException{
        SolrQuery query = new SolrQuery();
        query.setQuery("name:*");
        // 是否按照每组数量高低排序
        query.setIncludeScore(false);
        // 是否分组查询
        query.setFacet(true);
        //设置返回结果条数,如果你时分组查询,你就设置为0
        query.setRows(0);
        // 限制每次返回的结果数
        query.setFacetLimit(5);
        // 添加分组的字段
        query.addFacetField("name");
        
        QueryResponse response = server.query(query);
        //返回分组的结果(这里要和addFacetField相对应)
        List<Count> returnList = response.getFacetField("name").getValues();
        System.out.println("数量:"+returnList.size());
        for (Count count : returnList) {
            if (count.getCount() > 0) {
                // 打印每组的名字和数量
                System.out.println(count.getName()+","+count.getCount());
            }
        }
    }
    /**
     * 分组查询
     * @throws SolrServerException 
     */
    public static void groupQuery() throws SolrServerException{
        SolrQuery query = new SolrQuery();
        query.setQuery("schoolName:*");
        query.setParam("group", true);
        query.setParam("group.field", "schoolName");
        query.setParam("group.limit", "5");
        query.setParam("group.ngroups", true);
        QueryResponse response = server.query(query);
        GroupResponse gResponse = response.getGroupResponse();
        List<GroupCommand> commands = gResponse.getValues();
        if (commands != null) {
            for (GroupCommand groupCommand : commands) {
                System.out.println("总分组的个数:"+groupCommand.getNGroups().longValue());
                for (Group group : groupCommand.getValues()) {
                    System.out.println("-----group:"+group);
                    SolrDocumentList list = group.getResult();
                    for (SolrDocument solrDocument : list) {
                        String id = solrDocument.get("id").toString();
                        String schoolName = solrDocument.get("schoolName").toString();
                        String courseName = solrDocument.get("courseName").toString();
                        
                        System.out.println("id:"+id+",schoolName:"+schoolName+",courseName:"+courseName);
                    }
                }
            }
        }
    }
}

 

solr 基本命令二(权重查找)