Filtering

Separate from the q parameter (the user query), you can specify additional so-called filter queries that will filter the search results. Arguably, the user query is also a filter, but you instead see the word "search" used for that. Filter queries don't affect scoring, unlike the user query. To add a filter, simply use the fq parameter. This parameter can be added multiple times for additional filters. A document must match all filter queries and the user query for it to be in the results.

As an example, let's say, we wanted to make a search form for MusicBrainz that lets the user search for bands, not individual artists, and those that released an album in the last 10 years. Let's also say that the user's query string is Green. In the index, a_type is either person for an individual or group for a band, or 0 if unknown. Therefore, a query that would find non-individuals would be this, combined with the user's query:

+Green -a_type:person +a_release_date_latest:[NOW/YEAR-10YEARS TO *]

However, you should not use this approach. Instead, use multiple fq query parameters:

q=Green&fq=-a_type:person&fq=a_release_date_latest:[NOW/YEAR-10YEARS+TO+*]

A query that an application submits should appear slightly different due to URL Encoding special characters, such as the colon.

Filter queries have some tangential benefits:

  • They improve performance, because each filter query is cached in Solr's filter cache and can be applied extremely quickly.
  • They clarify the logs, which show what the user searched for without it being confused with the filters.

In general, raw user input doesn't wind up being part of a filter query. Instead, the filters are either known by your application in advance or are generated based on your data, for example, in faceted navigation.

Tip

You can disable caching of a filter by setting a cache local-param to false. This is useful to avoid pollution of the filter cache when you know the query is not likely to be used again. And if the query is the frange query parser (discussed in Chapter 6, Search Relevancy) or the geofilt query parser referencing a LatLonType field (discussed later), there is a potential performance benefit. Non-cached filter queries can be ordered too. For further details on this advanced technique, see https://cwiki.apache.org/confluence/display/solr/Common+Query+Parameters, under the The cache=false Parameter section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.100.120