solr将以导航为目的的查询结果称为facet. 它并不会修改查询结果信息, 只是在查询结果上根据分类添加了count信息, 而后用户根据count信息作进一步的查询, 好比淘宝的查询列表中, 上面会表示不一样的类目相关查询结果的数量.
好比搜索数码相机, 在搜索结果栏会根据厂商, 分辨率等维度列出, 这里厂商, 分辨率就是一个个facet.
而后在厂商下面会有nikon, canon, sony等品牌, 这个叫约束(constraints)
接下来是根据选择, 列出当前的导航路径, 这个叫面包屑(breadcrumb).
solr有几种facet:
普通facet, 好比从厂商品牌的维度创建fact
查询facet, 好比根据价格查询时, 将根据价格, 设置多个区间, 好比0-10, 10-20, 20-30等
日期facet, 也是一种特殊的范围查询, 好比按照月份进行facet.
facet的主要好处就是能够任意对搜索条件进行组合, 避免无效搜索, 改善搜索体验.
facet都是在查询时经过参数指定. 好比
在http api中这样写:
html
引用java
"&facet=true&facet.field=manu"
java代码 solrj这样写: web
query.setFacet(true);//是否分组查询 query.setRows(0);//设置返回结果条数,若是你时分组查询,你就设置为0 query.addFacetField("region");//增长分组字段 q query.addFacetField("theme");//增长分组字段 q QueryResponse rsp = server.query(query); //取出结果 FacetField facetField = rsp.getFacetField("region"); List<Count> counts = null; if (facetField != null) { counts = facetField.getValues(); if (counts != null) { for (Count count : counts) { if (count.getCount() != 0) { listRegion.add(count.getName()+"("+count.getCount()+")"); } } map.put("region", listRegion); } } FacetField facetFieldTheme = rsp.getFacetField("theme"); List<Count> countsTheme = null; if (facetFieldTheme != null) { countsTheme = facetFieldTheme.getValues(); if (countsTheme != null) { for (Count count : countsTheme) { if (count.getCount() != 0) { listTheme.add(count.getName()+"("+count.getCount()+")"); } } map.put("theme", listTheme); } }
而xml返回的结果为这样: api
<lst name="facet_fields"> <lst name="manu"> <int name="Canon USA">17</int> <int name="Olympus">12</int> <int name="Sony">12</int> <int name="Panasonic">9</int> <int name="Nikon">4</int> </lst> </lst>
经过java代码能够这样获取facet结果:spa
List<FacetField> facetFields = queryResponse.getFacetFields();
在已有的查询基础上增长facet query, 能够这样写: code
solrQuery.addFacetQuery("quality:[* TO 10]")
好比对价格按照指定的区间进行facet, 能够这样加上facet后缀:
orm
引用server
&facet=true&facet.query=price:[* TO 100]
&facet.query=price:[100 TO 200];&facet.query=[price:200 TO 300]
&facet.query=price:[300 TO 400];&facet.query=[price:400 TO 500]
&facet.query=price:[500 TO *]xml
若是要对价格在400到500期间的产品作进一步的搜索, 那么能够这样写(使用了solr的过滤查询):
htm
引用
http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu&facet.field=camera_type &fq=price:[400 to 500]
注意这里的facet field再也不包含price了
若是这里对类型作进一步的查询, 那么query语句能够这样写:
引用
http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu &fq=price:[400 to 500] &fq=camera_type:SLR
facet的使用场景:
1.类目导航
2.自动提示, 须要借助一个支持多值的tag field.
3.热门关键词排行, 也须要借助一个tag field