solr特点二:Facet
返回查询集合中指定field的统计情况,例如找到city一样的文档数目:
加入文档
- <add>
- <doc>
- <field name="id">1</field>
- <field name="name">Company 1</field>
- <field name="city">New York</field>
- </doc>
- <doc>
- <field name="id">2</field>
- <field name="name">Company 2</field>
- <field name="city">New Orleans</field>
- </doc>
- <doc>
- <field name="id">3</field>
- <field name="name">Company 3</field>
- <field name="city">New York</field>
- </doc>
- </add>
查询:http://localhost:8983/solr/select?q=name:company&facet=true&facet.field=city&facet.mincount=1
结果:
- <lst name="facet_fields">
- <lst name="city">
- <int name="New York">2</int>
- <int name="New Orleans">1</int>
- </lst>
- </lst>
获得指定日期范围内的文档:添加的文档
- <add>
- <doc>
- <field name="id">1</field>
- <field name="title">Lucene or Solr ?</field>
- <field name="added">2010-12-06T12:12:12Z</field>
- </doc>
- <doc>
- <field name="id">2</field>
- <field name="title">My Solr and the rest of the world</field>
- <field name="added">2010-12-07T11:11:11Z</field>
- </doc>
- <doc>
- <field name="id">3</field>
- <field name="title">Solr recipes</field>
- <field name="added">2010-11-30T12:12:12Z</field>
- </doc>
- <doc>
- <field name="id">4</field>
- <field name="title">Solr cookbook</field>
- <field name="added">2010-11-29T12:12:12Z</field>
- </doc>
- </add>
查询语句:分别指定时间的field,开始时间和结束时间,gap指定时间的划分,结果显示时间区间的数目。
http://localhost:8983/solr/select?q=*:*&rows=0&facet=true&facet.date=added&facet.date.start=NOW/DAY-30DAYS&facet.date.end=NOW/DAY&facet.date.gap=+7DAY
结果:
- <int name="2010-11-08T00:00:00Z">0</int>
- <int name="2010-11-15T00:00:00Z">0</int>
- <int name="2010-11-22T00:00:00Z">0</int>
- <int name="2010-11-29T00:00:00Z">2</int>
- <int name="2010-12-06T00:00:00Z">2</int>
得到数值范围的数目,和时间范围一样:
http://localhost:8983/solr/select?q=*:*&rows=0&facet=true&facet.range=price&facet.range.start=0&facet.range.end=400&facet.range.gap=100
自定义区间,而不是连续区间的划分:
http://localhost:8983/solr/select?q=name:car&facet=true&facet.query=price:[10 TO 80]&facet.query=price:[90 TO 300]
移除过滤:
http://localhost:8983/solr/select?q=name:company&facet=true&fq={!tag=stateTag}state:"New York"&facet.field={!ex=stateTag}city&facet.field={!ex=stateTag}state
fq={!tag=stateTag}state:"New York":只显示state为"New York"的结果。
facet.field={!ex=stateTag}city:移除stateTag的过滤后,在结果集中,对city域进行统计。
命名facet结果集:
http://localhost:8983/solr/select?q=name:company&facet=true&fq={!tag=stateTag}state:Luiziana&facet.field={!key=stateFiltered}city&facet.field={!ex=stateTag key=stateUnfiltered}state
acet.field={!key=stateFiltered}city:命名city为stateFiltered,并按照前面的过滤
对facet结果集进行排序按照字典序排序,默认是按数量排序:
http://localhost:8983/solr/select?q=name:house&facet=true&facet.field=city&facet.sort=index
实现自动提示:前缀为so的都会显示,一般不需要分词
http://localhost:8983/solr/select?q=*:*&rows=0&facet=true&facet.field=title_autocomplete&facet.prefix=so
得到某一个域中不含某词的facet,也可以是不含某个域:
http://localhost:8983/solr/select?q=title:solr&facet=true&facet.query=!price:[* TO *]
指定结果集数目的统计facet:-1表示所有
http://localhost:8983/solr/select?q=title:solr&facet=true&facet.field=category&facet.limit=-1
指定不同域的facet的限制数目:一个没限制,一个限制10
http://localhost:8983/solr/select?q=name:car&facet=true&facet.field=category&facet.field=manufacturer&f.category.facet.limit=-1&f.manufacturer.fac