Lucene 4.8 - Facet Demo
package com.fox.facet; /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.facet.DrillDownQuery;
import org.apache.lucene.facet.DrillSideways;
import org.apache.lucene.facet.DrillSideways.DrillSidewaysResult;
import org.apache.lucene.facet.FacetField;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version; /** Shows simple usage of faceted indexing and search. */
public class SimpleFacetsExample48 { private final Directory indexDir = new RAMDirectory();
private final Directory taxoDir = new RAMDirectory();
private final FacetsConfig config = new FacetsConfig(); /** Empty constructor */
public SimpleFacetsExample48() {
config.setHierarchical("Publish Date", true);
} /** Build the example index. */
private void index() throws IOException {
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_48, new WhitespaceAnalyzer(
Version.LUCENE_48))); // Writes facet ords to a separate directory from the main index
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); Document doc = new Document();
doc.add(new FacetField("Author", "Bob"));
doc.add(new FacetField("Publish Date", "2010", "10", "15"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new FacetField("Author", "Lisa"));
doc.add(new FacetField("Publish Date", "2010", "10", "20"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new FacetField("Author", "Lisa"));
doc.add(new FacetField("Publish Date", "2012", "1", "1"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new FacetField("Author", "Susan"));
doc.add(new FacetField("Publish Date", "2012", "1", "7"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new FacetField("Author", "Frank"));
doc.add(new FacetField("Publish Date", "1999", "5", "5"));
indexWriter.addDocument(config.build(taxoWriter, doc)); indexWriter.close();
taxoWriter.close();
} /** User runs a query and counts facets. */
private List<FacetResult> facetsWithSearch() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); FacetsCollector fc = new FacetsCollector(); // MatchAllDocsQuery is for "browsing" (counts facets
// for all non-deleted docs in the index); normally
// you'd use a "normal" query:
FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); // Retrieve results
List<FacetResult> results = new ArrayList<FacetResult>(); // Count both "Publish Date" and "Author" dimensions
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
results.add(facets.getTopChildren(10, "Author"));
results.add(facets.getTopChildren(10, "Publish Date")); indexReader.close();
taxoReader.close(); return results;
} /**
* User runs a query and counts facets only without collecting the matching
* documents.
*/
private List<FacetResult> facetsOnly() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); FacetsCollector fc = new FacetsCollector(); // MatchAllDocsQuery is for "browsing" (counts facets
// for all non-deleted docs in the index); normally
// you'd use a "normal" query:
searcher.search(new MatchAllDocsQuery(), null /* Filter */, fc); // Retrieve results
List<FacetResult> results = new ArrayList<FacetResult>(); // Count both "Publish Date" and "Author" dimensions
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc); results.add(facets.getTopChildren(10, "Author"));
results.add(facets.getTopChildren(10, "Publish Date")); indexReader.close();
taxoReader.close(); return results;
} /**
* User drills down on 'Publish Date/2010', and we return facets for
* 'Author'
*/
private FacetResult drillDown() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Passing no baseQuery means we drill down on all
// documents ("browse only"):
DrillDownQuery q = new DrillDownQuery(config); // Now user drills down on Publish Date/2010:
q.add("Publish Date", "2010");
FacetsCollector fc = new FacetsCollector();
FacetsCollector.search(searcher, q, 10, fc); // Retrieve results
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
FacetResult result = facets.getTopChildren(10, "Author"); indexReader.close();
taxoReader.close(); return result;
} /**
* User drills down on 'Publish Date/2010', and we return facets for both
* 'Publish Date' and 'Author', using DrillSideways.
*/
private List<FacetResult> drillSideways() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Passing no baseQuery means we drill down on all
// documents ("browse only"):
DrillDownQuery q = new DrillDownQuery(config); // Now user drills down on Publish Date/2010:
q.add("Publish Date", "2010"); DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
DrillSidewaysResult result = ds.search(q, 10); // Retrieve results
List<FacetResult> facets = result.facets.getAllDims(10); indexReader.close();
taxoReader.close(); return facets;
} /** Runs the search example. */
public List<FacetResult> runFacetOnly() throws IOException {
index();
return facetsOnly();
} /** Runs the search example. */
public List<FacetResult> runSearch() throws IOException {
index();
return facetsWithSearch();
} /** Runs the drill-down example. */
public FacetResult runDrillDown() throws IOException {
index();
return drillDown();
} /** Runs the drill-sideways example. */
public List<FacetResult> runDrillSideways() throws IOException {
index();
return drillSideways();
} /** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
System.out.println("Facet counting example:");
System.out.println("-----------------------");
SimpleFacetsExample48 example1 = new SimpleFacetsExample48();
List<FacetResult> results1 = example1.runFacetOnly();
System.out.println("Author: " + results1.get(0));
System.out.println("Publish Date: " + results1.get(1)); System.out.println("Facet counting example (combined facets and search):");
System.out.println("-----------------------");
SimpleFacetsExample48 example = new SimpleFacetsExample48();
List<FacetResult> results = example.runSearch();
System.out.println("Author: " + results.get(0));
System.out.println("Publish Date: " + results.get(1)); System.out.println("\n");
System.out.println("Facet drill-down example (Publish Date/2010):");
System.out.println("---------------------------------------------");
System.out.println("Author: " + example.runDrillDown()); System.out.println("\n");
System.out.println("Facet drill-sideways example (Publish Date/2010):");
System.out.println("---------------------------------------------");
for (FacetResult result : example.runDrillSideways()) {
System.out.println(result);
}
} }
Result:
Facet counting example:
-----------------------
Author: dim=Author path=[] value=5 childCount=4
Lisa (2)
Bob (1)
Susan (1)
Frank (1) Publish Date: dim=Publish Date path=[] value=5 childCount=3
2010 (2)
2012 (2)
1999 (1) Facet counting example (combined facets and search):
-----------------------
Author: dim=Author path=[] value=5 childCount=4
Lisa (2)
Bob (1)
Susan (1)
Frank (1) Publish Date: dim=Publish Date path=[] value=5 childCount=3
2010 (2)
2012 (2)
1999 (1) Facet drill-down example (Publish Date/2010):
---------------------------------------------
Author: dim=Author path=[] value=4 childCount=2
Bob (2)
Lisa (2) Facet drill-sideways example (Publish Date/2010):
---------------------------------------------
dim=Publish Date path=[] value=15 childCount=3
2010 (6)
2012 (6)
1999 (3) dim=Author path=[] value=6 childCount=2
Bob (3)
Lisa (3)
Lucene 4.8 - Facet Demo的更多相关文章
- Lucene 4.3 - Facet demo
package com.fox.facet; import java.io.IOException; import java.util.ArrayList; import java.util.List ...
- lucene 4.0 - Facet demo
package com.fox.facet; import java.io.File; import java.io.IOException; import java.util.ArrayList; ...
- lucene搜索之facet查询原理和facet查询实例——TODO
转自:http://www.lai18.com/content/7084969.html Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为 ...
- (一)Lucene简介以及索引demo
一.百度百科 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查 ...
- Facet with Lucene
Facets with Lucene Posted on August 1, 2014 by Pascal Dimassimo in Latest Articles During the develo ...
- lucene 索引 demo
核心util /** * Alipay.com Inc. * Copyright (c) 2004-2015 All Rights Reserved/ */ package com.lucene.de ...
- MVC+MQ+WinServices+Lucene.Net Demo
前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精神,再结合公司项目的实际情况,有了写一个Demo的想法,算是对自己能力 ...
- Lucene系列-facet
1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前提下,观察结果在各维度上的分布(一个维度下各子类的数目). 如jd上搜“手机”,得到4009个商品.其中品牌 ...
- lucene 4.4 demo
ackage com.zxf.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStr ...
随机推荐
- Vue.js学习使用心得(二)——自定义指令
自定义指令 除了核心功能默认内置的指令,Vue 也允许注册自定义指令. 自定义指令可以定义全局指令,也可以定义局部指令. 使用 directives 选项来自定义指令. 定义全局指令: <div ...
- lecture1-Word2vec实战班-七月在线nlp
nltk的全称是natural language toolkit,是一套基于python的自然语言处理工具集.自带语料库.词性分类库.自带分类分词等功能.强大社区支持.很多简单版wrapper 文本处 ...
- 系统间通信——RPC架构设计
架构设计:系统间通信(10)——RPC的基本概念 1.概述经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的 ...
- C++ Tips
1. 虚函数不能是内联的 因为“内联”是指“在编译期间用被调用的函数体本身来代替函数调用的指令,”但是虚函数的“虚”是指“直到运行时才能知道要调用的是哪一个函数.”如果编译器在某个函数的调用点不知道具 ...
- Python学习之---Python中的内置函数(方法)(更新中。。。)
add(item) #将item添加到s中,如果item已经在s中,则无任何效果 break #退出循环,不会再运行循环中余下的代码 bool() #将参数转换为布尔型 by ...
- 计算字符串最后一个单词的长度,单词以空格隔开。 java算法
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = ne ...
- golang xml parent node add attribute without struct
question: golang encoding/xml: foo>bar,attr - foo ignored solution: you can replace output resul ...
- 在CentOS 6上使用 AWStats 分析 httpd 和 Tomcat 日志
准备工作: Awstats 是由perl语言编写的,所以要首先准备好awstats的运行环境.# yum install –y perl* Apache 一.首先,要安装apache服务器,并且启 ...
- AD10 没有原理图是否可以修改 PCB
AD10 没有原理图是否可以修改 PCB 有朋友问 AD 是否可以在没有原理的情况下修改 PCB 呢? 答案是肯定的,可以. 比如增加元件和网络,可以先增加元件封装,再打开网络管理给焊盘加上网络. 相 ...
- COLUMN_FORMAT 的值:FIXED、DYNAMIC、DEFAULT 的区别(待补充)
参考===MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_ ...