lucene 4.0 - Facet demo
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的更多相关文章
- Lucene 4.3 - Facet demo
package com.fox.facet; import java.io.IOException; import java.util.ArrayList; import java.util.List ...
- Lucene 4.8 - Facet Demo
package com.fox.facet; /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- lucene搜索之facet查询原理和facet查询实例——TODO
转自:http://www.lai18.com/content/7084969.html Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为 ...
- 关于Lucene 3.0升级到Lucene 4.x 备忘
最近,需要对项目进行lucene版本升级.而原来项目时基于lucene 3.0的,很古老的一个版本的了.在老版本中中,我们主要用了几个lucene的东西: 1.查询lucene多目录索引. 2.构建R ...
- Lucene 6.0下使用IK分词器
Lucene 6.0使用IK分词器需要修改修改IKAnalyzer和IKTokenizer. 使用时先新建一个MyIKTokenizer类,一个MyIkAnalyzer类: MyIKTokenizer ...
- Lucene 4.0 正式版发布,亮点特性中文解读[转]
http://blog.csdn.net/accesine960/article/details/8066877 2012年10月12日,Lucene 4.0正式发布了(点击这里下载最新版),这个版本 ...
- Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)
作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...
- vue入门 0 小demo (挂载点、模板、实例)
vue入门 0 小demo (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...
- Lucene 6.5.0 入门Demo
Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...
随机推荐
- Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...
- AE1
i2.1 导入四种方式: 1.文件->导入 2.项目窗口空白区域双击 3.空白区域右键 4 ctrl+i 常用键: ctri+i ctrl shift alt delete 2.2移动属 ...
- CUDA学习
CUDA(Compute Unified Device Architecture,统一计算架构)是由NVIDIA所推出的一种集成技术,是该公司对于GPGPU的正式名称.通过这个技术,用户可利用NVID ...
- test-ipv6
http://test-ipv6.com/ ! 你的公网 IPv4 地址是 89.42.31.211! 你的公网 IPv6 地址是 2001:ac8:21:8::376e:989b! 你已接入 IPv ...
- Redis 开发与运维
Getting Start 高性能 性能优势的体现 C语言实现的内存管理 epoll的I/O多路复用技术+IO连接/关闭/读写通过事件实现异步的非阻塞IO TCP协议 单线程架构,不会因为高并发对服务 ...
- 树莓派ssh服务
从官网下载的镜像更新raspberry pi 3 B,但默认是不支持SSH的,即不可外部通过SSH登陆到板子里. 解决办法很简单,在SD卡的根目录下创建一个"ssh"的文件夹即可.
- linux运维发展路线
- 网易的Airtest
使用心得:https://testerhome.com/topics/12391 下载路径:https://airtest.netease.com/
- python之路---09 初始函数 参数
二十七.初始函数 1.函数: 对代码块和功能的封装和定义 2.格式及语法 def 函数名() # 定义 函数体 函数名() # 调用 3. r ...
- 我发起并创立了一个 VMBC 的 子项目 D#
大家好, 我发起并创立了一个 VMBC 的 子项目 D# . 有关 VMBC , 请参考 <我发起了一个 用 C 语言 作为 中间语言 的 编译器 项目 VMBC> https ...