def visitSEARCH_CLAUSE(self, node): # possible children: # CQL_QUERY # SEARCH_TERM # INDEX, RELATION, SEARCH_TERM firstChild = node.children[0].name results = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': (unqualifiedRhs,) = results unqualifiedTermFields = [ _formatBoost(_formatTerm(fieldname, unqualifiedRhs), boost) for fieldname, boost in self._unqualifiedTermFields ] if len(unqualifiedTermFields) == 1: return unqualifiedTermFields[0] return "(%s)" % ' OR '.join(unqualifiedTermFields) elif firstChild == 'INDEX': (index, (relation, boost), term) = results if relation in ['==', 'exact']: query = '%s:"%s"' % (index, term) elif relation == '=': query = _formatTerm(index, term) elif relation in ['>', '<', '>=', '<=']: query = _formatRangeTerm(index, relation, term) else: raise UnsupportedCQL("Only =, ==, exact, <, <=, > and >= are supported.") return _formatBoost(query, boost) else: ((query,),) = results return query
def visitSEARCH_CLAUSE(self, node): # possible children: # CQL_QUERY # SEARCH_TERM # INDEX, RELATION, SEARCH_TERM firstChild = node.children[0].name results = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': (unqualifiedRhs, ) = results unqualifiedTermFields = [ _formatBoost(_formatTerm(fieldname, unqualifiedRhs), boost) for fieldname, boost in self._unqualifiedTermFields ] if len(unqualifiedTermFields) == 1: return unqualifiedTermFields[0] return "(%s)" % ' OR '.join(unqualifiedTermFields) elif firstChild == 'INDEX': (index, (relation, boost), term) = results if relation in ['==', 'exact']: query = '%s:"%s"' % (index, term) elif relation == '=': query = _formatTerm(index, term) elif relation in ['>', '<', '>=', '<=']: query = _formatRangeTerm(index, relation, term) else: raise UnsupportedCQL( "Only =, ==, exact, <, <=, > and >= are supported.") return _formatBoost(query, boost) else: ((query, ), ) = results return query
def visitSEARCH_CLAUSE(self, node): firstChild = node.children[0].name result = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': self._logger(clause = result[0].lower()) elif firstChild == 'INDEX': self._logger(clause = "%s %s %s" % (result[0], result[1], quot(result[2].lower()))) return result
def visitSCOPED_CLAUSE(self, node): clause = CqlVisitor.visitSCOPED_CLAUSE(self, node) if len(clause) == 1: return clause[0] lhs, operator, rhs = clause query = BooleanQuery() query.add(lhs, LHS_OCCUR[operator]) query.add(rhs, RHS_OCCUR[operator]) return query
def visitSEARCH_CLAUSE(self, node): firstChild = node.children[0].name result = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': self._logger(clause=result[0].lower()) elif firstChild == 'INDEX': self._logger(clause="%s %s %s" % (result[0], result[1], quot(result[2].lower()))) return result
def visitRELATION(self, node): results = CqlVisitor.visitRELATION(self, node) if len(results) == 1: relation = results[0] boost = 1.0 else: (relation, (modifier, comparitor, value)) = results boost = float(value) return relation, boost
def visitSEARCH_CLAUSE(self, node): # possible children: # CQL_QUERY # SEARCH_TERM # INDEX, RELATION, SEARCH_TERM firstChild = node.children[0].name results = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': (unqualifiedRhs, ) = results if unqualifiedRhs == '*': return MatchAllDocsQuery() subQueries = [] for fieldname, boost in self._unqualifiedTermFields: subQuery = self._termOrPhraseQuery(fieldname, unqualifiedRhs) if isinstance( subQuery, PhraseQuery ) and not self._fieldRegistry.phraseQueryPossible(fieldname): continue subQuery.setBoost(boost) subQueries.append(subQuery) if len(subQueries) == 1: query = subQueries[0] else: query = BooleanQuery() for subQuery in subQueries: query.add(subQuery, BooleanClause.Occur.SHOULD) return query elif firstChild == 'INDEX': (left, (relation, boost), right) = results if relation in [ '==', 'exact' ] or (relation == '=' and self._fieldRegistry.isUntokenized(left)): query = TermQuery(self._createTerm(left, right)) elif relation == '=': query = self._termOrPhraseQuery(left, right) elif relation in ['<', '<=', '>=', '>']: query = self._termRangeQuery(left, relation, right) else: raise UnsupportedCQL("'%s' not supported for the field '%s'" % (relation, left)) query.setBoost(boost) return query else: ((query, ), ) = results return query
def visitSEARCH_CLAUSE(self, node): # possible children: # CQL_QUERY # SEARCH_TERM # INDEX, RELATION, SEARCH_TERM firstChild = node.children[0].name results = CqlVisitor.visitSEARCH_CLAUSE(self, node) if firstChild == 'SEARCH_TERM': (unqualifiedRhs,) = results if unqualifiedRhs == '*': return MatchAllDocsQuery() subQueries = [] for fieldname, boost in self._unqualifiedTermFields: subQuery = self._termOrPhraseQuery(fieldname, unqualifiedRhs) if isinstance(subQuery, PhraseQuery) and not self._fieldRegistry.phraseQueryPossible(fieldname): continue subQuery.setBoost(boost) subQueries.append(subQuery) if len(subQueries) == 1: query = subQueries[0] else: query = BooleanQuery() for subQuery in subQueries: query.add(subQuery, BooleanClause.Occur.SHOULD) return query elif firstChild == 'INDEX': (left, (relation, boost), right) = results if relation in ['==', 'exact'] or (relation == '=' and self._fieldRegistry.isUntokenized(left)): query = TermQuery(self._createTerm(left, right)) elif relation == '=': query = self._termOrPhraseQuery(left, right) elif relation in ['<','<=','>=','>']: query = self._termRangeQuery(left, relation, right) else: raise UnsupportedCQL("'%s' not supported for the field '%s'" % (relation, left)) query.setBoost(boost) return query else: ((query,),) = results return query
def visitRELATION(self, node): result = CqlVisitor.visitRELATION(self, node) if len(result) == 1: return result[0] relation, (modifier, comparitor, value) = result return "%s/%s%s%s" % (relation, modifier, comparitor, value)
def __init__(self, astTree, logger): CqlVisitor.__init__(self, astTree) self._logger = logger
def __init__(self, unqualifiedTermFields, node, analyzer, fieldRegistry): CqlVisitor.__init__(self, node) self._unqualifiedTermFields = unqualifiedTermFields self._analyzer = analyzer self._fieldRegistry = fieldRegistry
def visitINDEX(self, node): results = CqlVisitor.visitINDEX(self, node) return results.replace(':', '\\:')
def __init__(self, ast, unqualifiedTermFields): CqlVisitor.__init__(self, ast) self._unqualifiedTermFields = unqualifiedTermFields
def visitSCOPED_CLAUSE(self, node): clause = CqlVisitor.visitSCOPED_CLAUSE(self, node) if len(clause) == 1: return clause[0] return "(%s)" % ' '.join(clause)