Achim
Achim

Reputation: 15712

Restrict multi field facet calculation to subset of possible values

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?

Upvotes: 0

Views: 1140

Answers (3)

user2790935
user2790935

Reputation: 11

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.

Upvotes: 1

Achim
Achim

Reputation: 15712

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

Upvotes: 0

rchukh
rchukh

Reputation: 2947

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.

Upvotes: 0

Related Questions