Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr - {!ex} on a facet query

Tags:

solr

lucene

Background

I'm trying to combine 2 features that work great separately but having trouble making them work together.

*1) As documented on solr wiki I can tag a specific fq and then exclude it on my facet.field. That will make my facets' counts stay the same even when selecting a value, like this:

fq={!tag=pt}price:100&facet=true&facet.field={!ex=pt}price

*2) I want to use facet.query as follows:

facet=true&facet.query=price:[0 TO 100]&facet.query=price:[100 TO *]

So I want to combine *1 & *2, this is what I tried:

fq={!tag=pt}price:[0 to 100]&facet=true&facet.query={!ex=pt}price:[0 TO 100]&facet.query={!ex=pt}price:[100 TO *]

What actually happens is that I receive back from Solr:

<lst name="facet_queries">
    <int name="{!ex=pt}price:[0 TO 100]">8</int>
    <int name="{!ex=pt}price:[100 TO *]">19</int>
</lst>

My question is:

Why is the {!ex=pt} part of the name? It's messing up some of my logic. Maybe I misused it, if so then what's the right way?

More information

What I am expecting is this: (The same as I receive if running *2 without *1)

<lst name="facet_queries">
    <int name="price:[0 TO 100]">8</int>
    <int name="price:[100 TO *]">19</int>
</lst>

Which makes sense, since if I'm running *1 this is what I'm receiving in facet_fields:

<lst name="facet_fields">
    <lst name="price">
        <int name="80">8</int>
        <int name="150">19</int>
    </lst>
</lst>

It doesn't say name="{!ex=pt}price"

like image 637
Eran H. Avatar asked Jan 07 '14 23:01

Eran H.


3 Answers

I assume this is because:

  • *1 example uses facet.field which should be named the same way as the field it uses (without any excludes information).
  • *2 example uses facet.query which should represent the query (with all possible information used in the query... it would not make sense to display part of query, e.g. without excludes part)

Anyway, if there is a need to name the particular facet which uses exclude functionality, then it can be done in the following way(using key parameter):

facet.field={!ex=pt key=good_name_for_a_facet}price

The same workds for facet.query... E.g., if you do want to hide the ex part:

facet.query={!ex=pt key=$queryOne}price:[0 TO 100]

where queryOne is part of a raw parameter passed to solr as queryOne=price:[0 TO 100]

So the final query will look something like this:

fq={!tag=pt}price:[0 TO 100]&facet=true&facet.query={!ex=pt key=$queryOne}price:[0 TO 100]&facet.query={!ex=pt key=$queryTwo}price:[100 TO *]&queryOne=price:[0 TO 100]&queryTwo=price:[100 TO *]

P.s. I have used the external parameter for key, because in that way - there is not need to do manual escaping of the special characters.

like image 75
rchukh Avatar answered Nov 16 '22 03:11

rchukh


I ran into this issue, and I solved it by adding a local param key to the {!ex} param. So for your example, I would do:

fq={!tag=pt}price:[0 to 100]&facet=true&facet.query={!ex=pt key="0 TO 100"}price:[0 TO 100]&facet.query={!ex=pt key="100 TO *"}price:[100 TO *]

The reason for this is QueryFacet is treated differently from FieldFacet (facet.field vs facet.query). Solr only removes the local param, which is the {!ex …} in FieldFacet keys. I actually tracked it down to the code for this as you can see in line 680 in FacetComponent.java (v 4.6) from the link below:

http://svn.apache.org/viewvc/lucene/dev/tags/lucene_solr_4_6_0/solr/core/src/java/org/apache/solr/handler/component/FacetComponent.java?view=markup

I didn't further follow the issue because my use case requires a "pretty" key anyway :)

like image 44
tcao Avatar answered Nov 16 '22 04:11

tcao


Here's how I solved it:

for (int i = 0; i < facetQueries.size(); i++) {
    String value = facetQueries.get(i);
    query.addFacetQuery(String.format("{!ex=%s key=$fQValue_%s}%s", value, i, value));
    query.add(String.format("fQValue_%s", i), value);
}
like image 22
protonss Avatar answered Nov 16 '22 04:11

protonss