用Lucene实现分组,facet功能,FieldCache

假如你像用lucene来作分组,比如按类别分组,这种功能,好了你压力大了,lucene本身是不支持分组的。

当你想要这个功能的时候,就可能会用到基于lucene的搜索引擎solr。

不过也可以通过编码通过FieldCache和单字段,对索引进行分组,比如:想构造类别树。大类里面还有小类那种。

这个功能实现起来可能会比较麻烦,主要是lucene提供的支持也不多,参考资料也不多。

(以下代码都是我在做测试的时候做的,可以稍作修改满足相应需求。)

//用于分组统计的对象GroupCollector

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Scorer;

public class GroupCollector extends Collector {

private GroupField gf = new GroupField();// 保存分组统计结果
    private int docBase;
    // fieldCache
    private String[] fc;

@Override
    public boolean acceptsDocsOutOfOrder() {
        return true;
    }

@Override
    public void collect(int doc) throws IOException {
        // 因为doc是每个segment的文档编号,需要加上docBase才是总的文档编号
        final int docId = doc + this.docBase;
        // 添加的GroupField中,由GroupField负责统计每个不同值的数目
        this.gf.addValue(this.fc[docId]);

}

@Override
    public void setNextReader(IndexReader arg0, int arg1) throws IOException {
        this.docBase = this.docBase;

}

@Override
    public void setScorer(Scorer arg0) throws IOException {

}

public GroupField getGf() {
        return this.gf;
    }

public void setGf(GroupField gf) {
        this.gf = gf;
    }

public int getDocBase() {
        return this.docBase;
    }

public void setDocBase(int docBase) {
        this.docBase = docBase;
    }

public String[] getFc() {
        return this.fc;
    }

public void setFc(String[] fc) {
        this.fc = fc;
    }

}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用于保存分组统计后每个字段的分组结果
 */
public class GroupField {

/**
     * 字段名
     */
    private String name;
    /**
     * 商品类型的对象列表
     */
    private List<SimpleCategory> values = new ArrayList<SimpleCategory>();
    /**
     * 保存字段值和文档个数的对应关系
     */
    private Map<String, Integer> countMap = new HashMap<String, Integer>();

public Map<String, Integer> getCountMap() {
        return this.countMap;
    }

public void setCountMap(Map<String, Integer> countMap) {
        this.countMap = countMap;
    }

public String getName() {
        return this.name;
    }

public void setName(String name) {
        this.name = name;
    }

public List<SimpleCategory> getValues() {
        return this.values;
    }

public void setValues(List<SimpleCategory> values) {
        this.values = values;
    }

/**
     * 用于商品对象list的构造
     * 
     * @param value
     */
    public void addValue(String value) {
        if ((value == null) || "".equals(value)) return;
        // 对于多值的字段,支持按空格拆分
        final String[] temp = value.split(",");

if (this.countMap.get(temp[1]) == null) {
            this.countMap.put(temp[1], 1);
            // 构造商品类型临时对象
            final SimpleCategory simpleCategory = new SimpleCategory();

simpleCategory.setCategoryId(Integer.parseInt(temp[0]));
            simpleCategory.setCategoryName(temp[1]);
            simpleCategory.setParentId(Integer.parseInt(temp[2]));
            simpleCategory.setSortIndex(temp[3]);
            simpleCategory.setParentCategoryName(temp[4]);
            // simpleCategory.setAdImag(temp[5]);
            // simpleCategory.setParentAdImage(temp[6]);
            this.values.add(simpleCategory);
        }
        else {
            this.countMap.put(temp[1], this.countMap.get(temp[1]) + 1);
        }
        // for( String str : temp ){
        // if(countMap.get(str)==null){
        // countMap.put(str,1);
        // values.add(str);
        // }
        // else{
        // countMap.put(str, countMap.get(str)+1);
        // }
        // }
    }
    // class ValueComparator implements Comparator<String>{
    //
    // // public int compare(String value0, String value1) {
    // // if(countMap.get(value0)>countMap.get(value1)){
    // // return -1;
    // // }
    // // else if(countMap.get(value0)<countMap.get(value1)){
    // // return 1;
    // // }
    // // return 0;
    // // }
    // }
}

自己构建想返回的对象

/**
 * 用于将lucene索引中的商品类型CategoryIndex字段,转换成商品类型的一个对象。
 * 
 * @author xiaozd
 * 
 */
public class SimpleCategory extends BaseModel {

private static final long serialVersionUID = -2345212345526771266L;
    private int parentId;
    private int categoryId;
    private String categoryName;
    private String sortIndex;
    private int goodsCount;
    private String parentCategoryName;

public int getParentId() {
        return this.parentId;
    }

public void setParentId(int parentId) {
        this.parentId = parentId;
    }

public int getCategoryId() {
        return this.categoryId;
    }

public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

public String getCategoryName() {
        return this.categoryName;
    }

public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

public String getSortIndex() {
        return this.sortIndex;
    }

public void setSortIndex(String sortIndex) {
        this.sortIndex = sortIndex;
    }

public static long getSerialversionuid() {
        return SimpleCategory.serialVersionUID;
    }

public int getGoodsCount() {
        return this.goodsCount;
    }

public void setGoodsCount(int goodsCount) {
        this.goodsCount = goodsCount;
    }

public String getParentCategoryName() {
        return this.parentCategoryName;
    }

public void setParentCategoryName(String parentCategoryName) {
        this.parentCategoryName = parentCategoryName;
    }

}

