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. TJU Problem 2101 Bullseye

    注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<& ...

  2. mac上不了网

    不知怎么回事勾选了socks代理导致ping的通但打不开网页软件登不了

  3. webservice------UDDI SOAP WSDL 之间的关系

    [  真的是服了一些博客.....啰里啰唆的将一堆==   根本不知道讲的是什么 ... 在描述一个定义之前  (不如先通俗的讲它是干什么的)] SOAP(Simple Object Access P ...

  4. import sys

    目录 sys模块的常见函数列表 1.sys.argv 2.sys.platform 3.sys.path 4.sys.exit(n) sys模块提供了一系列有关Python运行环境的变量和函数. 回到 ...

  5. Linux系统安全笔记

    Linux系统安全笔记 https://insecure.org/https://sectools.org/SecTools.Org:排名前125的网络安全工具 http://www.ibm.com/ ...

  6. HTML表格元素

      学习要点:     1.表格元素总汇     2.构建表格解析 一.表格元素总汇     表格的基本构成最少需要三个元素:<table>.<tr>.<td>,其 ...

  7. mongodb千万级写入怎么优化

    从mysql数据库通过java程序导入单表1300w到mongodb,花了大概50分钟,前1000w条数据中每100w条大概要3分钟,之后的300多w条就差不多每100w条要5到6分钟,之后再从其他的 ...

  8. imrersize函数

    imrersize函数: 用法:imresize(图像I,method,倍数) 'nearest'(默认值)最近邻插值'bilinear'双线性插值'bicubic'双三次插值 使用方法: clear ...

  9. GoLand tool tips

    goland format code (on save ) preference -> tool -> file watcher  then ,select go fmt 2, highl ...

  10. mysql之mysqldump——备份与还原

    导出数据库里的某一张表 [root@localhost ~]# mysqldump -uroot -p test bptest>fi.mysql #导出test数据库中的bptest表 Ente ...