0
votes

When I enter this url

http://localhost:8983/solr/shop/select?facet.field=brand&facet=on&facet.mincount=1&fl=name,brand,cat&q=mobile

I got following

response {
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"mobile",
      "facet.field":"brand",
      "fl":"name,brand,cat",
      "facet.mincount":"1",
      "facet":"on"}},
  "response":{"numFound":7,"start":0,"docs":[
      {
        "name":"moto g",
        "brand":"motorola",
        "cat":["mobile"]},
      {
        "name":"moto x",
        "brand":"motorola",
        "cat":["mobile"]},
      {
        "name":"iphone 5",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5c",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4s",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5s",
        "brand":"apple",
        "cat":["mobile"]}]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "brand":[
        "apple",5,
        "motorola",2]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

Now I want to narrow down the search to mobile phones from brand apple. For that I use filter query like this:

http://localhost:8983/solr/shop/select?facet.field=brand&facet=on&facet.mincount=1&fl=name,brand,cat&q=mobile&fq=brand:apple

and I get this result:

{
  "responseHeader":{
    "status":0,
    "QTime":1,
    "params":{
      "q":"mobile",
      "facet.field":"brand",
      "fl":"name,brand,cat",
      "facet.mincount":"1",
      "fq":"brand:apple",
      "facet":"on"}},
  "response":{"numFound":5,"start":0,"docs":[
      {
        "name":"iphone 5",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5c",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4s",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5s",
        "brand":"apple",
        "cat":["mobile"]}]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "brand":[
        "apple",5]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

I'm happy with the result. But I also want facet_fields to contain both apple and motorola. In a nutshell I need following result:

{
  "responseHeader":{
    "status":0,
    "QTime":1,
    "params":{
      "q":"mobile",
      "facet.field":"brand",
      "fl":"name,brand,cat",
      "facet.mincount":"1",
      "fq":"brand:apple",
      "facet":"on"}},
  "response":{"numFound":5,"start":0,"docs":[
      {
        "name":"iphone 5",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5c",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 4s",
        "brand":"apple",
        "cat":["mobile"]},
      {
        "name":"iphone 5s",
        "brand":"apple",
        "cat":["mobile"]}]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "brand":[
        "apple",5,
        "motorola",2]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

Thanks for answer in advance.

1

1 Answers

1
votes

You can use filter tagging and filter exclusion to always get the complete list of facets regardless of specific filters.

q=mobile&fq={!tag=brand}brand:apple&facet=on&facet.field={!ex=brand}brand