/**
     * 查询商品的所有类型,方式:通过索引分组查询所有类型。
     *     @return    Map<String, String> 第一个参数表示商品类型id,第二个String表示商品类型名称
     */
    public List<SimpleCategory> getGoodsCategory() {
        
        List<SimpleCategory> values=new ArrayList<SimpleCategory>();
        try {
            
            IndexReader reader = IndexReader.open(FSDirectory.open(new File(luceneSearchPath)), true); // only searching, so read-only=true

//读取"modified"字段值,放到fieldCache中
            final String[] fc=FieldCache.DEFAULT.getStrings(reader, "categoryIndex");
            IndexSearcher searcher = new IndexSearcher(reader);
            //GroupCollector是自定义文档收集器,用于实现分组统计
            GroupCollector myCollector=new GroupCollector();
            myCollector.setFc(fc);
            searcher.search(new MatchAllDocsQuery(), myCollector);
            //GroupField用来保存分组统计的结果
            GroupField gf=myCollector.getGf();
            values=gf.getValues();
            for (SimpleCategory value : values) {
                System.out.println("商品类型名称:  "+value +"  数量:"+gf.getCountMap().get(value.getCategoryName())+"   商品父类型名称: "+value.getParentCategoryName());
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return values;
    }

http://blog.csdn.net/xiaozhengdong/article/details/7035607

用Lucene实现分组,facet功能,FieldCache,布布扣,bubuko.com

时间: 2024-07-28 14:43:07

用Lucene实现分组,facet功能,FieldCache的相关文章

指尖上的电商---(9).net开发Solr中的Facet功能

上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计,这一节我们看看怎样使用.NET开发Solr中的Facet功能.在讲Facet功能的同时, 我们看下.Net中怎样使用Solr查询.使用的客户端工具是easysorl.net,大家可以去codeplex下载.这个工具很好用. 看如下图,下图就是我们要演示的功能   1.模糊查询 模糊查询就是搜索指定的汉字得到一个结果.下面的示例就是查询商品名称中包含白色的所有商品,最终得到的结果如下图 代码 public void Quer

一步一步跟我学习lucene(16)---lucene搜索之facet查询查询示例(2)

本篇是接一步一步跟我学习lucene(14)---lucene搜索之facet索引原理和facet查询实例(http://blog.csdn.net/wuyinggui10000/article/details/45973769),上篇主要是统计facet的dim和每个种类对应的数量,个人感觉这个跟lucene的group不同的在于facet的存储类似于hash(key-field-value)形式的,而group则是单一的map(key-value)形式的,虽然都可以统计某一品类的数量,显然f

一步一步跟我学习lucene(14)---lucene搜索之facet查询原理和facet查询实例

Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为例,我们选择某一个品牌,系统会将该品牌对应的商品展示出来,效果图如下: 如上图,我们关注的是品牌,选购热点等方面,对于类似的功能我们用lucene的term查询当然可以,但是在数据量特别大的情况下还用普通查询来实现显然会因为FSDirectory.open等耗时的操作造成查询效率的低下,同时普通查询是全部document都扫描一遍,这样显然造成了查询效率低: lucene提供了facet

js 多个checkedBox分组互斥功能实现 由于QQ邮件无法发送暂且将js写在博客园

今天有一项目需要实现多个checkbox分组实现组内互斥功能 . 具体要求如下:大的分类分为A B C 三类, 其中 A   B 中又分为 A1 A2 A3   B1 B2 B3  二级分类,二级分类下又有 A11  A12 A13 A14  A21 A22 A31 A32  B11 B12 B13 B14 ... .要求实现 A B C 互斥 A1 A2 A3 B1 B2 B3 组内互斥.具体结构及代码如下 具体功能树如下: A A1 A11 A12 A13 A2 A21 A22 A3 A31

MYSQL-实现ORACLE 和SQLserver数据中- row_number() over(partition by ) 分组排序功能

网上看见了好多例子都基本上是一样的,没有过多的解释,对于一个初学MySQL来说有点难,我把部分转摘过来如下 原文:http://www.cnblogs.com/buro79xxd/archive/2012/08/29/2662489.html 要求目标:1.确定需求: 根据部门来分组,显示各员工在部门里按薪水排名名次. 创建表格:2.来创建实例数据: drop table if exists heyf_t10; create table heyf_t10 (empid int ,deptid i

【转载】使用Lucene.NET实现数据检索功能

1.索引的管理 //指定索引库文件存放文件位置 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(this.IndexDataDir), new NativeFSLockFactory()); //判断索引文件目录是否存在 bool isExist = IndexReader.IndexExists(directory); if (isExist) { if (IndexWriter.IsLocked(directory)) {

相似group by的分组计数功能

之前同事发过一个语句,实现的功能比較简单,相似group by的分组计数功能,由于where条件有like,又无法用group by来实现. SELECT a.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9 from (select count(*) N0 from tbl_loginfo_20141110 where keyrecord like '0%' or keyrecord like 'GJ_0%') a, (select count(*

类似group by的分组计数功能

之前同事发过一个语句,实现的功能比较简单,类似group by的分组计数功能,因为where条件有like,又无法用group by来实现. SELECT a.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9 from (select count(*) N0 from tbl_loginfo_20141110 where keyrecord like '0%' or keyrecord like 'GJ_0%') a, (select count(*

MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能

MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQL里需要实现这样的功能,我们只能用一些灵活的办法: 1.首先我们来创建实例数据: drop table if exists heyf_t10; create table heyf_t10 (empid int ,deptid int ,salary decimal(10,2) ); insert i