package com.fox.facet;

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.index.FacetFields;
import org.apache.lucene.facet.params.FacetSearchParams;
import org.apache.lucene.facet.search.CountFacetRequest;
import org.apache.lucene.facet.search.DrillDownQuery;
import org.apache.lucene.facet.search.FacetResult;
import org.apache.lucene.facet.search.FacetsCollector;
import org.apache.lucene.facet.taxonomy.CategoryPath;
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; /*
* 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.
*/ /** Shows simple usage of faceted indexing and search. */
public class SimpleFacetsExample { private final Directory indexDir = new RAMDirectory();
private final Directory taxoDir = new RAMDirectory(); /** Empty constructor */
public SimpleFacetsExample() {
} private void add(IndexWriter indexWriter, FacetFields facetFields, String... categoryPaths) throws IOException {
Document doc = new Document(); List<CategoryPath> paths = new ArrayList<CategoryPath>();
for (String categoryPath : categoryPaths) {
paths.add(new CategoryPath(categoryPath, '/'));
}
facetFields.addFields(doc, paths);
indexWriter.addDocument(doc);
} /** Build the example index. */
private void index() throws IOException {
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_43, new WhitespaceAnalyzer(
Version.LUCENE_43))); // Writes facet ords to a separate directory from the main index
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); // Reused across documents, to add the necessary facet fields
FacetFields facetFields = new FacetFields(taxoWriter); add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5"); indexWriter.close();
taxoWriter.close();
} /** User runs a query and counts facets. */
private List<FacetResult> search() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Count both "Publish Date" and "Author" dimensions
FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Publish Date"), 10),
new CountFacetRequest(new CategoryPath("Author"), 10)); // Aggregatses the facet counts
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader); // MatchAllDocsQuery is for "browsing" (counts facets
// for all non-deleted docs in the index); normally
// you'd use a "normal" query, and use MultiCollector to
// wrap collecting the "normal" hits and also facets:
searcher.search(new MatchAllDocsQuery(), fc); // Retrieve results
List<FacetResult> facetResults = fc.getFacetResults(); indexReader.close();
taxoReader.close(); return facetResults;
} /** User drills down on 'Publish Date/2010'. */
private List<FacetResult> drillDown() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Now user drills down on Publish Date/2010:
FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
DrillDownQuery q = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
q.add(new CategoryPath("Publish Date/2010", '/'));
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
searcher.search(q, fc); // Retrieve results
List<FacetResult> facetResults = fc.getFacetResults(); indexReader.close();
taxoReader.close(); return facetResults;
} /** Runs the search example. */
public List<FacetResult> runSearch() throws IOException {
index();
return search();
} /** Runs the drill-down example. */
public List<FacetResult> runDrillDown() throws IOException {
index();
return drillDown();
} /** 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("-----------------------");
List<FacetResult> results = new SimpleFacetsExample().runSearch();
for (FacetResult res : results) {
System.out.println(res);
} System.out.println("\n");
System.out.println("Facet drill-down example (Publish Date/2010):");
System.out.println("---------------------------------------------");
results = new SimpleFacetsExample().runDrillDown();
for (FacetResult res : results) {
System.out.println(res);
}
} }

Result:

Facet counting example:
-----------------------
Request: Publish Date nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 3
Publish Date (0.0)
Publish Date/2012 (2.0)
Publish Date/2010 (2.0)
Publish Date/1999 (1.0)
Request: Author nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 4
Author (0.0)
Author/Lisa (2.0)
Author/Frank (1.0)
Author/Susan (1.0)
Author/Bob (1.0) Facet drill-down example (Publish Date/2010):
---------------------------------------------
Request: Author nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 2
Author (0.0)
Author/Lisa (1.0)
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. linux 的IP配置和网络问题的排查

    1.6  IP的配制, 首先要会用: ifconfig  和加相关参数如: ifconfig -a, 来查看,自己的电脑网络配制. 再次就必需要知道,默认IP配制文件的地方: cd /etc/sysc ...

  2. 为何linux(包括mac系统)执行指令要加上 ./ ??

    比如,现在要在$HIVE_HOME/bin下执行hive指令来启动hive,则该指令的执行顺序如下所示: 1 先找PATH路径 1.1 如果PATH路径下配置了$HIVE_HOME/bin,无论PAT ...

  3. hdu6440 Dream(费马小定理)

    保证 当  n^p=n(mod p) 是成立 只要保证n*m=n*m(mod p); #include<bits/stdc++.h> using namespace std; int ma ...

  4. 20165313 《Java程序设计》第五周学习总结

    教材学习总结 下面是我认为的重点,不足之处还请谅解: 1内部类:在一个类中定义另一个类:外嵌类:包含内部类的类. 2内部类的类体中不能声明类变量和类方法:外嵌类的类体中可以用内部类声明对象. 3非内部 ...

  5. python基础-函数基本特性和用法

    函数: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值 ...

  6. linux系统lnmp环境包搬家教程

    打包搬家apt-get install zip unzip -yyum install zip unzip -y# debian ubuntu 用apt-get,centos用yumcd /home/ ...

  7. 【shell编程】之基础知识-函数

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...

  8. Mongo 3.6.1版本Sharding集群配置

    Mongo低版本和高版本的sharding集群配置,细节不太一样.目前网上的配置文档大都是针对低版本的.本人在配置3.6.1版本的mongosharding集群的过程中,碰到不少问题,官方文档没有直观 ...

  9. linux服务器的日志管理

    消息紧急程度排行 emerg:该系统不可用 alert:需要立即修改 crit:紧急情况 err:错误信息 warning:预警信息 notice:具有重要性的普通条件 info:提供信息的消息 de ...

  10. Compoxure example 应用说明

    Compoxure 官方提供了一个demo应用,包含了cache,error,layout 等功能 环境准备 demo 使用docker-compose 运行 clone 代码 git clone h ...