def testUnqualifiedTermFieldsWithNestedExpression(self): self.unqualifiedFields = [('aField', 1.0)] expr = QueryExpression.nested(operator='AND') expr.operands = [ QueryExpression.searchterm(term="value1"), QueryExpression.searchterm(term="value2") ] self.assertEquals( { 'type': 'BooleanQuery', 'clauses': [{ 'type': 'TermQuery', 'occur': 'MUST', 'term': { 'field': 'aField', 'value': u'value1' }, 'boost': 1.0 }, { 'type': 'TermQuery', 'occur': 'MUST', 'term': { 'field': 'aField', 'value': u'value2' }, 'boost': 1.0 }], }, self._convert(expr))
def testQueryExpression(self): conversion = Conversion() kwargs = {'q': QueryExpression.searchterm(term='term')} dump = conversion.jsonDumpMessage(message='aMessage', **kwargs) loadedMessage, loadedKwargs = conversion.jsonLoadMessage(dump) self.assertEquals('aMessage', loadedMessage) self.assertEquals({'q': QueryExpression.searchterm(term='term')}, loadedKwargs)
def convert(self, expression): if expression.must_not: r = QueryExpression.nested('AND') r.operands.append(QueryExpression.searchterm(term='*')) r.operands.append(expression) expression = r return JsonDict(self._expression(expression))
def testBooleanOrQuery(self): expr = QueryExpression.nested(operator='OR') expr.operands = [ QueryExpression.searchterm("field1", "=", "value1"), QueryExpression.searchterm("field2", "=", "value2") ] self.assertEquals( { "type": "BooleanQuery", "clauses": [{ "type": "TermQuery", "term": { "field": "field1", "value": "value1" }, "occur": "SHOULD" }, { "type": "TermQuery", "term": { "field": "field2", "value": "value2" }, "occur": "SHOULD" }] }, self._convert(expr))
def testBooleanNotQuery(self): expr = QueryExpression.nested(operator='AND') expr.operands = [ QueryExpression.searchterm("field1", "=", "value1"), QueryExpression.searchterm("field2", "=", "value2") ] expr.operands[1].must_not = True self.assertEquals( { "type": "BooleanQuery", "clauses": [{ "type": "TermQuery", "term": { "field": "field1", "value": "value1" }, "occur": "MUST" }, { "type": "TermQuery", "term": { "field": "field2", "value": "value2" }, "occur": "MUST_NOT" }] }, self._convert(expr))
def testBooleanNotQueryNested(self): expr = QueryExpression.nested(operator='AND') nestedNotExpr = QueryExpression.nested(operator='AND') nestedNotExpr.must_not = True nestedNotExpr.operands = [ QueryExpression.searchterm("field2", "=", "value2"), QueryExpression.searchterm("field3", "=", "value3") ] expr.operands = [QueryExpression.searchterm("field1", "=", "value1"), nestedNotExpr] self.assertEquals( { "type": "BooleanQuery", "clauses": [ { "type": "TermQuery", "term": {"field": "field1", "value": "value1"}, "occur": "MUST" }, { "type": "BooleanQuery", "occur": "MUST_NOT", "clauses": [ { "type": "TermQuery", "term": {"field": "field2", "value": "value2"}, "occur": "MUST" }, { "type": "TermQuery", "term": {"field": "field3", "value": "value3"}, "occur": "MUST" } ] } ] }, self._convert(expr))
def testQueryExpressionWithOperands(self): conversion = Conversion() qe = QueryExpression.nested(operator='AND') qe.operands = [QueryExpression.searchterm(term='term'), QueryExpression.searchterm(term='term1')] kwargs = {'q': qe} dump = conversion.jsonDumpMessage(message='aMessage', **kwargs) loadedMessage, loadedKwargs = conversion.jsonLoadMessage(dump) self.assertEquals('aMessage', loadedMessage) self.assertEquals({'q': qe}, loadedKwargs)
def testIntRangeQuery(self): # (field, lowerTerm, upperTerm, includeLower, includeUpper) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=1, upperTerm=None, includeLower=False, includeUpper=True) self.assertEquals(q, self._convert('intField > 1')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=1, upperTerm=None, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField >= 1')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=None, upperTerm=3, includeLower=True, includeUpper=False) self.assertEquals(q, self._convert('intField < 3')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=None, upperTerm=3, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField <= 3')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=3, upperTerm=3, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField = 3')) self.assertEquals( q, self._convert( QueryExpression.searchterm(index='intField', relation='exact', term=3))) self.assertEquals( q, self._convert( QueryExpression.searchterm(index='intField', relation='=', term=3)))
def convert(self, expression, unqualifiedTermFields=None, composedQuery=None): if expression.must_not: r = QueryExpression.nested('AND') r.operands.append(QueryExpression.searchterm(term='*')) r.operands.append(expression) expression = r return JsonDict(_Converter( analyzer=self._analyzer, fieldRegistry=self._fieldRegistry, ignoreStemmingForWords=self._ignoreStemmingForWords, unqualifiedTermFields=unqualifiedTermFields or self._unqualifiedTermFields, composedQuery=composedQuery).convert(expression))
def testQueryExpressionWithOperands(self): conversion = Conversion() qe = QueryExpression.nested(operator='AND') qe.operands = [ QueryExpression.searchterm(term='term'), QueryExpression.searchterm(term='term1') ] kwargs = {'q': qe} dump = conversion.jsonDumpMessage(message='aMessage', **kwargs) loadedMessage, loadedKwargs = conversion.jsonLoadMessage(dump) self.assertEquals('aMessage', loadedMessage) self.assertEquals({'q': qe}, loadedKwargs)
def testEquals(self): self.assertEquals(QueryExpression(index='field', relation='=', term='term'), QueryExpression(index='field', relation='=', term='term')) self.assertEquals(cqlToExpression('field=value AND otherfield=othervalue'), QueryExpression(operator='AND', operands=[QE('field=value'), QE('otherfield=othervalue')])) self.assertTrue(QueryExpression.searchterm('a') == QueryExpression.searchterm('a')) self.assertFalse(QueryExpression.searchterm('a') != QueryExpression.searchterm('a')) self.assertFalse(QueryExpression.searchterm('a') == QueryExpression.searchterm('b')) self.assertTrue(QueryExpression.searchterm('a') != QueryExpression.searchterm('b'))
def testIntRangeQuery(self): # (field, lowerTerm, upperTerm, includeLower, includeUpper) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=1, upperTerm=None, includeLower=False, includeUpper=True) self.assertEquals(q, self._convert('intField > 1')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=1, upperTerm=None, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField >= 1')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=None, upperTerm=3, includeLower=True, includeUpper=False) self.assertEquals(q, self._convert('intField < 3')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=None, upperTerm=3, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField <= 3')) q = dict(type="RangeQuery", rangeType="Int", field='intField', lowerTerm=3, upperTerm=3, includeLower=True, includeUpper=True) self.assertEquals(q, self._convert('intField = 3')) self.assertEquals(q, self._convert(QueryExpression.searchterm(index='intField', relation='exact', term=3))) self.assertEquals(q, self._convert(QueryExpression.searchterm(index='intField', relation='=', term=3)))
def testUnqualifiedTermFieldsWithNestedExpression(self): self.unqualifiedFields = [('aField', 1.0)] expr = QueryExpression.nested(operator='AND') expr.operands = [ QueryExpression.searchterm(term="value1"), QueryExpression.searchterm(term="value2") ] self.assertEquals({ 'type': 'BooleanQuery', 'clauses': [ {'type': 'TermQuery', 'occur': 'MUST', 'term': {'field': 'aField', 'value': u'value1'}, 'boost': 1.0}, {'type': 'TermQuery', 'occur': 'MUST', 'term': {'field': 'aField', 'value': u'value2'}, 'boost': 1.0} ], }, self._convert(expr))
def testPerQueryUnqualifiedFields(self): self.unqualifiedFields = [('aField', 1.0)] converter = self._prepareConverter() self.assertEquals( { "type": "BooleanQuery", "clauses": [{ "type": "TermQuery", "term": { "field": "aField", "value": "value" }, 'boost': 2.0, 'occur': 'SHOULD' }, { "type": "TermQuery", "term": { "field": "anotherField", "value": "value" }, 'boost': 3.0, 'occur': 'SHOULD' }] }, converter.convert(QueryExpression.searchterm(term="value"), unqualifiedTermFields=[('aField', 2.0), ('anotherField', 3.0)]))
def convert(self, expression, core): filterQueries = {} if expression.operator is None or expression.operator == 'OR': e = QueryExpression.nested('AND') e.operands.append(expression) expression = e if expression.operator == 'AND': oldOperands = expression.operands expression.operands = [] for operand in oldOperands: operandCores = list( self.coresInExpression(expression=operand, core=core)) if len(operandCores) == 1 and operandCores[0] != core: removeCoreFromFieldname(operandCores[0], operand) filterQueries.setdefault(operandCores[0], []).append(operand) else: expression.operands.append(operand) if len(expression.operands) == 1: expression = expression.operands[0] elif len(expression.operands) == 0: expression = None if expression and self.coresInExpression(expression=expression, core=core) != set([core]): raise TooComplexQueryExpression( 'Multiple core query detected, but unable to convert to a correct composed query' ) return expression, filterQueries
def testTermQuery(self): self.assertConversion({ "type": "TermQuery", "term": { "field":"field", "value": "value", } }, QueryExpression.searchterm("field", "=", "value")) self.assertConversion({"term": {"field": "field", "value": "value"}, "type": "TermQuery"}, QueryExpression.searchterm("field", "=", "value"))
def testPhraseOutput(self): self.assertConversion({ "type": "PhraseQuery", "boost": 1.0, "terms": [ {"field": "unqualified", "value": "cats"}, {"field": "unqualified", "value": "dogs"} ] }, QueryExpression.searchterm(term='"cats dogs"'))
def testTermQuery(self): self.assertEquals( { "type": "TermQuery", "term": { "field": "field", "value": "value", } }, self._convert(QueryExpression.searchterm("field", "=", "value"))) self.assertEquals( { "term": { "field": "field", "value": "value" }, "type": "TermQuery" }, self._convert(QueryExpression.searchterm("field", "=", "value")))
def testRightHandSideIsLowercase(self): self.assertEquals( { 'boost': 1.0, 'term': { 'field': 'unqualified', 'value': 'cat' }, 'type': 'TermQuery' }, self._convert(QueryExpression.searchterm(term="CaT")))
def testOtherCoreAndQuery(self): self.assertEquals( { 'type': 'JoinAndQuery', 'first': { "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? "core": "thisCore", "collectKeyName": "A", # where does this keyName come from? "filterKeyName": "A", "query": { "type": "TermQuery", "term": { "field": "field0", "value": "value", } } }, 'second': { "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? "core": "otherCore", "collectKeyName": "A", # where does this keyName come from? "filterKeyName": "A", "query": { "type": "TermQuery", "term": { "field": "field", "value": "value", } } } }, self._convert( QueryExpression( operator='AND', operands=[ QueryExpression.searchterm('field0', '=', 'value'), QueryExpression.searchterm("otherCore.field", "=", "value") ])))
def testOneTermOutputWithANumber(self): self.assertEquals( { 'boost': 1.0, 'term': { 'field': 'unqualified', 'value': '2005' }, 'type': 'TermQuery' }, self._convert(QueryExpression.searchterm(term="2005")))
def testBooleanOrQuery(self): expr = QueryExpression.nested(operator='OR') expr.operands=[ QueryExpression.searchterm("field1", "=", "value1"), QueryExpression.searchterm("field2", "=", "value2") ] self.assertConversion({ "type": "BooleanQuery", "clauses": [ { "type": "TermQuery", "term": {"field": "field1", "value": "value1"}, "occur": "SHOULD" }, { "type": "TermQuery", "term": {"field": "field2", "value": "value2"}, "occur": "SHOULD" } ] }, expr)
def testTermQuery(self): self.assertEquals( { "type": "TermQuery", "term": { "field":"field", "value": "value", } }, self._convert(QueryExpression.searchterm("field", "=", "value"))) self.assertEquals( {"term": {"field": "field", "value": "value"}, "type": "TermQuery"}, self._convert(QueryExpression.searchterm("field", "=", "value")))
def testUnqualifiedTermFields(self): self.unqualifiedFields = [('aField', 1.0)] self.assertEquals( { "type": "TermQuery", "term": { "field": "aField", "value": "value" }, 'boost': 1.0 }, self._convert(QueryExpression.searchterm(term="value")))
def testBooleanNotQuery(self): expr = QueryExpression.nested(operator='AND') expr.operands=[ QueryExpression.searchterm("field1", "=", "value1"), QueryExpression.searchterm("field2", "=", "value2") ] expr.operands[1].must_not = True self.assertConversion({ "type": "BooleanQuery", "clauses": [ { "type": "TermQuery", "term": {"field": "field1", "value": "value1"}, "occur": "MUST" }, { "type": "TermQuery", "term": {"field": "field2", "value": "value2"}, "occur": "MUST_NOT" } ] }, expr)
def testBooleanAndQuery(self): expr = QueryExpression.nested(operator='AND') expr.operands = [ QueryExpression.searchterm("field1", "=", "value1"), QueryExpression.searchterm("field2", "=", "value2") ] self.assertEquals( { "type": "BooleanQuery", "clauses": [ { "type": "TermQuery", "term": {"field": "field1", "value": "value1"}, "occur": "MUST" }, { "type": "TermQuery", "term": {"field": "field2", "value": "value2"}, "occur": "MUST" } ] }, self._convert(expr))
def testPhraseOutput(self): self.assertEquals( { "type": "PhraseQuery", "boost": 1.0, "terms": [{ "field": "unqualified", "value": "cats" }, { "field": "unqualified", "value": "dogs" }] }, self._convert(QueryExpression.searchterm(term='"cats dogs"')))
def testOtherCoreAndQuery(self): self.assertEquals({ 'type': 'JoinAndQuery', 'first': { "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? "core": "thisCore", "collectKeyName": "A", # where does this keyName come from? "filterKeyName": "A", "query": { "type": "TermQuery", "term": { "field":"field0", "value": "value", } } }, 'second': { "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? "core": "otherCore", "collectKeyName": "A", # where does this keyName come from? "filterKeyName": "A", "query": { "type": "TermQuery", "term": { "field":"field", "value": "value", } } } }, self._convert( QueryExpression(operator='AND', operands=[ QueryExpression.searchterm('field0', '=', 'value'), QueryExpression.searchterm("otherCore.field", "=", "value") ]) ) )
def testNotExpression(self): expr = QueryExpression.searchterm("field", "=", "value") expr.must_not = True self.assertConversion({ "type": "BooleanQuery", "clauses": [ { "type": "MatchAllDocsQuery", "occur": "MUST" }, { "type": "TermQuery", "term": {"field": "field", "value": "value"}, "occur": "MUST_NOT" } ] }, expr)
def testConversionOfComposedQuery(self): conversion = Conversion() cq = ComposedQuery('coreA') cq.setCoreQuery(core='coreA', query=parseString('Q0'), filterQueries=['Q1', 'Q2'], facets=['F0', 'F1']) cq.setCoreQuery(core='coreB', query=QueryExpression.searchterm(term='Q3'), filterQueries=['Q4']) cq.addMatch(dict(core='coreA', uniqueKey='keyA'), dict(core='coreB', key='keyB')) cq.addUnite(dict(core='coreA', query='AQuery'), dict(core='coreB', query='anotherQuery')) cq.start = 0 cq.sortKeys = [dict(sortBy='field', sortDescending=True)] kwargs = {'q': cq} dump = conversion.jsonDumpMessage(message='aMessage', **kwargs) self.assertEquals(str, type(dump)) message, kwargs = conversion.jsonLoadMessage(dump) self.assertEquals('aMessage', message) cq2 = kwargs['q'] self.assertEquals(parseString('Q0'), cq2.queryFor('coreA'))
def testMultipleUnqualifiedTermFields(self): self.unqualifiedFields = [('aField', 1.0), ('oField', 2.0)] self.assertConversion({ "type": "BooleanQuery", "clauses": [ { "type": "TermQuery", "term": {"field": "aField", "value": "value"}, "boost": 1.0, "occur": "SHOULD" }, { "type": "TermQuery", "term": {"field": "oField", "value": "value"}, "boost": 2.0, "occur": "SHOULD" } ] }, QueryExpression.searchterm(term="value"))
def testPerQueryUnqualifiedFields(self): self.unqualifiedFields = [('aField', 1.0)] converter = self._prepareConverter() self.assertEquals({ "type": "BooleanQuery", "clauses": [{ "type": "TermQuery", "term": {"field": "aField", "value": "value"}, 'boost': 2.0, 'occur': 'SHOULD' }, { "type": "TermQuery", "term": {"field": "anotherField", "value": "value"}, 'boost': 3.0, 'occur': 'SHOULD' }]}, converter.convert( QueryExpression.searchterm(term="value"), unqualifiedTermFields=[('aField', 2.0), ('anotherField', 3.0)]))
def testNotExpression(self): expr = QueryExpression.searchterm("field", "=", "value") expr.must_not = True self.assertEquals( { "type": "BooleanQuery", "clauses": [{ "type": "MatchAllDocsQuery", "occur": "MUST" }, { "type": "TermQuery", "term": { "field": "field", "value": "value" }, "occur": "MUST_NOT" }] }, self._convert(expr))
def testOtherCoreTermQuery(self): query = ComposedQuery('thisCore') query.cores.add('otherCore') query.addMatch( dict(core='thisCore', uniqueKey='A'), dict(core='otherCore', uniqueKey='B') ) self.assertEquals({ "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? (with MatchAllDocs) "core": "otherCore", "collectKeyName": "B", "filterKeyName": "B", "query": { "type": "TermQuery", "term": { "field": "field", "value": "value", } }}, self._convert(QueryExpression.searchterm("otherCore.field", "=", "value"), composedQuery=query))
def testOtherCoreTermQuery(self): query = ComposedQuery('thisCore') query.cores.add('otherCore') query.addMatch(dict(core='thisCore', uniqueKey='A'), dict(core='otherCore', uniqueKey='B')) self.assertEquals( { "type": "RelationalLuceneQuery", # should this not be 'joined' to own core somehow? (with MatchAllDocs) "core": "otherCore", "collectKeyName": "B", "filterKeyName": "B", "query": { "type": "TermQuery", "term": { "field": "field", "value": "value", } } }, self._convert(QueryExpression.searchterm("otherCore.field", "=", "value"), composedQuery=query))
def convert(self, expression, core): filterQueries = {} if expression.operator is None or expression.operator == 'OR': e = QueryExpression.nested('AND') e.operands.append(expression) expression = e if expression.operator == 'AND': oldOperands = expression.operands expression.operands = [] for operand in oldOperands: operandCores = list(self.coresInExpression(expression=operand, core=core)) if len(operandCores) == 1 and operandCores[0] != core: removeCoreFromFieldname(operandCores[0], operand) filterQueries.setdefault(operandCores[0], []).append(operand) else: expression.operands.append(operand) if len(expression.operands) == 1: expression = expression.operands[0] elif len(expression.operands) == 0: expression = None if expression and self.coresInExpression(expression=expression, core=core) != set([core]): raise TooComplexQueryExpression('Multiple core query detected, but unable to convert to a correct composed query') return expression, filterQueries
def testMultipleUnqualifiedTermFields(self): self.unqualifiedFields = [('aField', 1.0), ('oField', 2.0)] self.assertEquals( { "type": "BooleanQuery", "clauses": [{ "type": "TermQuery", "term": { "field": "aField", "value": "value" }, "boost": 1.0, "occur": "SHOULD" }, { "type": "TermQuery", "term": { "field": "oField", "value": "value" }, "boost": 2.0, "occur": "SHOULD" }] }, self._convert(QueryExpression.searchterm(term="value")))
def testMatchAllQuery(self): self.assertEquals({"type": "MatchAllDocsQuery"}, self._convert(QueryExpression.searchterm(term="*")))
def testConvertJoinQueryToFilters(self): consume(self.tree.any.executeQuery(cqlAbstractSyntaxTree=parseCQL('field=value AND otherCore.field=value2'))) self.assertEquals(['executeComposedQuery'], self.observer.calledMethodNames()) cq = self.observer.calledMethods[0].kwargs['query'] self.assertEqual(QueryExpression.searchterm('field', '=', 'value'), cq.queryFor('defaultCore')) self.assertEqual([QueryExpression.searchterm('field', '=', 'value2')], cq.filterQueriesFor('otherCore'))
def testMatchAllQuery(self): self.assertConversion({"type": "MatchAllDocsQuery"}, QueryExpression.searchterm(term="*"))
def __init__(self, arguments, default_facet_terms_count, maximum_record_number, page_size, default_record_schema): query = arguments.pop('query', [None])[0] if query is None: raise MissingArgument('query') self._request = dict(query=query) self._next_request = dict(query=query) self.query_expression = WebQuery(query, antiUnaryClause="*").query page = getInt(arguments, 'page', 1) if page <= 0: raise InvalidArgument('page', 'expected value > 0') if 'page' in arguments: self._request['page'] = page self._next_request['page'] = page arguments.pop('page') sortKeys = None if 'sort' in arguments: sort = [ s.strip() for sortOption in arguments.pop('sort') for s in sortOption.split(',') if s.strip() ] sortKeys = [] for key in sort: ascending = True if key.startswith('-'): ascending = False key = key[1:] if key: sortKeys.append( dict(sortBy=key, sortDescending=not ascending)) self._request.setdefault( 'sort', []).append(('' if ascending else '-') + key) self._next_request.setdefault( 'sort', []).append(('' if ascending else '-') + key) self.recordSchema = arguments.pop('recordSchema', [default_record_schema])[0] pageSize = getInt(arguments, 'page-size', page_size) if pageSize < 0: raise InvalidArgument('page-size', 'expected value >= 0') if 'page-size' in arguments: self._request['page-size'] = pageSize self._next_request['page-size'] = pageSize arguments.pop('page-size') self.start = (page - 1) * pageSize if self.start > maximum_record_number: raise InvalidArgument( 'page', 'expected value <= {}'.format( (maximum_record_number + pageSize - 1) // pageSize)) self.stop = min(self.start + pageSize, maximum_record_number) facets = arguments.pop('facet', []) queryFacets = [] for facet in facets: fieldname = facet maxTerms = default_facet_terms_count splitted = facet.rsplit(':', 1) try: maxTerms = int(splitted[1]) if len( splitted) > 1 else default_facet_terms_count fieldname = splitted[0] except ValueError: pass queryFacets.append( dict(fieldname=fieldname, maxTerms=maxTerms, sortBy=DRILLDOWN_SORTBY_COUNT)) self._request.setdefault('facet', []).append({ 'index': fieldname, 'max-terms': maxTerms }) facet_term_count = ':{0}'.format( maxTerms) if maxTerms != default_facet_terms_count else '' self._next_request.setdefault('facet', []).append(fieldname + facet_term_count) facetFilters = arguments.pop('facet-filter', []) if facetFilters: self._request['facet-filter'] = [] self._next_request['facet-filter'] = [] q = QueryExpression.nested('AND') q.operands.append(self.query_expression) for facetFilter in facetFilters: if '=' not in facetFilter: raise InvalidArgument( 'facet-filter', "expected <field>=<value> as a filter") index, term = facetFilter.split('=', 1) q.operands.append( QueryExpression.searchterm(index=index, relation='exact', term=term)) self._request['facet-filter'].append({ 'index': index, 'term': term }) self._next_request['facet-filter'].append('{}={}'.format( index, term)) self.query_expression = q self.queryKwargs = dict( start=self.start, stop=self.stop, query=self.query_expression, facets=queryFacets or None, sortKeys=sortKeys or None, ) extra_arguments = {} for k, v in arguments.items(): if k.startswith('x-'): extra_arguments[k] = v else: raise BadArgument(k) if extra_arguments: self._request.update(extra_arguments) self._next_request.update(extra_arguments) self.queryKwargs['extraArguments'] = extra_arguments
def testAsDictFromDict(self): expression = cqlToExpression('aap NOT (noot OR title=mies) AND subject =/boost=3.0 boeien') d = expression.asDict() self.assertEquals(dict, type(d)) self.assertEquals(expression, QueryExpression.fromDict(d))
def testQuery(self): self.request() executeQueryMethod = self.observer.calledMethods[0] self.assertEquals(QueryExpression.searchterm(term='*'), executeQueryMethod.kwargs['query'])
def testQuery(self): self.request() executeQueryMethod = self.observer.calledMethods[0] self.assertEqual(QueryExpression.searchterm(term='*'), executeQueryMethod.kwargs['query'])
def testUnqualifiedTermFields(self): self.unqualifiedFields = [('aField', 1.0)] self.assertEquals( {"type": "TermQuery", "term": {"field": "aField", "value": "value"}, 'boost': 1.0}, self._convert(QueryExpression.searchterm(term="value")))
def testMatchAllQuery(self): self.assertEquals( {"type": "MatchAllDocsQuery"}, self._convert(QueryExpression.searchterm(term="*")))
def testOneTermOutputWithANumber(self): self.assertEquals( {'boost': 1.0, 'term': {'field': 'unqualified', 'value': '2005'}, 'type': 'TermQuery'}, self._convert(QueryExpression.searchterm(term="2005")))
def executeQuery(self, query=None, extraArguments=None, facets=None, drilldownQueries=None, filterQueries=None, sortKeys=None, **kwargs): if 'cqlAbstractSyntaxTree' in kwargs: query = kwargs.pop('cqlAbstractSyntaxTree') query = cqlToExpression(query) extraArguments = extraArguments or {} cq = ComposedQuery(self._resultsFrom) for matchTuple in self._matches: cq.addMatch(*matchTuple) for key in ['start', 'stop', 'suggestionRequest']: if key in kwargs: setattr(cq, key, kwargs[key]) coreQuery, filters = self._extraFilterQueries.convert(query, self._resultsFrom) if coreQuery: cq.setCoreQuery(core=self._resultsFrom, query=coreQuery) for core, aFilter in ((core, aFilter) for core, filters in filters.items() for aFilter in filters): cq.addFilterQuery(core, aFilter) for sortKey in sortKeys or []: core, sortBy = self._parseCorePrefix(sortKey['sortBy'], self._cores) cq.addSortKey(dict(sortKey, core=core, sortBy=sortBy)) filters = extraArguments.get('x-filter', []) for f in filters: core, filterQuery = self._coreQuery(query=f, cores=self._cores) cq.addFilterQuery(core=core, query=filterQuery) for core, filterQuery in filterQueries or []: cq.addFilterQuery(core=core, query=cqlToExpression(filterQuery)) rankQueries = extraArguments.get('x-rank-query', []) if rankQueries: queries = defaultdict(list) for rankQuery in rankQueries: core, rankQuery = self._parseCorePrefix(rankQuery, self._cores) queries[core].append(rankQuery) for core, q in queries.items(): cq.setRankQuery(core=core, query=cqlToExpression(' OR '.join(q))) if self._dedupFieldName: if 'true' == extraArguments.get('x-filter-common-keys', ['true' if self._dedupByDefault else 'false'])[0]: setattr(cq, "dedupField", self._dedupFieldName) setattr(cq, "dedupSortField", self._dedupSortFieldName) if self._groupingEnabled and 'true' == extraArguments.get('x-grouping', [None])[0]: if coreQuery != QueryExpression.searchterm(term='*'): setattr(cq, "groupingField", self._groupingFieldName) if self._clusteringEnabled and 'true' == extraArguments.get('x-clustering', [None])[0]: setattr(cq, "clustering", True) facetOrder = [] fieldTranslations = {} for drilldownField in (facets or []): path = drilldownField['fieldname'].split('>') fieldname, path = path[0], path[1:] facetOrder.append((fieldname, path)) core, newFieldname = self._coreFacet(fieldname, self._cores) newFieldname = self._drilldownFieldnamesTranslate(newFieldname) fieldTranslations[newFieldname] = fieldname cq.addFacet(core=core, facet=dict(fieldname=newFieldname, path=path, maxTerms=drilldownField['maxTerms'])) for drilldownQuery in (drilldownQueries or []): core, fieldname = self._coreDrilldownQuery(drilldownQuery[0], self._cores) fieldname = self._drilldownFieldnamesTranslate(fieldname) cq.addDrilldownQuery(core=core, drilldownQuery=(fieldname, drilldownQuery[1])) result = yield self.any.executeComposedQuery(query=cq) drilldownData = getattr(result, "drilldownData", None) if drilldownData: for facet in drilldownData: fieldname = facet['fieldname'] facet['fieldname'] = fieldTranslations.get(fieldname, fieldname) result.drilldownData = sorted( drilldownData, key=lambda d: facetOrder.index((d['fieldname'], d.get('path', []))) ) raise StopIteration(result)
def testRightHandSideIsLowercase(self): self.assertEquals( {'boost': 1.0, 'term': {'field': 'unqualified', 'value': 'cat'}, 'type': 'TermQuery'}, self._convert(QueryExpression.searchterm(term="CaT")))