Take the 2-minute tour ×

Imagine an index like the following:

id   partno      name          description
1    1000.001    Apple iPod    iPod by Apple
2    1000.123    Apple iPhone  The iPhone

When the user searches for "Apple" both documents would be returned. Now I'd like to give the user the possibility to narrow down the results by limiting the search to one or more fields that have documents containing the term "Apple" within those fields.

So, ideally, the user would see something like this in the filter section of the ui after his first query:

Filter by field
name (2)
description (1)

When the user applies the filter for field "description", only documents which contain the term "Apple" within the field "description" would be returned. So the result set of that second request would be the iPod document only. For that I'd use a query like ?q=Apple&qf=description (I'm using the Extended DisMax Query Parser)

How can I accomplish that with Solr?

I already experimented with faceting, grouping and highlighting components, but did not really come to a decent solution to this.

Just to make that clear again: The main problem here is to get the information needed for displaying the "Filter by field" section. This includes the names of the fields and the hits per field. Sending a second request with one of those filters applied already works.

share|improve this question

5 Answers 5

up vote 1 down vote accepted

Solr just plain Doesn't Do This. If you absolutely need it, I'd try it the multiple requests solution and benchmark it -- solr tends to be a lot faster than what people put in front of it, so an couple few requests might not be that big of a deal.

share|improve this answer
Not quite the answer I'd like to hear ;-) You seem to be sure of this. Do you think this could easily be implemented as an extension to Solr? I'd assume those informations wouldn't be too hard to collect on the Solr side. –  Chris Dec 19 '12 at 12:54
if you're planning to write your own extension/plugin then yes, it's completely doable! not so easy as chaining queries, tho... –  Samuele Mattiuzzo Dec 19 '12 at 13:10
Solr does do this, through facet.query, but you need to repeat the query for each field. Thus it is very similar to the multiple requests solution, but is a single request. –  joeln Oct 10 '13 at 3:05

This can be achieved with Solr faceting, but it's not neat. For example, I can issue this query:


to find the number of documents containing donkey in the title and text fields. I may get this response:


Since you also want the documents back for the field-disjunctive query, something like the following works:

share|improve this answer

if you want the term to be searched over the same fields every time, you have 2 options not breaking the "single query" requirement:

1) copyField: you group at index time all the fields that should match togheter. With just one copyfield your problem doesn't exist, if you need more than one, you're at the same spot.

2) you could filter the query each time dynamically adding the "fq" parameter at the end

http://<your_url_and_stuff>/?q=Apple&fq=name:Apple ...

this works if you'll be searching always on the same two fields (or you can setup them before querying) otherwise you'll always need at least a second query

Since i said "you have 2 options" but you actually have 3 (and i rushed my answer), here's the third:

3) the dismax plugin described by them like this:

The DisMaxQParserPlugin is designed to process simple user entered phrases 
(without heavy syntax) and search for the individual words across several fields 
using different weighting (boosts) based on the significance of each field.

so, if you can use it, you may want to give it a look and start from the qf parameters (that is what the option number 2 wanted to be about, but i changed it in favor of fq... don't ask me why...)

share|improve this answer
Thank you for your answer. To my understanding, the copyfield approach would be useful if I'd like to know if the search term was in any of the fields it was composed of. I still would have to identify the actual field that contains the value and the total hits per field manually, no? Regarding your second option, do you mean &fq=name:Apple OR description:Apple? Again I would have to identify and count manually I guess. –  Chris Dec 19 '12 at 12:45
yes that's what i meant. but i actually merged that answer with something else i wasn't meant to say there. you have a third option that i forgot about, apparently. let me update the answer –  Samuele Mattiuzzo Dec 19 '12 at 12:53
I did forget to mention that I'm already using (E)DisMax. The qf parameter would be my choice for limiting the search to one field in the filtering request (second part of my question). Updated the question. –  Chris Dec 19 '12 at 13:17

you could achieve this with two different search requests/queries:

name:apple -> 2 hits

description:apple -> 1 hit


You also could implement your own SearchComponent that executes multiple queries in the background and put it in the SearchHandler processing chain so you only will need a single query in the frontend.

share|improve this answer
Sounds reasonable, but the disadvantage would be, that you would have to send two more requests to get the number of hits for each query. As I still need to have the results for the "standard query" (q=Apple), this would be three requests per search action. Please correct me, if I'm wrong. Can those three queries be combined into one with the desired results? –  Chris Dec 19 '12 at 11:14
I've added an alternative to my answer –  kromit Dec 19 '12 at 12:58

SolrFaceting should solve your problem. Have a look at the Examples.

share|improve this answer
Solr faceting does not apply to the fields being searched upon. It will return facets for the Search Results. –  Jayendra Dec 19 '12 at 10:17

Your Answer


