先看示例,再看概念
有这么一个字段:
<field name="csFacetTwo" type="string" indexed="true" stored="false"/>
插入三条记录:
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("rowkey","a1");
doc1.addField("csFacetTwo","电脑品牌1");
doc1.addField("title","笔记本电脑");
doc1.addField("size","14寸");
doc1.addField("count","双核");
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("rowkey","a2");
doc2.addField("csFacetTwo","电脑品牌2");
doc2.addField("title","笔记本电脑");
doc2.addField("size","14寸");
doc2.addField("count","双核");
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("rowkey","a3");
doc3.addField("csFacetTwo","电脑品牌3");
doc3.addField("title","笔记本电脑");
doc3.addField("size","14寸");
doc3.addField("count","双核");
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc1);
docs.add(doc2);
docs.add(doc3); try {
server.add(docs);
server.commit();
System.out.println("保存成功!");
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
查询:doc1.addField("rowkey","a1");
doc1.addField("csFacetTwo","电脑品牌1");
doc1.addField("title","笔记本电脑");
doc1.addField("size","14寸");
doc1.addField("count","双核");
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("rowkey","a2");
doc2.addField("csFacetTwo","电脑品牌2");
doc2.addField("title","笔记本电脑");
doc2.addField("size","14寸");
doc2.addField("count","双核");
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("rowkey","a3");
doc3.addField("csFacetTwo","电脑品牌3");
doc3.addField("title","笔记本电脑");
doc3.addField("size","14寸");
doc3.addField("count","双核");
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc1);
docs.add(doc2);
docs.add(doc3); try {
server.add(docs);
server.commit();
System.out.println("保存成功!");
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SolrQuery query = new SolrQuery();
query.setQuery("title:笔记本电脑");
query.setFacet(true);
query.addFacetField("csFacetTwo");
try {
QueryResponse response = server.query(query);
SolrDocumentList docs = response.getResults();
List<FacetField> facetFields = response.getFacetFields(); //获取到Facet集合
for (SolrDocument doc : docs) { //遍历主查询
System.out.println("rowkey: " + doc.getFieldValue("rowkey"));
}
System.out.println();
for (FacetField facet:facetFields) { //遍历集合
System.out.println(facet.getName());
System.out.println("----------------");
List<Count> counts = facet.getValues();
for(Count count : counts){
System.out.println(count.getName()+":"+count.getCount());
}
System.out.println();
}
}catch (Exception e) {
e.printStackTrace();
}
打印结果:query.setQuery("title:笔记本电脑");
query.setFacet(true);
query.addFacetField("csFacetTwo");
try {
QueryResponse response = server.query(query);
SolrDocumentList docs = response.getResults();
System.out.println("rowkey: " + doc.getFieldValue("rowkey"));
}
System.out.println();
for (FacetField facet:facetFields) { //遍历集合
System.out.println(facet.getName());
System.out.println("----------------");
List<Count> counts = facet.getValues();
for(Count count : counts){
System.out.println(count.getName()+":"+count.getCount());
}
System.out.println();
}
}catch (Exception e) {
e.printStackTrace();
}
rowkey: a1
rowkey: a2
rowkey: a3
csFaceTwo
----------------
电脑品牌1:1
电脑品牌2:1
电脑品牌3:1
概念:
solr将以导航为目的的查询结果称为facet. 它并不会修改查询结果信息, 只是在查询结果上根据分类添加了count信息
然后用户根据count信息做进一步的查询, 比如淘宝的查询列表中, 上面会表示不同的类目相关查询结果的数量.
Facet 的字段必须被索引 . 一般来说该字段无需分词 , 无需存储 .
无需分词是因为该字段的值代表了一个整体概念 , 如电脑的品牌 ” 某想 ” 代表了一个整体概念 , 如果拆成 ” 某 ”,” 想 ” 两个字都不具有实际意义 . 另外该字段的值无需进行大小写转换等处理 , 保持其原貌即可 .
无需存储是因为一般而言用户所关心的并不是该字段的具体值 , 而是作为对查询结果进行分组的一种手段 , 用户一般会沿着这个分组进一步深入搜索 .
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29754888/viewspace-1724182/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29754888/viewspace-1724182/