0
votes

I have a non trivial SOLR query, which already involves a filter query and facet calculations over multiple fields. One of the facet fields is a a multi value integer field, that is used to store categories. There are many possible categories and new ones are created dynamically, so using multiple fields is not an option.

What I want to do, is to restrict facet calculation over this field to a certain set of integers (= categories). So for example I want to calculate facets of this field, but only taking categories 3,7,9 and 15 into account. All other values in that field should be ignored.

How do I do that? Is there some build in functionality which can be used to solve this? Or do I have to write a custom search component?

3
So.. the results should contain documents with any category(1,2,3...), but facets should only have the information about categories 3,7,9,15 ? In other words you want to apply specific filter query only to the facet part of the results, but not to the documents part, correct?rchukh
Yes, that's exactly what I want to have. Looks like I could solve it via facet prefixes, but I still have to evaluate it.Achim
Hmm... There is a way to do it vice versa(that is - exluding fq from the facet results) if that would be of any help to someone answering this question. For example, like this.rchukh
And it would not be an option to execute the same query with an altered filter query? Just to populate that facet. As Solr does a good job of caching.cheffe

3 Answers

1
votes

The parameter can be defined for each field specified by the facet.field parameter – you can do it, by adding a parameter like this: facet.field_name.prefix.

0
votes

I don't know about any way to define the facet base that should be different from the result, but one can use the facet.query to explicitly define each facet filter, e.g.:

facet.query={!key=3}category:3&facet.query={!key=7}category:7&facet.query={!key=9}category:9&facet.query={!key=15}category:15

Given the solr schema/data from this gist, the results will have something like this:

"facet_counts": {
    "facet_queries": {
      "3": 1,
      "7": 1,
      "9": 0,
      "15": 0
    },
    "facet_fields": {
      "category": [
        "2",
        2,
        "1",
        1,
        "3",
        1,
        "7",
        1,
        "8",
        1
      ]
    },
    "facet_dates": {},
    "facet_ranges": {}
}

Thus giving the needed facet result.

I have some doubts about performance here(especially when there will be more than 4 categories and if the initial query is returning a lot of results), so it is better to do some benchmarking, before using this in production.

0
votes

Not exactly the answer to my own question, but the solution we are using now: The numbers I want to filter on, build distinct groups. So we can prefix the id with a group id like this:

1.3
1.8
1.9
2.4
2.5
2.11
...

Having the data like this in SOLR, we can use facted prefixes to facet only over a single group: http://wiki.apache.org/solr/SimpleFacetParameters#facet.prefix