Use an iterator and proper query method instead of the 'hasAttrSomewhere' method.