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

[经验分享] solr的facet解读

[复制链接]

尚未签到

发表于 2015-7-16 13:51:32 | 显示全部楼层 |阅读模式
  开发采用更加灵活的solr搜索服务器来实现分层功能。
  1.QueryResponse类:
  view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
public class QueryResponse extends SolrResponseBase   
{   
  // Direct pointers to known types   
  private NamedList _header = null;   
  private SolrDocumentList _results = null;   
  private NamedList _sortvalues = null;   
  private NamedList _facetInfo = null;   
  private NamedList _debugInfo = null;   
  private NamedList _highlightingInfo = null;   
  private NamedList _spellInfo = null;   
  
  // Facet stuff   
  private Map _facetQuery = null;   
  private List _facetFields = null;   
  private List _limitingFacets = null;   
  private List _facetDates = null;   
  
  // Highlight Info   
  private Map _highlighting = null;   
  
  // SpellCheck Response   
  private SpellCheckResponse _spellResponse = null;   
     
  // Debug Info   
  private Map _debugMap = null;   
  private Map _explainMap = null;   
  
  // utility variable used for automatic binding -- it should not be serialized   
  private transient final SolrServer solrServer;   
//.....   
//.....   
private void extractFacetInfo( NamedList info )   
  {   
    // Parse the queries   
    _facetQuery = new HashMap();   
    NamedList fq = (NamedList) info.get( "facet_queries" );   
    for( Map.Entry entry : fq ) {   
      _facetQuery.put( entry.getKey(), entry.getValue() );   
    }   
      
    // Parse the facet info into fields   
    // TODO?? The list could be  or ?  If always  then we can switch to   
    NamedList ff = (NamedList) info.get( "facet_fields" );   
    if( ff != null ) {   
      _facetFields = new ArrayList( ff.size() );   
      _limitingFacets = new ArrayList( ff.size() );   
         
      long minsize = _results.getNumFound();   
      for( Map.Entry facet : ff ) {   
        FacetField f = new FacetField( facet.getKey() );   
        for( Map.Entry entry : facet.getValue() ) {   
          f.add( entry.getKey(), entry.getValue().longValue() );   
        }   
           
        _facetFields.add( f );   
        FacetField nl = f.getLimitingFields( minsize );   
        if( nl.getValueCount() > 0 ) {   
          _limitingFacets.add( nl );   
        }   
      }   
    }   
      
    //Parse date facets   
    NamedList df = (NamedList) info.get("facet_dates");   
    if (df != null) {   
      // System.out.println(df);   
      _facetDates = new ArrayList( df.size() );   
      for (Map.Entry facet : df) {   
        // System.out.println("Key: " + facet.getKey() + " Value: " + facet.getValue());   
        NamedList values = facet.getValue();   
        String gap = (String) values.get("gap");   
        Date end = (Date) values.get("end");   
        FacetField f = new FacetField(facet.getKey(), gap, end);   
           
        for (Map.Entry entry : values)   {   
          try {   
            f.add(entry.getKey(), Long.parseLong(entry.getValue().toString()));   
          } catch (NumberFormatException e) {   
            //Ignore for non-number responses which are already handled above   
          }   
        }   
           
        _facetDates.add(f);   
      }   
    }   
  }   
}  
public class QueryResponse extends SolrResponseBase
{
  // Direct pointers to known types
  private NamedList _header = null;
  private SolrDocumentList _results = null;
  private NamedList _sortvalues = null;
  private NamedList _facetInfo = null;
  private NamedList _debugInfo = null;
  private NamedList _highlightingInfo = null;
  private NamedList _spellInfo = null;
  // Facet stuff
  private Map _facetQuery = null;
  private List _facetFields = null;
  private List _limitingFacets = null;
  private List _facetDates = null;
  // Highlight Info
  private Map _highlighting = null;
  // SpellCheck Response
  private SpellCheckResponse _spellResponse = null;
  
