zoukankan      html  css  js  c++  java
  • 【例子】Bobobrowse:lucene分组统计扩展组件

    Bobo-browse是一个基于lucene的分组统计插件,可以完成对搜索结果的分面统计,比如“男装(221) 女装(332)”等。

    这里做首次尝试,只谈使用,不谈原理。用熟了才有可能去研究仔细。

    lucene3 + bobo-browse2.5

    建索引,更新索引跟它没关系,仅关注搜索。索引建立好后,开始进行分面搜索。

    bobo-spring.xml:

    <?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"
    >

    <!--
    com.browseengine.bobo.facets.impl.SimpleFacetHandler no-analyzar
    com.browseengine.bobo.facets.impl.MultiValueFacetHandler analyzar-enable
    com.browseengine.bobo.facets.impl.CompactMultiValueFacetHandler analyzar-enable
    com.browseengine.bobo.facets.impl.PathFacetHandler no-analyzar
    com.browseengine.bobo.facets.impl.RangeFacetHandler no-analyzar
    -->

    <bean id="brand" class="com.browseengine.bobo.facets.impl.MultiValueFacetHandler">
    <constructor-arg value="brand" />
    </bean>

    <bean id="pic_url1" class="com.browseengine.bobo.facets.impl.PathFacetHandler">
    <constructor-arg value="pic_url1" />
    <constructor-arg value="true" />
    <property name="separator" value="/" />
    </bean>

    <bean id="sold_num" class="com.browseengine.bobo.facets.impl.SimpleFacetHandler">
    <constructor-arg value="sold_num" />
    </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.Double"/>
    <constructor-arg value="00000000000000000000" />
    </bean>
    </constructor-arg>
    <constructor-arg>
    <list>
    <value>[* TO 2]</value>
    <value>[3 TO 15000]</value>
    <value>[15001 TO 17500]</value>
    <value>[17501 TO *]</value>
    </list>
    </constructor-arg>
    </bean>

    <bean id="handlers" class="java.util.ArrayList">
    <constructor-arg>
    <list>
    <ref bean="brand" />
    <ref bean="pic_url1" />
    <ref bean="price" />
    <ref bean="sold_num" />
    </list>
    </constructor-arg>
    </bean>
    </beans>


    代码:

    @SuppressWarnings("deprecation")  
    public void testBoboBrowser(){
    String splittype = "pic_url1";
    String splittype2 = "price";
    String splittype3 = "brand";
    String splittype4 = "sold_num";
    try {
    // 1
    IndexReader indexReader = IndexReader.open(
    FSDirectory.open(
    new File("D://workspace2//normandy_normandypositionii//NormandyPositionII//luceneIndex//goods//")));
    BoboIndexReader boboIndexReader = BoboIndexReader.getInstance(indexReader);

    BrowseRequest browseRequest = new BrowseRequest();
    browseRequest.setCount(10);
    browseRequest.setOffset(0);

    // 2
    String indexName = "brand";
    String keywords = "飞扬";
    Analyzer analyzer = new IKAnalyzer();
    QueryParser queryParser = new QueryParser(Version.LUCENE_CURRENT, indexName, analyzer);
    Query query = queryParser.parse(keywords);

    browseRequest.setQuery(query);

    // 3 设置分面类型
    FacetSpec facetSpec = new FacetSpec();
    facetSpec.setMaxCount(10);// 搜索出来的标签数目
    facetSpec.setOrderBy(FacetSortSpec.OrderHitsDesc);

    browseRequest.setFacetSpec(splittype, facetSpec);
    browseRequest.setFacetSpec(splittype2, facetSpec);
    browseRequest.setFacetSpec(splittype3, facetSpec);
    browseRequest.setFacetSpec(splittype4, facetSpec);

    // 4
    Browsable browser = new BoboBrowser(boboIndexReader);
    BrowseResult browseResult = browser.browse(browseRequest);

    // 5
    int totalHits = browseResult.getNumHits();
    BrowseHit[] browseHit = browseResult.getHits();

    System.out.println("total count:"+totalHits);

    // 上面添加了哪些分类字段,这里就有哪些分类字段可以被取值
    for(BrowseHit b : browseHit){
    System.out.println(b.getField(splittype2));
    }

    // 搜索出来的标签
    Map<String,FacetAccessible> facetMap = browseResult.getFacetMap();

    System.out.println("bobo分面:"+splittype+"-----------------------");
    FacetAccessible colorFacets = facetMap.get(splittype);
    List<BrowseFacet> facetVals = colorFacets.getFacets();
    for(BrowseFacet f:facetVals){
    System.out.println(f.getValue() + "(" + f.getHitCount() + ")");
    }

    System.out.println("bobo分面:"+splittype2+"-----------------------");
    colorFacets = facetMap.get(splittype2);
    facetVals = colorFacets.getFacets();
    for(BrowseFacet f:facetVals){
    System.out.println(f.getValue() + "(" + f.getHitCount() + ")");
    }

    System.out.println("bobo分面:"+splittype3+"-----------------------");
    colorFacets = facetMap.get(splittype3);
    facetVals = colorFacets.getFacets();
    for(BrowseFacet f:facetVals){
    System.out.println(f.getValue() + "(" + f.getHitCount() + ")");
    }

    System.out.println("bobo分面:"+splittype4+"-----------------------");
    colorFacets = facetMap.get(splittype4);
    facetVals = colorFacets.getFacets();
    for(BrowseFacet f:facetVals){
    System.out.println(f.getValue() + "(" + f.getHitCount() + ")");
    }
    } catch (CorruptIndexException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ParseException e) {
    e.printStackTrace();
    } catch (BrowseException e) {
    e.printStackTrace();
    }
    }

    结果:

    total count:2
    00000000000000000211
    00000000000000001111
    bobo分面:pic_url1-----------------------
    分类1/(2)
    bobo分面:price-----------------------
    [3 TO 15000](2)
    bobo分面:brand-----------------------
    空间(2)
    飞扬(2)
    bobo分面:sold_num-----------------------
    2(1)
    3(1)


  • 相关阅读:
    typedef void (*funcptr)(void) typedef void (*PFV)(); typedef int32_t (*PFI)();
    STM32 STM32F4 寄存器怎么配置不上, 无法往寄存器写入数据
    GPIO
    JSP和selevt 生命周期详解(JSP的生命周期和select很像,jsp底层就是一个selevt)
    jquery自带的排序方法(js也是)
    GET和POST是HTTP请求的两种基本方法,区别是什么!?
    springboot特性
    restful风格接口类型和优点
    提升必看!!!
    分组函数 partition by 的详解,与order by 区别
  • 原文地址:https://www.cnblogs.com/ibook360/p/2270443.html
Copyright © 2011-2022 走看看