def _MatchField(self, field, match, operator, document): """Check if a field matches a query tree. Args: field_query_node: Either a string containing the name of a field, a query node whose text is the name of the field, or a document_pb.Field. match: A query node to match the field with. operator: The a query node type corresponding to the type of match to perform (eg QueryParser.EQ, QueryParser.GT, etc). document: The document to match. """ if isinstance(field, (basestring, tree.CommonTree)): if isinstance(field, tree.CommonTree): field = query_parser.GetQueryNodeText(field) fields = search_util.GetAllFieldInDocument(document, field) return any( self._MatchField(f, match, operator, document) for f in fields) if field.value().type() in search_util.TEXT_DOCUMENT_FIELD_TYPES: if operator != QueryParser.EQ: return False return self._MatchTextField(field, match, document) if field.value().type() in search_util.NUMBER_DOCUMENT_FIELD_TYPES: return self._MatchNumericField(field, match, operator, document) if field.value().type() == document_pb.FieldValue.DATE: return self._MatchDateField(field, match, operator, document) type_name = document_pb.FieldValue.ContentType_Name( field.value().type()).lower() raise search_util.UnsupportedOnDevError( 'Matching fields of type %s is unsupported on dev server (searched for ' 'field %s)' % (type_name, field.name()))
def _MatchGeoField(self, field, matcher, operator, document): """Check if a geo field matches a query tree node.""" if not isinstance(matcher, DistanceMatcher): return False field = self._GetFieldName(field) values = [field.value() for field in search_util.GetAllFieldInDocument(document, field) if field.value().type() == document_pb.FieldValue.GEO] return matcher.IsMatch(values, operator)
def _MatchGeoField(self, field, matcher, operator, document): """Check if a geo field matches a query tree node.""" if not isinstance(matcher, DistanceMatcher): return False if isinstance(field, tree.CommonTree): field = query_parser.GetQueryNodeText(field) values = [ field.value() for field in search_util.GetAllFieldInDocument(document, field) if field.value().type() == document_pb.FieldValue.GEO ] return matcher.IsMatch(values, operator)
def _MatchAnyField(self, field, match, operator, document): """Check if a field matches a query tree. Args: field: the name of the field, or a query node containing the field. match: A query node to match the field with. operator: The query node type corresponding to the type of match to perform (eg QueryParser.EQ, QueryParser.GT, etc). document: The document to match. """ if isinstance(field, tree.CommonTree): field = query_parser.GetQueryNodeText(field) fields = search_util.GetAllFieldInDocument(document, field) return any(self._MatchField(f, match, operator, document) for f in fields)
def _MatchAnyField(self, field, match, operator, document): """Check if a field matches a query tree. Args: field: the name of the field, or a query node containing the field. match: A query node to match the field with. operator: The query node type corresponding to the type of match to perform (eg QueryParser.EQ, QueryParser.GT, etc). document: The document to match. Raises: ExpressionTreeException: when != operator is used or right hand side of numeric inequality is not a numeric constant. """ fields = search_util.GetAllFieldInDocument(document, self._GetFieldName(field)) return any(self._MatchField(f, match, operator, document) for f in fields)