返回> 网站首页 

[转载]Java-利用bobo-browse实现lucene的分组统计功能

yoours2011-09-25 22:07:05 阅读 1371

简介一边听听音乐,一边写写文章。

         bobo-browse 是一用java写的lucene扩展组件,通过它可以很方便在lucene上实现分组统计功能。

可以从 http://code.google.com/p/bobo-browse/ 上下载和查看相关文档。


下面介绍如何使用:

第一步:设置相关配置文件

bobo-browse 使用了spring,这里主要配置bobo.spring和field.xml两个文件。可以从他的源码例子中找到这两个文件,参考它做相应的修改。

bobo.spring

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.     <bean id="color" class="com.browseengine.bobo.facets.impl.SimpleFacetHandler">  
  7.         <constructor-arg value="color" />  
  8.     </bean>  
  9.       
  10.     <bean id="category" class="com.browseengine.bobo.facets.impl.SimpleFacetHandler">  
  11.         <constructor-arg value="category" />  
  12.     </bean>  
  13.       
  14.     <bean id="city" class="com.browseengine.bobo.facets.impl.PathFacetHandler">  
  15.         <constructor-arg value="city" />  
  16.         <property name="separator" value="/" />  
  17.     </bean>  
  18.       
  19.     <bean id="makemodel" class="com.browseengine.bobo.facets.impl.PathFacetHandler">  
  20.         <constructor-arg value="makemodel" />  
  21.         <property name="separator" value="/" />  
  22.     </bean>  
  23.       
  24.     <bean id="year" class="com.browseengine.bobo.facets.impl.RangeFacetHandler">  
  25.         <constructor-arg value="year" />  
  26.         <constructor-arg value="true" />  
  27.     </bean>  
  28.       
  29.     <bean id="price" class="com.browseengine.bobo.facets.impl.RangeFacetHandler">  
  30.         <constructor-arg value="price" />  
  31.         <constructor-arg>  
  32.             <bean class="com.browseengine.bobo.facets.data.PredefinedTermListFactory">  
  33.                 <constructor-arg value="java.lang.Float" />  
  34.                 <constructor-arg value="00000000000000000000" />  
  35.             </bean>  
  36.         </constructor-arg>  
  37.         <constructor-arg value="true" />  
  38.     </bean>  
  39.       
  40.     <bean id="mileage" class="com.browseengine.bobo.facets.impl.RangeFacetHandler">  
  41.         <constructor-arg value="mileage" />  
  42.         <constructor-arg>  
  43.             <bean class="com.browseengine.bobo.facets.data.PredefinedTermListFactory">  
  44.                 <constructor-arg value="java.lang.Integer"/>  
  45.                 <constructor-arg value="00000000000000000000" />  
  46.             </bean>  
  47.         </constructor-arg>  
  48.         <constructor-arg>  
  49.              <list>  
  50.                 <value>[* TO 12500]</value>  
  51.                 <value>[12501 TO 15000]</value>  
  52.                 <value>[15001 TO 17500]</value>  
  53.                 <value>[17501 TO *]</value>  
  54.              </list>  
  55.         </constructor-arg>  
  56.     </bean>  
  57.   
  58.     <bean id="tags" class="com.browseengine.bobo.facets.impl.MultiValueFacetHandler">  
  59.         <constructor-arg value="tags" />  
  60.     </bean>  
  61.       
  62.     <bean id="handlers" class="java.util.ArrayList">  
  63.         <constructor-arg>  
  64.             <list>  
  65.                 <ref bean="color" />  
  66.                 <ref bean="category" />  
  67.                 <ref bean="city" />  
  68.                 <ref bean="makemodel" />  
  69.                 <ref bean="year" />  
  70.                 <ref bean="price" />  
  71.                 <ref bean="mileage" />  
  72.                 <ref bean="tags" />  
  73.             </list>  
  74.         </constructor-arg>  
  75.     </bean>  
  76. </beans>  

 field.xml(field.xml不是必须的)

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <field-info>  
  3.     <field>  
  4.         <name type="simple">category</name>  
  5.         <param name="preloadcache" value="true"/>  
  6.     </field>  
  7.     <field>  
  8.         <name type="path">city</name>  
  9.                         <param name="preloadcache" value="true"/>  
  10.     </field>  
  11.     <field>  
  12.         <name type="simple">color</name>  
  13.                         <param name="preloadcache" value="true"/>  
  14.     </field>  
  15.     <field>  
  16.         <name type="path">makemodel</name>  
  17.                         <param name="preloadcache" value="true"/>  
  18.     </field>  
  19.     <field>  
  20.         <name type="range">price</name>  
  21.                         <param name="preloadcache" value="false"/>  
  22.                         <param name="value_type" value="float"/>  
  23.                         <param name="format" value="00000000000000000000" />  
  24.                         <param name="display" value=".00"/>  
  25.     </field>  
  26.     <field>  
  27.         <name type="range">year</name>  
  28.                         <param name="preloadcache" value="false"/>  
  29.                         <param name="value_type" value="integer"/>  
  30.                         <param name="format" value="00000000000000000000" />  
  31.     </field>  
  32.     <field>  
  33.         <name type="range">mileage</name>  
  34.                         <param name="preloadcache" value="false"/>  
  35.                         <param name="value_type" value="integer"/>  
  36.                         <param name="format" value="00000000000000000000" />  
  37.     </field>  
  38.     <field>  
  39.         <name type="multi">tags</name>  
  40.         <param name="maxVal" value="15"/>  
  41.         <param name="preloadcache" value="true"/>  
  42.     </field>  
  43. </field-info>  

 

