1. package com.fox.facet;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.facet.index.FacetFields;
  10. import org.apache.lucene.facet.params.FacetSearchParams;
  11. import org.apache.lucene.facet.search.CountFacetRequest;
  12. import org.apache.lucene.facet.search.DrillDownQuery;
  13. import org.apache.lucene.facet.search.FacetResult;
  14. import org.apache.lucene.facet.search.FacetsCollector;
  15. import org.apache.lucene.facet.taxonomy.CategoryPath;
  16. import org.apache.lucene.facet.taxonomy.TaxonomyReader;
  17. import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
  18. import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
  19. import org.apache.lucene.index.DirectoryReader;
  20. import org.apache.lucene.index.IndexWriter;
  21. import org.apache.lucene.index.IndexWriterConfig;
  22. import org.apache.lucene.search.IndexSearcher;
  23. import org.apache.lucene.search.MatchAllDocsQuery;
  24. import org.apache.lucene.store.Directory;
  25. import org.apache.lucene.store.RAMDirectory;
  26. import org.apache.lucene.util.Version;
  27.  
  28. /*
  29. * Licensed to the Apache Software Foundation (ASF) under one or more
  30. * contributor license agreements. See the NOTICE file distributed with
  31. * this work for additional information regarding copyright ownership.
  32. * The ASF licenses this file to You under the Apache License, Version 2.0
  33. * (the "License"); you may not use this file except in compliance with
  34. * the License. You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing, software
  39. * distributed under the License is distributed on an "AS IS" BASIS,
  40. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. * See the License for the specific language governing permissions and
  42. * limitations under the License.
  43. */
  44.  
  45. /** Shows simple usage of faceted indexing and search. */
  46. public class SimpleFacetsExample {
  47.  
  48. private final Directory indexDir = new RAMDirectory();
  49. private final Directory taxoDir = new RAMDirectory();
  50.  
  51. /** Empty constructor */
  52. public SimpleFacetsExample() {
  53. }
  54.  
  55. private void add(IndexWriter indexWriter, FacetFields facetFields, String... categoryPaths) throws IOException {
  56. Document doc = new Document();
  57.  
  58. List<CategoryPath> paths = new ArrayList<CategoryPath>();
  59. for (String categoryPath : categoryPaths) {
  60. paths.add(new CategoryPath(categoryPath, '/'));
  61. }
  62. facetFields.addFields(doc, paths);
  63. indexWriter.addDocument(doc);
  64. }
  65.  
  66. /** Build the example index. */
  67. private void index() throws IOException {
  68. IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_43, new WhitespaceAnalyzer(
  69. Version.LUCENE_43)));
  70.  
  71. // Writes facet ords to a separate directory from the main index
  72. DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  73.  
  74. // Reused across documents, to add the necessary facet fields
  75. FacetFields facetFields = new FacetFields(taxoWriter);
  76.  
  77. add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
  78. add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
  79. add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
  80. add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
  81. add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");
  82.  
  83. indexWriter.close();
  84. taxoWriter.close();
  85. }
  86.  
  87. /** User runs a query and counts facets. */
  88. private List<FacetResult> search() throws IOException {
  89. DirectoryReader indexReader = DirectoryReader.open(indexDir);
  90. IndexSearcher searcher = new IndexSearcher(indexReader);
  91. TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  92.  
  93. // Count both "Publish Date" and "Author" dimensions
  94. FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Publish Date"), 10),
  95. new CountFacetRequest(new CategoryPath("Author"), 10));
  96.  
  97. // Aggregatses the facet counts
  98. FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
  99.  
  100. // MatchAllDocsQuery is for "browsing" (counts facets
  101. // for all non-deleted docs in the index); normally
  102. // you'd use a "normal" query, and use MultiCollector to
  103. // wrap collecting the "normal" hits and also facets:
  104. searcher.search(new MatchAllDocsQuery(), fc);
  105.  
  106. // Retrieve results
  107. List<FacetResult> facetResults = fc.getFacetResults();
  108.  
  109. indexReader.close();
  110. taxoReader.close();
  111.  
  112. return facetResults;
  113. }
  114.  
  115. /** User drills down on 'Publish Date/2010'. */
  116. private List<FacetResult> drillDown() throws IOException {
  117. DirectoryReader indexReader = DirectoryReader.open(indexDir);
  118. IndexSearcher searcher = new IndexSearcher(indexReader);
  119. TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  120.  
  121. // Now user drills down on Publish Date/2010:
  122. FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
  123. DrillDownQuery q = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
  124. q.add(new CategoryPath("Publish Date/2010", '/'));
  125. FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
  126. searcher.search(q, fc);
  127.  
  128. // Retrieve results
  129. List<FacetResult> facetResults = fc.getFacetResults();
  130.  
  131. indexReader.close();
  132. taxoReader.close();
  133.  
  134. return facetResults;
  135. }
  136.  
  137. /** Runs the search example. */
  138. public List<FacetResult> runSearch() throws IOException {
  139. index();
  140. return search();
  141. }
  142.  
  143. /** Runs the drill-down example. */
  144. public List<FacetResult> runDrillDown() throws IOException {
  145. index();
  146. return drillDown();
  147. }
  148.  
  149. /** Runs the search and drill-down examples and prints the results. */
  150. public static void main(String[] args) throws Exception {
  151. System.out.println("Facet counting example:");
  152. System.out.println("-----------------------");
  153. List<FacetResult> results = new SimpleFacetsExample().runSearch();
  154. for (FacetResult res : results) {
  155. System.out.println(res);
  156. }
  157.  
  158. System.out.println("\n");
  159. System.out.println("Facet drill-down example (Publish Date/2010):");
  160. System.out.println("---------------------------------------------");
  161. results = new SimpleFacetsExample().runDrillDown();
  162. for (FacetResult res : results) {
  163. System.out.println(res);
  164. }
  165. }
  166.  
  167. }

