title: UNION and intersection using SPARQL queries
body: <p>I am converting user-defined-queries into SPARQL. For example, when user says, "abc", it means give me all nodes of a given type which have some attribute named "abc".  As an extension of this, if user says, "abc or (pqr and lmn)", I need to find all nodes of a given type for which some attribute is "abc or (pqr and lmn)". Following is the query I have come up with:</p>
  
  <pre><code>SELECT DISTINCT ?node, ?type                                                 
  WHERE                                                                          
  {
    {                                                                              
      ?node a ?type .                                                          
      FILTER ( ?type != &lt;sometype&gt;)
    }
  {           
    {                                                                              
      ?node ?reln0 ?obj0 .                                                     
      FILTER ( regex(str(?obj0), 'abc', "i") )                           
    }                                                                              
    UNION                                                                          
    {                                                                              
      {                                                                              
        ?node ?reln1 ?obj1 .                                                     
        FILTER ( regex(str(?obj1), 'pqr', "i") )                                
      }
      {                                                                             
        ?node ?reln2 ?obj2 .                                                     
        FILTER ( regex(str(?obj2), 'lmn', "i") )                               
      }                                                                              
    }
  }                                                                              
  }                                                                              
  ORDER BY ?node
  </code></pre>
  
  <p>But it doesn't return proper results. Is there something wrong with the above given query? I don't want to use the following because I need to generate the conditions dynamically and each clause needs to be separate.</p>
  
  <pre><code>FILTER (regex(str(?obj2), 'abc', "i") || regex(str(?obj2), 'pqr', "i") &amp;&amp; regex(str(?obj2), 'lmn', "i"))
  </code></pre>