dotancohen
dotancohen

Reputation: 31481

Treat two facets as the same value

Assume a list of books with an Author field. How might one facet on the Author field, but treat the values "Stephen King" and "Richard Bachman" as the same? So that these results:

Would be displayed as:

Note that it is unimportant if the facet title is "Stephen King", "Richard Bachman", or something else. It is only important that they are faceted together.

Note that a query-time solution is needed. Unfortunately the schema cannot be changed for this index, it is a general-purpose index and if every user could make his own schema 'tweak' it would get out of hand.

Upvotes: 2

Views: 521

Answers (3)

Ion Cojocaru
Ion Cojocaru

Reputation: 2583

I assume you do not need the whole list of facets, just top n authors. If this is the case you can do it in a post processing step.

You know your synonyms and if you put a slightly higher facet.limit(let's say 2*n) then you just have to filter out the synonyms from the result set. If you end up with < n results then just repeat the previous step(worse case you have to do one more request(s) depending on the number of synonyms).

in ex ...&facet=true&facet.field=author&facet.limit=100&facet.mincount=1

This one has nothing to do with Solr, but considering all the restrictions it might just cut it.

Best regards,

Upvotes: 1

mjalajel
mjalajel

Reputation: 2201

You can achieve that by combining facet fields with facet queries.

Add these to your query:

&facet=true
&facet.field=author
&facet.query=author:("Hemmingway" OR "Stephen King")

Facets returned will look like this:

facet_counts: {
    facet_queries: {
       "author:("Hemmingway" OR "Stephen King")" : 18
    }
    facet_fields: {
         author: {
            "Hemmingway"      : 8,
            "Stephen King"    : 10,
            "Edgar Allan Poe" : 20,
            "Richard Bachman" : 5
        }
    }
}

You can also add an 'alias' to the facet query. Change this

&facet.query=author:("Hemmingway" OR "Stephen King")

To

&facet.query={!ex=dt key="Hemmingway"}author:("Hemmingway" OR "Stephen King")

And the facet query output will be:

    facet_queries: {
       "Hemmingway" : 18
    }

I'm not sure if you can merge both output fields (facet_queries and facet_fields) from Solr, but doing that from any client should be straight-forward.

Upvotes: 3

Mike Sokolov
Mike Sokolov

Reputation: 7044

You need an analysis chain that converts the strings. I think SynonymFilter will do this for you if you apply it at index time and at query time. You would need to make sure the sysnonym mapping goes one way only.

Upvotes: 1

Related Questions