bobo-browse 是一用java写的lucene扩展组件,通过它可以很方便在lucene上实现分组统计功能。
可以从http://code.google.com/p/bobo-browse/上下载和查看相关文档。
下面介绍如何使用:
第一步:设置相关配置文件
bobo-browse 使用了spring,这里主要配置bobo.spring和field.xml两个文件。可以从他的源码例子中找到这两个文件,参考它做相应的修改。
bobo.spring
<?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="catCode" class="com.browseengine.bobo.facets.impl.PathFacetHandler">
<constructor-arg value="catCode" />
<property name="separator" value="/" />
</bean>
<bean id="handlers" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="catCode" />
</list>
</constructor-arg>
</bean>
</beans>
field.xml(前两天收到软件原作者John Wang的邮件 说field.xml不是必须的。)
<?xml version="1.0" encoding="UTF-8"?>
<field-info>
<field>
<name type="path">catCode</name>
<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
从转来的文章的例子,好像有的方法已不适用了,可能原作者采用的JDK和boboBrowser版本与我的不同。下面是我的测试代码。
/**
* @param args
*/
public static void main(String[] args) {
Directory idx;
try {
idx = FSDirectory.getDirectory(new File("D://DATAMANAGER//INDEX//OAKSYSTEM"));
IndexReader reader = IndexReader.open(idx,true);
// decorate it with a bobo index reader
BoboIndexReader boboReader = BoboIndexReader.getInstance(reader);
Browsable browser=new BoboBrowser(boboReader);
// creating a browse request
BrowseRequest br=new BrowseRequest();
br.setCount(10);
br.setOffset(0);
// parse a query
QueryParser qp = new QueryParser("docName",new StandardAnalyzer());
Query q=qp.parse("广");
br.setQuery(q);
// add the facet output specs
FacetSpec colorSpec = new FacetSpec();
colorSpec.setMaxCount(10);
colorSpec.setOrderBy(FacetSortSpec.OrderHitsDesc);
br.setFacetSpec("catCode",colorSpec);
// perform browse
BrowseResult result=browser.browse(br);
int totalHits = result.getNumHits();
BrowseHit[] hits = result.getHits();
System.out.println("totalHits:"+totalHits+";hits="+hits.toString());
Map<String,FacetAccessible> facetMap = result.getFacetMap();
FacetAccessible colorFacets = facetMap.get("catCode");
List<BrowseFacet> facetVals = colorFacets.getFacets();
for(java.util.Iterator<BrowseFacet> iter=facetVals.iterator();iter.hasNext();){
BrowseFacet bf = (BrowseFacet)iter.next();
System.out.print(bf.getValue()+"="+bf.getHitCount()+",");
}
} catch (IOException e) {
e.printStackTrace();
} catch (BrowseException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
经本人测试,效率还不错 ,有问题欢迎与我站内联系。
效果可以参考它上面介绍的网站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/
标签:bobo,browse,lucene,field,values,new,com From: https://blog.51cto.com/u_2544485/7395958