该组件提供了五种FacetHandler 来处理上面配置中的字段,字段中的一些属性设置可以参考他的api文档。

 

simple: (com.browseengine.bobo.facets.impl.SimpleFacetHandler ) Used when there is a discrete set of facet values, for example: color, with values: red,green,blue,white,black. Each document can have only 1 value in this field. When being indexed, this field should not be tokenized.

该字段的值只能对应一个分类或分组,并且该字段在索引是必须为非分词的。

 

 multi: (com.browseengine.bobo.facets.impl.MultiValueFacetHandler ) Similar to simple type field, multi field allows a document to have multiple values. When being indexed, this field can be tokenized. Or alternatively, one can index multiple values in multiple document fields under the same field name.

该字段可以对应多个分类,并且字段需要分词的。

 

compact multi: (com.browseengine.bobo.facets.impl.CompactMultiValueFacetHandler ) Same as MultiValueFacetHandler, multiple values are allowed, the total possible values are limited to 32. However, this is more efficient than MultiValueFacetHandler and has a smaller memory footprint.

 感觉应该和multi类似,我还没用过。

 

path: (com.browseengine.bobo.facets.impl.PathFacetHandler ) Used to denote facet values with hierarchical structure, for example: "A/B/C/D" Each document can have only 1 value in this field. When being indexed, this field should not be tokenized.

该字段我的理解是这样的,当前值属于:A(顶级分类)=>B(二级分类)=>C(三级分类)=>D(四级分类),字段必须为未分词的。

 

range: (com.browseengine.bobo.facets.impl.RangeFacetHandler ) Used to denote a range of facet, e.g. dates, prices etc. Each document can have only 1 value in this field. When being indexed, this field should not be tokenized. Furthermore, the values need to be formatted to ensure sorting by lexical order is the same as the value order.

 范围,不用我介绍了,上面的bobo.spring里有这个例子。

 

参考它自带的例子 将你要进行分组的字段 设置好bobo.spring和field.xml这两个文件。

同时这两个文件要放在和索引文件的同一目录下。

 

第二步:搜索实现

字段配置写好之后,就可以在搜索的java文件中加上一些代码就可以得到统计结果了。

参考文档 http://code.google.com/p/bobo-browse/wiki/GettingStarted

比如要获取color字段按多到少的10个值

 

Java代码
  1. // opening a lucene index  
  2. Directory idx = FSDirectory.open(new File("myidx"));  
  3. IndexReader reader = IndexReader.open(idx);  
  4. // decorate it with a bobo index reader  
  5. BoboIndexReader boboReader = BoboIndexReader.getInstance(reader);  
  6. // creating a browse request  
  7. BrowseRequest br=new BrowseRequest();  
  8. br.setCount(10);  
  9. br.setOffset(0);  
  10. // parse a query  
  11. QueryParser qp = new QueryParser(fields,new StandardAnalyzer());  
  12. Query q=qp.parse(keyword);  
  13. br.setQuery(q);   
  14. // add the facet output specs  
  15. FacetSpec colorSpec = new FacetSpec();  
  16.   
  17. colorSpec.setMaxHitCount(10);  
  18. colorSpec.setOrderBy(FacetSortSpec.OrderHitsDesc);   
  19. br.setFacetSpec("color",colorSpec);  
  20. // perform browse  
  21. Browsable browser=new BoboBrowser(boboReader);  
  22. BrowseResult result=browser.browse(br);  
  23. int totalHits = result.getNumHits();  
  24. BrowseHit[] hits = result.getHits();   
  25. Map<String,FacetAccessible> facetMap = result.getFacetMap();   
  26. FacetAccessible colorFacets = facetMap.get("color");  
  27. List<BrowseFacet> facetVals = colorFacets.getFacets();   
 


效果 可以参考它上面介绍的网站 http://www.simplyhired.com/a/jobs/list/o-13201


C# 可以参考使用 HubbleDotNet - Open source full-text search engine
http://hubbledotnet.codeplex.com/
微信小程序扫码登陆

文章评论

1371人参与,0条评论