  // Debug Info
  private Map _debugMap = null;
  private Map _explainMap = null;
  // utility variable used for automatic binding -- it should not be serialized
  private transient final SolrServer solrServer;
//.....
//.....
private void extractFacetInfo( NamedList info )
  {
    // Parse the queries
    _facetQuery = new HashMap();
    NamedList fq = (NamedList) info.get( "facet_queries" );
    for( Map.Entry entry : fq ) {
      _facetQuery.put( entry.getKey(), entry.getValue() );
    }
   
    // Parse the facet info into fields
    // TODO?? The list could be  or ?  If always  then we can switch to
    NamedList ff = (NamedList) info.get( "facet_fields" );
    if( ff != null ) {
      _facetFields = new ArrayList( ff.size() );
      _limitingFacets = new ArrayList( ff.size() );
      
      long minsize = _results.getNumFound();
      for( Map.Entry facet : ff ) {
        FacetField f = new FacetField( facet.getKey() );
        for( Map.Entry entry : facet.getValue() ) {
          f.add( entry.getKey(), entry.getValue().longValue() );
        }
        
        _facetFields.add( f );
        FacetField nl = f.getLimitingFields( minsize );
        if( nl.getValueCount() > 0 ) {
          _limitingFacets.add( nl );
        }
      }
    }
   
    //Parse date facets
    NamedList df = (NamedList) info.get("facet_dates");
    if (df != null) {
      // System.out.println(df);
      _facetDates = new ArrayList( df.size() );
      for (Map.Entry facet : df) {
        // System.out.println("Key: " + facet.getKey() + " Value: " + facet.getValue());
        NamedList values = facet.getValue();
        String gap = (String) values.get("gap");
        Date end = (Date) values.get("end");
        FacetField f = new FacetField(facet.getKey(), gap, end);
        
        for (Map.Entry entry : values)   {
          try {
            f.add(entry.getKey(), Long.parseLong(entry.getValue().toString()));
          } catch (NumberFormatException e) {
            //Ignore for non-number responses which are already handled above
          }
        }
        
        _facetDates.add(f);
      }
    }
  }
}
  _facetFields:所有的分层结果  使用 getFacetField(String name) 获得结果
  _limitingFacets:facetFields中个数大于0的分层,使用getLimitingFacets()返回所有Count个数大于0的FacetField。
  _facetDates:根据日期的分层结果 getFacetDate(String name)
  例外,要使用Facet功能,除了设置 Field外,还需要将facet属性设置为true,默认是false的。
  view plaincopy to clipboardprint?
params.set("facet", "true");   
params.set("facet.field", "site_search");   
params.set("facet.field", "kindtag_search");
  

  1. FacetField 内嵌了 Count类,Count类保存了被分层出来的的 词 以及该词在搜有搜索出来的文档中出现的次数。
  
view plaincopy to clipboardprint?
package org.apache.solr.client.solrj.response;   
  
import java.io.Serializable;   
import java.util.ArrayList;   
import java.util.Date;   
import java.util.List;   
  
import org.apache.solr.client.solrj.util.ClientUtils;   
   
/**  
  * A utility class to hold the facet response.  It could use the NamedList container,  
  * but for JSTL, it is nice to have something that implements List so it can be iterated  
  *   
  * @version $Id: FacetField.java 638357 2008-03-18 13:12:27Z gsingers $  
  * @since solr 1.3  
  */  
public class FacetField implements Serializable   
{   
   public static class Count implements Serializable   
   {   
     private String _name = null;   
     private long _count = 0;   
     // hang onto the FacetField for breadcrumb creation convenience   
     private FacetField _ff = null;   
        
     public Count( FacetField ff, String n, long c )   
     {   
       _name = n;   
       _count = c;   
       _ff = ff;   
     }   
        
     public String getName() {   
       return _name;   
     }   
        
     public void setName( String n )   
     {   
       _name = n;   
     }   
  
     public long getCount() {   
       return _count;   
     }   
        
     public void setCount( long c )   
     {   
       _count = c;   
     }   
        
     public FacetField getFacetField() {   
       return _ff;   
     }   
        
     @Override  
     public String toString()   
     {   
       return _name+" ("+_count+")";   
     }   
        
     public String getAsFilterQuery() {   
       if (_ff.getName().equals("facet_queries")) {   
         return _name;   
       }   
       return   
          ClientUtils.escapeQueryChars( _ff._name ) + ":" +   
          ClientUtils.escapeQueryChars( _name );   
     }   
   }   
      
   private String      _name   = null;   
   private List _values = null;   
   private String _gap = null;   
   private Date _end = null;   
  
//.........   
//.........方法   
  
   }  
package org.apache.solr.client.solrj.response;
  import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
  import org.apache.solr.client.solrj.util.ClientUtils;

/**
  * A utility class to hold the facet response.  It could use the NamedList container,
  * but for JSTL, it is nice to have something that implements List so it can be iterated
  *
  * @version $Id: FacetField.java 638357 2008-03-18 13:12:27Z gsingers $
  * @since solr 1.3
  */
public class FacetField implements Serializable
{
   public static class Count implements Serializable
   {
     private String _name = null;
     private long _count = 0;
     // hang onto the FacetField for breadcrumb creation convenience
     private FacetField _ff = null;
     
     public Count( FacetField ff, String n, long c )
     {
       _name = n;
       _count = c;
       _ff = ff;
     }
     
     public String getName() {
       return _name;
     }
     
     public void setName( String n )
     {
       _name = n;
     }
  public long getCount() {
       return _count;
     }
     
     public void setCount( long c )
     {
       _count = c;
     }
     
     public FacetField getFacetField() {
       return _ff;
     }
     
     @Override
     public String toString()
     {
       return _name+" ("+_count+")";
     }
     
     public String getAsFilterQuery() {
       if (_ff.getName().equals("facet_queries")) {
         return _name;
       }
       return
          ClientUtils.escapeQueryChars( _ff._name ) + ":" +
          ClientUtils.escapeQueryChars( _name );
     }
   }
   
   private String      _name   = null;
   private List _values = null;
   private String _gap = null;
   private Date _end = null;
  //.........
//.........方法
  }

  一个FacetField实例拥有多个Count实例。
  
  

运维网声明 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-87348-1-1.html 上篇帖子: solr索引 下篇帖子: SOLR安装与配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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