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

news/2024/7/5 5:30:52

假如你像用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

转载于:https://www.cnblogs.com/chenying99/p/3819336.html


http://www.niftyadmin.cn/n/4137512.html

相关文章

I.MX6 wpa_supplicant_8 编译问题

/************************************************************************* I.MX6 wpa_supplicant_8 编译问题* 说明&#xff1a;* 在移植wifi过程中&#xff0c;要编译wpa_supplicant_8这个模块&#xff0c;记录一下问题。** …

LeetCode206. 反转链表

难度&#xff1a;简单 题目描述&#xff1a; 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr)…

SQL Server中一个隐性的IO性能杀手-Forwarded record

SQL Server中一个隐性的IO性能杀手-Forwarded record 原文:SQL Server中一个隐性的IO性能杀手-Forwarded record简介 最近在一个客户那里注意到一个计数器很高&#xff08;Forwarded Records/Sec&#xff09;&#xff0c;伴随着间歇性的磁盘等待队列的波动。本篇文章分享什么是…

reactjs学习笔记2--组件的介绍

什么是组件 组件就像是乐高积木&#xff0c;一个完整的房子&#xff0c;可以由砖头一块一块的组成&#xff0c;一块砖头&#xff0c;就可以称为一个组件。 如何创建组件 调用 React.createClass 方法&#xff0c;传入的参数为一个对象&#xff0c;对象必须定义一个 render 方法…

textarea焦点的用法(转)

1.文本框显示默认文字&#xff1a; <textarea>白鸽男孩</textarea> <textarea>白鸽男孩</textarea>   2.鼠标点击文本框&#xff0c;默认文字消失&#xff1a; <textarea οnfοcus”if(value’白鸽男孩’) {value’ ‘}”>白鸽男孩&…

RecyclerView notifyDataSetChanged不起作用

一般listview设置完data后调用notifyDataSetChanged便可刷新布局界面&#xff0c;然而recycleview调用这个方法却没有任何反应。对于很多不熟悉recycleview的话很容易躺坑&#xff0c;折腾了好久。在此记录下。一、recycleview刷新&#xff1a;设置相关属性&#xff1a; recycl…

09.打印“caoliuzhenhao”

printf 命令用于格式化输出&#xff0c; 是echo命令的增强版。它是C语言printf()库函数的一个有限的变形&#xff0c;并且在语法上有些不同。注意&#xff1a;printf 由 POSIX 标准所定义&#xff0c;移植性要比 echo 好。如同 echo 命令&#xff0c;printf 命令也可以输出简单…

Ubuntu14.04 Kylin下 GO语言环境搭建

sudo apt-get install golang gccgo安装 gcc -v 查看 --enable-languagesc,c,objc,obj-c,java,fortran,ada,go,lto --enable-plugin 如果有一个go&#xff0c;说明你的gcc支持golang&#xff0c;那么就执行以下命令安装gcc-go(大家试一下这命令&#xff0c;我不敢确定)&#x…