惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
V
V2EX
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
小众软件
小众软件
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
罗磊的独立博客
T
Tor Project blog
C
Cisco Blogs
美团技术团队
博客园 - Franky
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Latest
Security Latest
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
Spread Privacy
Spread Privacy
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Project Zero
Project Zero

博客园 - searchDM

Elasticsearch 集成 Elasticsearch 环境 Elasticsearch 优化 Elasticsearch入门 Elasticsearch概述 吴恩达自然语言处理第一课:分类与向量空间 Linux下C语言字符串操作之字符串转数值型 在ubuntu上安装全文搜索中文分词Coreseek/sphinx及和Rails集成 solr3.4 高亮(highlight),拼写检查(spellCheck),匹配相似(moreLikeThis) 应用实践 堆与堆排序 Trie树|字典树的简介及实现 快速排序 归并排序的实现 Lucene.net搜索结果排序(单条件和多条件) 直接选择排序及交换二个数据的实现 冒泡排序 直接插入排序的三种实现 希尔排序的实现 几乎所有食物的英文翻译
lucene 3.4 contrib/facet 切面搜索
searchDM · 2013-03-28 · via 博客园 - searchDM

solr  有facet  search  ,BOBO也有;现在lucene3.4之后也有了,这个是贡献版本,在apache 官方的包里面有提供,这种功能对于分组统计和类别统计是一个很好的帮手;

有了这个就不用羡慕solr了,不是我抗拒solr,只是像我们公司有时间让我们开发的情况下,我更偏向于底层点的api开发,lucene更得心应手。

再说现在的solr没有近实时搜索,听说要4.0后有。

废话不说,直接上代码

public class Indexer {

//需要索引的信息
public static String[] docTitles = {
"white car",
"white dog",
};
public static String[] docTexts = {
"the white car is the one I want.",
"the white dog does not belong to anyone.",
};

//分的类别
public static CategoryPath[] categories = {
new CategoryPath("root","a","f1"), new CategoryPath("root","a","f2")
};

public static void index (Directory indexDir, Directory taxoDir) throws Exception {
// 创建一个普通的indexwriter
IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
//创建一个 taxonomy writer
TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);


int nDocsAdded = 0;
int nFacetsAdded = 0;
for (int docNum=0; docNum<docTexts.length; docNum++)
{

// 准备当前的切面
List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(categories[0]);

//
CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo).setCategoryPaths(facetList);

// 创建document
Document doc = new Document();
doc.add(new Field(SimpleUtils.TITLE, docTitles[docNum], Store.YES, Index.ANALYZED));
doc.add(new Field(SimpleUtils.TEXT, docTexts[docNum], Store.NO, Index.ANALYZED));

// 把切面的索引信息添加到document
categoryDocBuilder.build(doc);

// 最终写入索引
iw.addDocument(doc);

nDocsAdded ++;
nFacetsAdded += facetList.size();
}

// commit changes.
// we commit changes to the taxonomy index prior to committing them to the search index.
// this is important, so that all facets referred to by documents in the search index
// will indeed exist in the taxonomy index.
taxo.commit();
iw.commit();

// close the taxonomy index and the index - all modifications are
// now safely in the provided directories: indexDir and taxoDir.
taxo.close();
iw.close();
System.out.println("Indexed "+nDocsAdded+" documents with overall "+nFacetsAdded+" facets.");
}

public static void main(String[] args){
String indexp = "D:/work/data/index/facet_index/n_index";
String indexp_t = "D:/work/data/index/facet_index/t_index";
try {
Directory directory = FSDirectory.open(new File(indexp));
Directory directory_t = FSDirectory.open(new File(indexp_t));

new Indexer().index(directory, directory_t);
} catch (Exception e) {
e.printStackTrace();
}
}

}

public class Searcher {

public void query(Directory indexDir, Directory taxoDir) throws CorruptIndexException, IOException
{
Query q = new TermQuery(new Term(SimpleUtils.TEXT, "white"));

IndexReader indexReader = IndexReader.open(indexDir, true);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true);

FacetIndexingParams indexingParams = new DefaultFacetIndexingParams();
FacetSearchParams facetSearchParams = new FacetSearchParams(indexingParams);
facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));

FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);

// perform documents search and facets accumulation
searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));

// Obtain facets results and print them
List<FacetResult> res = facetsCollector.getFacetResults();
System.out.println(res.size());
int i = 0;
for (FacetResult facetResult : res) {
System.out.println("Res " + (i++) + ": " + facetResult);
System.out.println("-------------------------------------");
System.out.println( facetResult.getFacetResultNode().getNumSubResults());
System.out.println( facetResult.getFacetResultNode().getOrdinal());
System.out.println( facetResult.getFacetResultNode().getValue());
System.out.println( facetResult.getFacetResultNode().getResidue());
}
}

public static void main(String[] args) {
String indexp = "D:/work/data/index/facet_index/n_index";
String indexp_t = "D:/work/data/index/facet_index/t_index";
try {
Directory directory = FSDirectory.open(new File(indexp));
Directory directory_t = FSDirectory.open(new File(indexp_t));

new Searcher().query(directory, directory_t);
} catch (Exception e) {
e.printStackTrace();
}
}
}

在最新的lucene.net 3.0.3中也有facet  search