0%

Why parentheses are important: combine multiple Boolean operators

When you combine multiple filters, it’s best to use parentheses to ensure the operators are applied as you intend.

Let’s say we are interested in two genes, Prkdc and Wrn, with a p-value less than 0.0001. The query without parentheses for this example is:

p_value:[* TO 1e-4] AND marker_symbol:Prkdc OR marker_symbol:Wrn. 

It returns seven documents. The output includes only the Prkdc gene hits because of the order of operations.

First, the p-value less than 1e-4 and the Prkdc marker_symbol are filtered out, and then the second boolean operation is applied. However, by this point, everything that is not Prkdc has already been omitted. Query with parentheses:

num_found, df = solr_request(
    core='statistical-result', 
    params={
        'q': 'p_value:[* TO 1e-4] AND (marker_symbol:Prkdc OR marker_symbol:Wrn)',
        'fl': 'marker_symbol,top_level_mp_term_name,effect_size,p_value',
        'rows': 10
    }
)

We found 15 documents instead of 7 because the order of operations has changed. First, we filtered out genes with the marker_symbol Prkdc or Wrn, then applied the p-value filter.

It is important to specify the order of operations to avoid unexpected behaviour of the function. Use parentheses, when multiple boolean operators are applied.