Queries can be used in query context, and filters can be used in filter context.

When used in filtering context, the query is said to be a “non-scoring” or “filtering” query. That is, the query simply asks the question: “Does this document match?”. The answer is always a simple, binary yes|no.

  • Is the lastModified date in the range 2016 – 2017?
  • is document ids in the range 1 -100 ?
  • Does the subject field contain the term solr?
  • Is the lat_lon field within 20km of a specified point?

Solr provide facility to execute multiple filter query in a single query request by specifying multiple fq params.

Multiple filter queries example:

http://localhost:8983/solr/multiplefilterqueryexample/query?q=*:*&fq=popularity:[6 TO 10]&fq=DOC_ID:[1 TO 100]&fq=FROM:"[email protected]"&rows=10&indent=true

In above example we have added three filter queries by adding fq params three times.

multiple-filter-query-solr-dashboard

Solrj Example:

HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr/multiplefilterqueryexample");
  SolrQuery query = new SolrQuery();
  query.setQuery("*:*"); //main query
  query.addFilterQuery("DOC_ID:[1 TO 100]","FROM:[email protected]","popularity:[6 TO 10]"); //filter query , we can pass multiple queries
  query.setFields("DOC_ID","HITS","CAT");
  query.set("defType", "edismax");
  query.setStart(0);    
  
  QueryResponse response = solr.query(query);
  SolrDocumentList results = response.getResults();
  for (int i = 0; i < results.size(); ++i) {
    System.out.println(results.get(i));
  }

Refer main-query-vs-filter-query , CommonQueryParameters for more details.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *