package com.fox.facet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; 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.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.facet.index.CategoryDocumentBuilder;
import org.apache.lucene.facet.search.FacetsCollector;
import org.apache.lucene.facet.search.params.CountFacetRequest;
import org.apache.lucene.facet.search.params.FacetSearchParams;
import org.apache.lucene.facet.search.results.FacetResult;
import org.apache.lucene.facet.search.results.FacetResultNode;
import org.apache.lucene.facet.taxonomy.CategoryPath;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class FacetingExample {
private static final String INDEX = "d:/facet/index";
private static final String INDEX_TAXO = "d:/facet/taxo"; public static void main(final String[] args) throws IOException {
Directory dir = FSDirectory.open(new File(INDEX));
Directory taxoDir = FSDirectory.open(new File(INDEX_TAXO));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc);
TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE_OR_APPEND); List<Book> books = Arrays.asList(new Book("Tom Sawyer", "Mark Twain", "1840", "Novel"), new Book("Collected Tales",
"Mark Twain", "1850", "Novel"), new Book("The Trial", "Franz Kafka", "1901", "Novel"), new Book("Some book",
"Some author", "1901", "Novel")); createDocuments(writer, taxoWriter, books);
taxoWriter.commit();
writer.commit();
writer.close();
taxoWriter.close(); IndexReader indexReader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
Query q = new TermQuery(new Term("category", "Novel"));
TopScoreDocCollector tdc = TopScoreDocCollector.create(10, true);
FacetSearchParams facetSearchParams = new FacetSearchParams();
facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("author"), 10));
facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("category"), 10));
facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("published"), 10));
FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);
searcher.search(q, MultiCollector.wrap(tdc, facetsCollector));
List<FacetResult> res = facetsCollector.getFacetResults();
System.out.println("Search for books with the category:Novel returned : " + res.size()
+ " results\n---------------------------------");
for (final FacetResult r : res) {
System.out.println("\nMatching " + r.getFacetResultNode().getLabel() + ":\n------------------------------------");
for (FacetResultNode n : r.getFacetResultNode().getSubResults()) {
System.out.println(String.format("\t%s: %.0f", n.getLabel().lastComponent(), n.getValue()));
}
}
} private static void createDocuments(final IndexWriter writer, final TaxonomyWriter taxoWriter, final List<Book> books)
throws IOException {
for (final Book b : books) {
Document doc = new Document();
doc.add(new StringField("title", b.getTitle(), Store.YES));
doc.add(new StringField("category", b.getCategory(), Store.YES));
List<CategoryPath> categories = new ArrayList<CategoryPath>();
categories.add(new CategoryPath("author", b.getAuthor()));
categories.add(new CategoryPath("category", b.getCategory()));
categories.add(new CategoryPath("published", b.getPublished()));
CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxoWriter);
categoryDocBuilder.setCategoryPaths(categories);
categoryDocBuilder.build(doc);
writer.addDocument(doc);
}
}
} class Book {
private final String title;
private final String author;
private final String published;
private final String category; public Book(final String title, final String author, final String published, final String category) {
this.title = title;
this.author = author;
this.published = published;
this.category = category;
} public String getTitle() {
return title;
} public String getAuthor() {
return author;
} public String getPublished() {
return published;
} public String getCategory() {
return category;
}
}

Result

Search for books with the category:Novel returned : 3 results
--------------------------------- Matching author:
------------------------------------
Mark Twain: 2
Some author: 1
Franz Kafka: 1 Matching category:
------------------------------------
Novel: 4 Matching published:
------------------------------------
1901: 2
1850: 1
1840: 1

lucene 4.0 - Facet demo的更多相关文章

  1. Lucene 4.3 - Facet demo

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

  2. Lucene 4.8 - Facet Demo

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

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

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

  4. 关于Lucene 3.0升级到Lucene 4.x 备忘

    最近,需要对项目进行lucene版本升级.而原来项目时基于lucene 3.0的,很古老的一个版本的了.在老版本中中,我们主要用了几个lucene的东西: 1.查询lucene多目录索引. 2.构建R ...

  5. Lucene 6.0下使用IK分词器

    Lucene 6.0使用IK分词器需要修改修改IKAnalyzer和IKTokenizer. 使用时先新建一个MyIKTokenizer类,一个MyIkAnalyzer类: MyIKTokenizer ...

  6. Lucene 4.0 正式版发布,亮点特性中文解读[转]

    http://blog.csdn.net/accesine960/article/details/8066877 2012年10月12日,Lucene 4.0正式发布了(点击这里下载最新版),这个版本 ...

  7. Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  8. vue入门 0 小demo (挂载点、模板、实例)

    vue入门 0 小demo  (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...

  9. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...

随机推荐

  1. [LeetCode&Python] Problem 733. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  2. epoll c++封装

    #ifndef _BFC_EPOLL_FLOW_HPP_ #define _BFC_EPOLL_FLOW_HPP_ #include <string.h> #include <err ...

  3. lecture2-word2vec-七月在线nlp

    离散表示: one-hot bag of words -- 词权重 ~不能表示顺序关系   TF-IDF (Term Frequency - Inverse Document Frequency) [ ...

  4. 开发工具-Eclipse

    1.Eclipse的视窗和视图概述 - A:视窗 每一个基本的窗体被称为视窗  * PackageExplorer 显示项目结构,包,类,及资源   * Outline 显示类的结构,方便查找,识别, ...

  5. (18)jq事件操作

    jq的私人网站:http://jquery.cuishifeng.cn/ 具体的查看上面的网站 <!DOCTYPE html><html><head> <me ...

  6. 《DSP using MATLAB》Problem 5.1

    恰逢清明,这几天是清明时节雪纷纷,断崖式降温:又回到了老家,儿时上蹿下跳的核桃树,远去的故乡,远去的时代…… 用到的公式: 这里只放前两个小题的计算过程,都比较简单,细心就行.代码如下: %% --- ...

  7. python time模块使用笔记(更新)

    import time 添加time模块 关于时间和时间戳: 时间是指日常生活中用的,如某年某月某日 时间戳是一个时间长度,是时间关于一个初始时间(好像是1970.1.1)的秒数 localtime方 ...

  8. day07 hadoop里面的RPC框架使用

    PS: RPC远程调用 Webservice啥的都是远程调用.下面简单介绍其使用过程 Hadoop已经实现了RPC框架,不用我们自己写,不过需要我们注意几点:1.发布服务端和客户端必须包名相同 1.服 ...

  9. 使用nvm-windows安装nodejs遇到的问题(转载)

    ##使用nvm-windows安装nodejs遇到的问题 ####问题概述 由于国内网络限制导致使用nvm(nvm-windows,以下都使用nvm简称)安装nodejs超时或出现远程连接被关闭的问题 ...

  10. nginx+php windows安装配置

    https://blog.csdn.net/zjiang1994/article/details/72876193 https://blog.csdn.net/bruce_wang_janet/art ...