prevent qrewrite returning same nested query as the main one

* fix query_rewrite when already querying by same PK and the field is actually missing (e.g. DBS3 vs DBS2).
* minor refactoring

this was happening if querying by PK, and the field [that was in keylearning] is not available in results: 
the suggested main query and the subquery are the same: e.g.  as ```run.bfield``` is not existent in DBS3, and if our keylearning is not updated, ```run=123 | grep run.bfield``` would return this:

```
run run=123 | grep run.run_number
for each result:
    run run=<PK> | grep run.bfield  (replace <PK> with value of run.run_number from first query)
``` 
which is incorect.