您现在的位置: 365建站网 > 365学习 > 利用bobo-browse 实现lucene的分组统计功能

利用bobo-browse 实现lucene的分组统计功能

文章来源:365jz.com     点击数:796    更新时间:2009-10-04 11:12   参与评论

转载出处:http://blog.csdn.net/strayly/archive/2009/08/08/4424889.aspx

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

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

下面介绍如何使用:

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

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

bobo.spring

 view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/Schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 
    <bean id="color" class="com.browseengine.bobo.facets.impl.SimpleFacetHandler"> 
        <constructor-arg value="color" /> 
    </bean
      
    <bean id="category" class="com.browseengine.bobo.facets.impl.SimpleFacetHandler"> 
        <constructor-arg value="category" /> 
    </bean
      
    <bean id="city" class="com.browseengine.bobo.facets.impl.PathFacetHandler"> 
        <constructor-arg value="city" /> 
        <property name="separator" value="/" /> 
    </bean
      
    <bean id="makemodel" class="com.browseengine.bobo.facets.impl.PathFacetHandler"> 
        <constructor-arg value="makemodel" /> 
        <property name="separator" value="/" /> 
    </bean
      
    <bean id="year" class="com.browseengine.bobo.facets.impl.RangeFacetHandler"> 
        <constructor-arg value="year" /> 
        <constructor-arg value="true" /> 
    </bean
      
    <bean id="price" class="com.browseengine.bobo.facets.impl.RangeFacetHandler"> 
        <constructor-arg value="price" /> 
        <constructor-arg> 
            <bean class="com.browseengine.bobo.facets.data.PredefinedTermListFactory"> 
                <constructor-arg value="java.lang.Float" /> 
                <constructor-arg value="00000000000000000000" /> 
            </bean
        </constructor-arg> 
        <constructor-arg value="true" /> 
    </bean
      
    <bean id="mileage" class="com.browseengine.bobo.facets.impl.RangeFacetHandler"> 
        <constructor-arg value="mileage" /> 
        <constructor-arg> 
            <bean class="com.browseengine.bobo.facets.data.PredefinedTermListFactory"> 
                <constructor-arg value="java.lang.Integer"/> 
                <constructor-arg value="00000000000000000000" /> 
            </bean
        </constructor-arg> 
        <constructor-arg> 
             <list
                <value>[* TO 12500]</value
                <value>[12501 TO 15000]</value
                <value>[15001 TO 17500]</value
                <value>[17501 TO *]</value
             </list
        </constructor-arg> 
    </bean
 
    <bean id="tags" class="com.browseengine.bobo.facets.impl.MultiValueFacetHandler"> 
        <constructor-arg value="tags" /> 
    </bean
      
    <bean id="handlers" class="java.util.ArrayList"> 
        <constructor-arg> 
            <list
                <ref bean="color" /> 
                <ref bean="category" /> 
                <ref bean="city" /> 
                <ref bean="makemodel" /> 
                <ref bean="year" /> 
                <ref bean="price" /> 
                <ref bean="mileage" /> 
                <ref bean="tags" /> 
            </list
        </constructor-arg> 
    </bean
</beans>  
 

field.xml(前两天收到软件原作者John Wang的邮件 说field.xml不是必须的。)

 view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
<field-info> 
    <field
        <name type="simple">category</name
        <param name="preloadcache" value="true"/> 
    </field
    <field
        <name type="path">city</name
                        <param name="preloadcache" value="true"/> 
    </field
    <field
        <name type="simple">color</name
                        <param name="preloadcache" value="true"/> 
    </field
    <field
        <name type="path">makemodel</name
                        <param name="preloadcache" value="true"/> 
    </field
    <field
        <name type="range">price</name
                        <param name="preloadcache" value="false"/> 
                        <param name="value_type" value="float"/> 
                        <param name="format" value="00000000000000000000" /> 
                        <param name="display" value=".00"/> 
    </field
    <field
        <name type="range">year</name
                        <param name="preloadcache" value="false"/> 
                        <param name="value_type" value="integer"/> 
                        <param name="format" value="00000000000000000000" /> 
    </field
    <field
        <name type="range">mileage</name
                        <param name="preloadcache" value="false"/> 
                        <param name="value_type" value="integer"/> 
                        <param name="format" value="00000000000000000000" /> 
    </field
    <field
        <name type="multi">tags</name
        <param name="maxVal" value="15"/> 
        <param name="preloadcache" value="true"/> 
    </field
</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个值

view plaincopy to clipboardprint?// opening a lucene index  

Directory idx = FSDirectory.open(new File("myidx"));  

IndexReader reader = IndexReader.open(idx);  

// decorate it with a bobo index reader  

 BoboIndexReader boboReader = BoboIndexReader.getInstance(reader);  

// creating a browse request  

BrowseRequest br=new BrowseRequest();  

br.setCount(10);  

br.setOffset(0);  

// parse a query  

QueryParser qp = new QueryParser(fields,new StandardAnalyzer());  

Query q=qp.parse(keyword);  

br.setQuery(q);   

// add the facet output specs  

FacetSpec colorSpec = new FacetSpec();    

colorSpec.setMaxHitCount(10);  

colorSpec.setOrderBy(FacetSortSpec.OrderHitsDesc);   

br.setFacetSpec("color",colorSpec);  

 // perform browse  

Browsable browser=new BoboBrowser(boboReader);  

BrowseResult result=browser.browse(br);  

int totalHits = result.getNumHits();  

BrowseHit[] hits = result.getHits();   

Map<String,FacetAccessible> facetMap = result.getFacetMap();   

FacetAccessible colorFacets = facetMap.get("color");  

List<BrowseFacet> facetVals = colorFacets.getFacets();  

经本人测试,效率还不错 ,有问题欢迎与我站内联系。

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


获取源码:http://sourceforge.net/projects/bobo-browse/develop

api:http://test.project-voldemort.com:8080/job/bobo-trunk/javadoc/

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (796人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------