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?

When used in a querying context, the query becomes a “scoring” query. Similar to its non-scoring sibling, this determines if a document matches and how well the document matches.

A typical use for a query is to find documents:

  • Best matching the words full text search
  • Containing the word run, but maybe also matching runs, running, jog, or sprint
  • Containing the words solr, blog, and post—the closer together they are, the more relevant the document

A scoring query calculates how relevant each document is to the query, and assigns it a relevance _score, which is later used to sort matching documents by relevance. This concept of relevance is well suited to full-text search, where there is seldom a completely “correct” answer.

query params

fq param used for filter queries and q param is used for main query.

SolrJ Example

HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr");

    SolrQuery query = new SolrQuery();
    query.setQuery("solr blog"); //main query
    query.addFilterQuery("DOC_ID:[1 TO 100]","FROM:[email protected]"); //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));
    }

 

Performance Comparison

Filtering queries are simple checks for set inclusion/exclusion, which make them very fast to compute. There are various optimizations that can be leveraged when at least one of your filtering query is “sparse” (few matching documents), and frequently used non-scoring queries can be cached in memory for faster access.

In contrast, scoring queries have to not only find matching documents, but also calculate how relevant each document is, which typically makes them heavier than their non-scoring counterparts. Also, query results are not cacheable.

Thanks to the inverted index, a simple scoring query that matches just a few documents may perform as well or better than a filter that spans millions of documents. In general, however, a filter will outperform a scoring query. And it will do so consistently.

The goal of filtering is to reduce the number of documents that have to be examined by the scoring queries.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

2 comments. Leave new

I suppose this scoring will be relevant when you have a composite query with OR ed sub-queries for fields with different scores.

Yes,scoring will be relevant when we have a query with different scores.When we do not apply any boost factor,it will take 1.0 as a boost factor which is default.

Leave a Reply

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