Result:

  1. Facet counting example:
  2. -----------------------
  3. Request: Publish Date nRes=10 nLbl=10
  4. Num valid Descendants (up to specified depth): 3
  5. Publish Date (0.0)
  6. Publish Date/2012 (2.0)
  7. Publish Date/2010 (2.0)
  8. Publish Date/1999 (1.0)
  9. Request: Author nRes=10 nLbl=10
  10. Num valid Descendants (up to specified depth): 4
  11. Author (0.0)
  12. Author/Lisa (2.0)
  13. Author/Frank (1.0)
  14. Author/Susan (1.0)
  15. Author/Bob (1.0)
  16.  
  17. Facet drill-down example (Publish Date/2010):
  18. ---------------------------------------------
  19. Request: Author nRes=10 nLbl=10
  20. Num valid Descendants (up to specified depth): 2
  21. Author (0.0)
  22. Author/Lisa (1.0)
  23. Author/Bob (1.0)

Lucene 4.3 - Facet demo的更多相关文章

  1. Lucene 4.8 - Facet Demo

    package com.fox.facet; /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...

  2. lucene 4.0 - Facet demo

    package com.fox.facet; import java.io.File; import java.io.IOException; import java.util.ArrayList; ...

  3. lucene搜索之facet查询原理和facet查询实例——TODO

    转自:http://www.lai18.com/content/7084969.html Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为 ...

  4. (一)Lucene简介以及索引demo

    一.百度百科 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查 ...

  5. Facet with Lucene

    Facets with Lucene Posted on August 1, 2014 by Pascal Dimassimo in Latest Articles During the develo ...

  6. lucene 索引 demo

    核心util /** * Alipay.com Inc. * Copyright (c) 2004-2015 All Rights Reserved/ */ package com.lucene.de ...

  7. MVC+MQ+WinServices+Lucene.Net Demo

    前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精神,再结合公司项目的实际情况,有了写一个Demo的想法,算是对自己能力 ...

  8. Lucene系列-facet

    1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前提下,观察结果在各维度上的分布(一个维度下各子类的数目). 如jd上搜“手机”,得到4009个商品.其中品牌 ...

  9. lucene 4.4 demo

    ackage com.zxf.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStr ...

随机推荐

  1. CodeForces - 1101D:GCD Counting (树分治)

    You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...

  2. German Collegiate Programming Contest 2013-B:Booking(贪心)

        Booking Pierre is in great trouble today! He is responsible for managing the bookings for the AC ...

  3. Brute Force Sorting(HDU6215)

    题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...

  4. rest-framework之分页器

    rest-framework之分页器 本文目录 一 简单分页(查看第n页,每页显示n条) 二 偏移分页(在第n个位置,向后查看n条数据) 三 CursorPagination(加密分页,只能看上一页和 ...

  5. 《DSP using MATLAB》Problem 6.5

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. hdu1686 Oulipo KMP/AC自动机

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  7. AangularJS相关术语

    1.   数据模型对象(model object)是指$scope对象.$scope对象又是一个简单的JavaScript对象,其中的属性可以被视图访问,也可以同控制器进行交互. 2.  $scope ...

  8. Go Example--接口

    package main import ( "math" "fmt" ) type geometry interface { area() float64 pe ...

  9. oracle之logminer日志分析

    alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; select sysdate from dual; 执行增删操作 alter sy ...

  10. zabbix监控mysql最简单的方法

    该实验基于我的上一篇文章监控第一台主机的基础上 首先,因为水平有限,我选择直接关闭了防火墙和SELinux. 环境: 两台centos7,服务器端IP是192.168.200.128(以下简称主机), ...