lucene facet bobo-browse实现

  • D4_609853
    了解作者
  • 16.8MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-12 19:02
    上传日期
lucene的facet用bobo-browse实现的办法,可以实现淘宝分类样式
16-lucene-bobo-browse.zip
内容介绍
package com.example; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.index.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; import org.apache.lucene.search.*; import org.apache.lucene.queryParser.*; import com.browseengine.bobo.api.BoboBrowser; import com.browseengine.bobo.api.BoboIndexReader; import com.browseengine.bobo.api.Browsable; import com.browseengine.bobo.api.BrowseFacet; import com.browseengine.bobo.api.BrowseHit; import com.browseengine.bobo.api.BrowseRequest; import com.browseengine.bobo.api.BrowseResult; import com.browseengine.bobo.api.FacetAccessible; import com.browseengine.bobo.api.FacetSpec; import com.browseengine.bobo.api.FacetSpec.FacetSortSpec; import com.browseengine.bobo.facets.FacetHandler; import com.browseengine.bobo.facets.impl.*; import com.browseengine.bobo.facets.data.PredefinedTermListFactory; public class TestBoboBrowse { private Directory indexDir = null; private Version luceneVersion = Version.LUCENE_34; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestBoboBrowse app = new TestBoboBrowse(); app.test1(); } public void test1(){ try{ indexDir = new RAMDirectory(); createIndex(); searchTest(); indexDir.close(); }catch(Exception ex){ ex.printStackTrace(); } } public void createIndex(){ String[][] data = new String[][]{ new String[] {"lenovo", "Intel", "PC Core2 E8200 2GB DDR2 250GB 7200RPM 22LCD", "08998"}, new String[] {"lenovo", "Intel", "PC Core2 E8300 2GB DDR2 320GB 7200RPM 22LCD", "09998"}, new String[] {"lenovo", "Intel", "PC Core2 Q6600 2GB DDR2 320GB 7200RPM 22LCD", "11998"}, new String[] {"lenovo", "Intel", "PC Core2 QX9770 4GB DDR2 320GB 7200RPM RAID-1 22LCD", "19998"}, new String[] {"lenovo", "Intel", "PC pentium E2200 1GB DDR2 160GB 5400RPM 19LCD", "05998"}, new String[] {"hp", "Intel", "PC pentium E2180 1GB DDR2 160GB 7200RPM 20LCD", "06398"}, new String[] {"hp", "Intel", "PC Core2 E8200 2GB DDR2 250GB 5400RPM 22LCD", "08998"}, new String[] {"hp", "Intel", "PC Core2 E6550 2GB DDR2 250GB 7200RPM 20LCD", "07398"}, new String[] {"hp", "Intel", "PC Core2 QX6850 4GB DDR2 320GB 5400RPM 22LCD", "13998"}, new String[] {"asus", "AMD", "PC Core2 QX9650 4GB DDR2 450GB 7200RPM 22LCD", "17998"}, new String[] {"dell", "AMD", "PC Core2 athlon FX76 4GB DDR2 450GB 7200RPM 22LCD", "12998"} }; try{ Analyzer analyzer = new StandardAnalyzer(luceneVersion); // Lucene Version >= 3.2.0 (Version.LUCENE_32) IndexWriterConfig indexConfig = new IndexWriterConfig(luceneVersion, analyzer); indexConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); IndexWriter indexWriter = new IndexWriter(indexDir, indexConfig); // Lucene Version < 3.2.0 (Version.LUCENE_32) // IndexWriter indexWriter = new IndexWriter(indexDir, analyzer, true, MaxFieldLength.LIMITED); for(int i = 0; i < data.length; i++){ Document doc = new Document(); doc.add(new Field("id",i + "", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("vendor",data[i][0], Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("cpu",data[i][1], Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("desc",data[i][2], Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("price",data[i][3], Field.Store.YES, Field.Index.NOT_ANALYZED)); indexWriter.addDocument(doc); } indexWriter.commit(); indexWriter.close(); }catch(Exception ex){ ex.printStackTrace(); } } private void searchTest(){ try{ String fieldName = "desc"; String keywords = "E8200"; QueryParser queryParser = new QueryParser(luceneVersion, fieldName, new StandardAnalyzer(luceneVersion)); Query query = queryParser.parse(keywords); IndexReader indexReader = IndexReader.open(indexDir,true); List<FacetHandler<?>> facetHandlers = new ArrayList<FacetHandler<?>>(); facetHandlers.add(new SimpleFacetHandler("vendor")); facetHandlers.add(new SimpleFacetHandler("cpu")); //facetHandlers.add(new RangeFacetHandler("price", Arrays.asList(new String[]{"[* TO 09998]", "[09999 TO 11998]", "[11999 TO *]"}))); String[] ranges = new String[]{"[00000 TO 09999]", "[10000 TO 11998]", "[11999 TO 30000]"}; facetHandlers.add(new RangeFacetHandler("price", new PredefinedTermListFactory(Integer.class, "0"), Arrays.asList(ranges))); BoboIndexReader boboIndexReader = BoboIndexReader.getInstance(indexReader,facetHandlers); BrowseRequest browseRequest = new BrowseRequest(); browseRequest.setCount(10); browseRequest.setOffset(0); browseRequest.setQuery(query); //SortField[] sortFields = new SortField[2]; //排序 //sortFields[0] = new SortField("vendor", SortField.STRING, true); //sortFields[1] = new SortField("price", SortField.INT, true); //browseRequest.setSort(sortFields); FacetSpec facetSpec = new FacetSpec(); facetSpec.setMaxCount(10);// 搜索出来的标签数目 facetSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); browseRequest.setFacetSpec("vendor", facetSpec); browseRequest.setFacetSpec("cpu", facetSpec); browseRequest.setFacetSpec("price", facetSpec); Browsable browser = new BoboBrowser(boboIndexReader); BrowseResult browseResult = browser.browse(browseRequest); int totalHits = browseResult.getNumHits(); BrowseHit[] browseHit = browseResult.getHits(); for(BrowseHit browseHit2:browseHit){ } IndexSearcher searcher = new IndexSearcher(indexReader); TopDocs topDocs = searcher.search(query, totalHits); System.out.println("length" + topDocs.totalHits); ScoreDoc [] scoreDocs = topDocs.scoreDocs; Document doc = searcher.doc(scoreDocs[0].doc); System.out.println(doc.get("desc")); System.out.println("=====Total records: "+totalHits); // 获取分组统计结果 Map<String,FacetAccessible> facetMap = browseResult.getFacetMap(); if(facetMap.size() > 0){ System.out.println("-------Vendor-----------------------");
评论
    相关推荐
    • lucene开发实例
      lucene相关开发实例 包含了lucene基本应用。。相关资料等。。
    • lucene实例lucene实例
      lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例
    • 行业分类-设备装置-一种修改Lucene索引文件中词的方法及装置.zip
      行业分类-设备装置-一种修改Lucene索引文件中词的方法及装置
    • 基于Lucene4.8的空间检索
      针对已知坐标search一定范围内的其他point,根据分类或名称进行search,源文件为类似"PVID","ELPVID","POINAME" 的UTF-8的csv文件
    • lucene3.0 例子
      lucene3.0 例子lucene3.0 例子 lucene3.0 例子 ,很好的学习,只有原代原,jar 包自己加上去就OK了
    • lucene学习文档
      从网上找的资料,然后生成的pdf文档,后来发现网上已经有了,不过还是和大家分享一下,里面含一个sql+lucene实例
    • Lucene搜索例子
      Lucene搜索的一个例子,web工程,有分页。使用Lucene建立索引,实现文档的检索,并把检索结果显示在页面上,对显示结果做分页
    • lucene学习笔记
      有很简单的实例 内容详实 适合初学者主要是lucene的基本操作
    • Lucene 3.0 原理
      Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档