23

Someone please give me a decent explanation of the difference between q and fq in Solr query, covering some points such as -

  • Do they have the same syntax?
  • Do they return same results?
  • When to use which one and why?
  • Any other differences
Mawia
  • 4,220
  • 13
  • 40
  • 55

2 Answers2

22

Standard solr queries use the "q" parameter in a request. Filter queries use the "fq" parameter.

The primary difference is that filtered queries do not affect relevance scores; the query functions purely as a filter (docset intersection, essentially).

jro
  • 7,372
  • 2
  • 23
  • 36
  • 2
    This should be the top answer, as the most important difference is in the fact that "q" calculates the score, and "fq" doesn't. – Marko Jan 13 '20 at 10:43
10

The q parameter takes your query and execute against the index. Then you can use filter queries (can use multiple filter queries) to filter the results.

For example your query can look like this.

q=author:shakespeare

this will match the documents which has 'shakespeare' in the 'author' field. Then you can use filter queries like this.

fq=title:hamlet
fq=type:play

Those will filter the results based on the other fields. You can even filter on the same field.

The query syntax is similar for both q and fq parameters

  • 1
    why not use q=*:* and fq=author:shakespeare? won't it improve performance? – ravi Apr 02 '14 at 06:23
  • 2
    why do you need to use both q=*.* and fq=author:shakspeare. You can just use q=author:shakspeare. fq is useful whenever you filter using multiple fields. – Sapumal Jayaratne Apr 03 '14 at 07:06
  • 8
    having q=*:* will give you all the documents and it won't calculate the relevance score as all documents qualifies, then doing fq=author:shakespeare will run the filter operation which is fast, and in case of q=author:shakespeare it need to calculate score of selected documents also which will be not be that fast. – ravi Apr 04 '14 at 05:56
  • 8
    Why not just do `q=author:shakespeare AND title:hamlet AND type:play` instead? – John Strood Jan 11 '19 at 11:36
  • 1
    Ravi's point is, "Is there a cost to calculating score?" and "Is there a way to avoid that if score isn't needed?" – Basil Jun 24 '20 at 17:11