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. 小白入门photoscan

    1.安装 我装的是photoscanPro 1.4.5版本.[注]:刚开始是在官网上下载的,要收费就点了试用,结果当我等了一天把将近200张图片处理完后,告诉我试用版不能保存文件...(绝望-_- - ...

  2. next_permutation(start,end)

    一道水题,简单的next_permutation用法,相同的还有prev_permutation 包含在头文件<algorithm>中 字符串 acab 含有两个a ,一个b ,一个c , ...

  3. 压缩文件破解rarcrack-支持格式zip,rar和7z

    Kali上没有,需要自己安装 apt-get install rarcrack 安装成功后, 新建一个文本文档,元素: <?xml version="1.0" encodin ...

  4. SQL经常使用的一些词

    sp_helptext: 例:exec sp_helptext proc_name(查看存储过程的定义) sp_rename: 例:exec sp_rename 'proc_name1','proc_ ...

  5. [Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition]

    https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n, ...

  6. 《DSP using MATLAB》Problem 5.24-5.25-5.26

    代码: function y = circonvt(x1,x2,N) %% N-point Circular convolution between x1 and x2: (time domain) ...

  7. 转:《Javascript模块化编程》

    (一):模块的写法 转载至:http://www.ruanyifeng.com/blog/2012/10/javascript_module.html (二):AMD规范 转载至:http://www ...

  8. 【liunx】sftp常用命令

    sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分, ...

  9. eclipse操作(备忘)

    myecplise破解   https://blog.csdn.net/by_xiaobai007/article/details/81177367 1.查看类路径 2.建立模板 window--pr ...

  10. vorpal 又一个方便的cli 开发包

    vorpal 是一个npm 包,我们可以用来开发专业的cli 程序 简单使用 初始化项目 yarn init -y 添加依赖 yarn add vorpal 简单demo app.js // cons ...