def _MatchField(self, field, match, operator, document): """Check if a field matches a query tree. Args: field: a document_pb.Field instance to match. 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 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) if field.value().type() == document_pb.FieldValue.GEO: return False 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 _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 _CheckOp(self, op): if op == QueryParser.EQ: raise ExpressionTreeException('Equality comparison not available for Geo type') if op == QueryParser.NE: raise ExpressionTreeException('!= comparison operator is not available') if op not in (QueryParser.GT, QueryParser.GE, QueryParser.LESSTHAN, QueryParser.LE): raise search_util.UnsupportedOnDevError( 'Operator %s not supported for distance matches on development server.' % str(op))
def _MatchComparableField(self, field, match, cast_to_type, query_node_types, document): """A generic method to test matching for comparable types. Comparable types are defined to be anything that supports <, >, <=, >=, == and !=. For our purposes, this is numbers and dates. Args: field: The document_pb.Field to test match: The query node to match against cast_to_type: The type to cast the node string values to query_node_types: The query node types that would be valid matches document: The document that the field is in Returns: True iff the field matches the query. Raises: UnsupportedOnDevError: Raised when an unsupported operator is used, or when the query node is of the wrong type. """ field_val = cast_to_type(field.value().string_value()) op = QueryParser.EQ if match.getType() in query_node_types: try: match_val = cast_to_type(query_parser.GetQueryNodeText(match)) except ValueError: return False elif match.children: op = match.getType() try: match_val = cast_to_type( query_parser.GetQueryNodeText(match.children[0])) except ValueError: return False else: return False if op is QueryParser.EQ: return field_val == match_val if op is QueryParser.NE: return field_val != match_val if op is QueryParser.GT: return field_val > match_val if op is QueryParser.GE: return field_val >= match_val if op is QueryParser.LT: return field_val < match_val if op is QueryParser.LE: return field_val <= match_val raise search_util.UnsupportedOnDevError( 'Operator %s not supported for numerical fields on development server.' % match.getText())
def _MatchComparableField(self, field, match, cast_to_type, op, document): """A generic method to test matching for comparable types. Comparable types are defined to be anything that supports <, >, <=, >=, ==. For our purposes, this is numbers and dates. Args: field: The document_pb.Field to test match: The query node to match against cast_to_type: The type to cast the node string values to op: The query node type representing the type of comparison to perform document: The document that the field is in Returns: True iff the field matches the query. Raises: UnsupportedOnDevError: Raised when an unsupported operator is used, or when the query node is of the wrong type. ExpressionTreeException: Raised when a != inequality operator is used. """ field_val = cast_to_type(field.value().string_value()) if match.getType() == QueryParser.VALUE: try: match_val = cast_to_type( query_parser.GetPhraseQueryNodeText(match)) except ValueError: return False else: return False if op == QueryParser.EQ or op == QueryParser.HAS: return field_val == match_val if op == QueryParser.NE: raise ExpressionTreeException( '!= comparison operator is not available') if op == QueryParser.GT: return field_val > match_val if op == QueryParser.GE: return field_val >= match_val if op == QueryParser.LESSTHAN: return field_val < match_val if op == QueryParser.LE: return field_val <= match_val raise search_util.UnsupportedOnDevError( 'Operator %s not supported for numerical fields on development server.' % match.getText())
def _MatchField(self, field_query_node, match, document): """Check if a field matches a query tree.""" if isinstance(field_query_node, str): field = search_util.GetFieldInDocument(document, field_query_node) else: field = search_util.GetFieldInDocument(document, field_query_node.getText()) if not field: return False if field.value().type() in search_util.TEXT_DOCUMENT_FIELD_TYPES: return self._MatchTextField(field, match, document) if field.value().type() in search_util.NUMBER_DOCUMENT_FIELD_TYPES: return self._MatchNumericField(field, match, document) if field.value().type() == document_pb.FieldValue.DATE: return self._MatchDateField(field, match, document) raise search_util.UnsupportedOnDevError( 'Matching to field type of field "%s" (type=%d) is unsupported on ' 'dev server' % (field.name(), field.value().type()))
def RaiseUnsupported(*args): raise search_util.UnsupportedOnDevError( '%s is currently unsupported on dev_appserver.' % method)