def testFilterX4(self): wq = WebQuery('fiets') wq.addFilter('field1', 'value1') wq.addFilter('field2', 'value2') wq.addFilter('field3', 'value3') wq.addFilter('field4', 'value4') self.assertCql(parseCql('(fiets) AND field1 exact value1 AND field2 exact value2 AND field3 exact value3 AND field4 exact value4'), wq.ast)
def testQuery(self): wq = WebQuery('cats', antiUnaryClause='antiunary exact true') self.assertFalse(wq.isBooleanQuery()) self.assertFalse(wq.isPlusMinusQuery()) self.assertTrue(wq.isDefaultQuery()) self.assertFalse(wq.needsBooleanHelp()) self.assertEquals(parseCql('cats'), wq.ast)
def testReplaceTermsWithFilters(self): wq = WebQuery('fiets kaart') wq.addFilter('label', 'value') newWq = wq.replaceTerm('fiets', 'bike') self.assertEquals('fiets kaart', wq.original) self.assertEquals('bike AND kaart', newWq.original) self.assertCql(parseCql('(bike AND kaart) AND label exact value'), newWq.ast)
def _assertQuery(self, expected, input, boolean=False, plusminus=False, default=False, needsBooleanHelp=False, asString=None): input = expected if input == None else input asString = expected if asString == None else asString wq = WebQuery(input, antiUnaryClause='antiunary exact true') self.assertEquals((boolean, plusminus, default, needsBooleanHelp), (wq.isBooleanQuery(), wq.isPlusMinusQuery(), wq.isDefaultQuery(), wq.needsBooleanHelp())) self.assertEquals(parseCql(expected), wq.ast) self.assertEqual(cqlToExpression(expected), wq.query) self.assertEquals(asString, wq.asString()) self.assertEquals(input, wq.original)
def testUnqualifiedTermFields(self): composer = LuceneQueryComposer(unqualifiedTermFields=[("field0", 0.2), ("field1", 2.0)], luceneSettings=LuceneSettings()) ast = parseCql("value") result = composer.compose(ast) query = BooleanQuery() left = TermQuery(Term("field0", "value")) left.setBoost(0.2) query.add(left, BooleanClause.Occur.SHOULD) right = TermQuery(Term("field1", "value")) right.setBoost(2.0) query.add(right, BooleanClause.Occur.SHOULD) self.assertEquals(type(query), type(result)) self.assertEquals(repr(query), repr(result))
def convert(self, expression=None, cql=None): if expression is None: expression = cqlToExpression(parseCql(cql)) unqualifiedFields = getattr(self, 'unqualifiedFields', [("unqualified", 1.0)]) settings = LuceneSettings() if hasattr(self, '_analyzer'): settings.analyzer = self._analyzer if hasattr(self, 'fieldRegistry'): settings.fieldRegistry = self.fieldRegistry else: settings.fieldRegistry = FieldRegistry() settings.fieldRegistry.register("intField", fieldDefinition=INTFIELD) settings.fieldRegistry.register("longField", fieldDefinition=LONGFIELD) converter = QueryExpressionToLuceneQueryDict( unqualifiedTermFields=unqualifiedFields, luceneSettings=settings, ignoreStemmingForWords=getattr(self, '_ignoredStemmingForWords', None) ) return converter.convert(expression)
def testTermFilter(self): wq = WebQuery('fiets') wq.addTermFilter("water") #self.assertCql(parseCql('water AND (fiets)'), wq.ast) self.assertCql(parseCql('(fiets) AND water'), wq.ast)
def testAcceptsCqlAstAndQueryExpression(self): a = cqlToExpression('field = value') b = cqlToExpression(parseCql('field = value')) c = cqlToExpression(b) self.assertEqual(a, b) self.assertEqual(a, c)
def testSimpleExpression(self): expression = cqlToExpression(parseCql('field=value')) self.assertEqual("field", expression.index) self.assertEqual("=", expression.relation) self.assertEqual("value", expression.term)
def testAcceptsCqlAstAndQueryExpression(self): a = cqlToExpression('field = value') b = cqlToExpression(parseCql('field = value')) c = cqlToExpression(b) self.assertEquals(a, b) self.assertEquals(a, c)
def testFilterWithSpaces(self): wq = WebQuery('fiets') wq.addFilter('field', 'value with spaces') self.assertCql(parseCql('(fiets) AND field exact "value with spaces"'), wq.ast)
def assertConversion(self, expected, input): result = self.composer.compose(parseCql(input)) self.assertEquals(type(expected), type(result), "expected %s, but got %s" % (repr(expected), repr(result))) self.assertEquals(repr(expected), repr(result))
def cqlToExpression(cql): if isinstance(cql, QueryExpression): return cql if not hasattr(cql, 'accept'): cql = parseCql(cql) return CqlToExpressionVisitor(cql).visit()
def assertConversion(self, expected, input): result = self.composer.compose(parseCql(input)) self.assertEquals(expected, result)
def luceneQueryFromCql(cqlString): return LuceneQueryComposer([], luceneSettings=LuceneSettings()).compose(parseCql(cqlString))
def testFilter(self): wq = WebQuery('fiets') wq.addFilter('field', 'value') self.assertCql(parseCql('(fiets) AND field exact value'), wq.ast)
def luceneQueryFromCql(cqlString): return LuceneQueryComposer(unqualifiedTermFields=[], luceneSettings=LuceneSettings()).compose(parseCql(cqlString))
def _makeExpression(self, input): return cqlToExpression(parseCql(input)) if isinstance( input, basestring) else input
def testWildcards(self): query = PrefixQuery(Term('unqualified', 'prefix')) self.assertConversion(query, 'prefix*') self.assertConversion(query, 'PREfix*') query = PrefixQuery(Term('field', 'prefix')) self.assertConversion(query, 'field="PREfix*"') self.assertConversion(query, 'field=prefix*') query = PrefixQuery(Term('field', 'oc-0123')) self.assertConversion(query, 'field="oc-0123*"') query = TermQuery(Term('field', 'p')) self.assertConversion(query, 'field="P*"') #only prefix queries for now query = TermQuery(Term('field', 'post')) self.assertConversion(query, 'field="*post"') query = TermQuery(Term('field', 'prefix')) self.assertConversion(query, 'field=prefix**') result = LuceneQueryComposer(unqualifiedTermFields=[("field0", 0.2), ("field1", 2.0)], luceneSettings=LuceneSettings()).compose(parseCql("prefix*")) query = BooleanQuery() left = PrefixQuery(Term("field0", "prefix")) left.setBoost(0.2) query.add(left, BooleanClause.Occur.SHOULD) right = PrefixQuery(Term("field1", "prefix")) right.setBoost(2.0) query.add(right, BooleanClause.Occur.SHOULD) self.assertEquals(type(query), type(result)) self.assertEquals(repr(query), repr(result))
def testRemoteService(self): remote = SynchronousRemote(host='localhost', port=self.httpPort, path='/remote') response = remote.executeQuery(parseCql('*')) self.assertEquals(100, response.total)
def testMagicExact(self): exactResult = self.composer.compose(parseCql('animal exact "cats dogs"')) fieldRegistry = FieldRegistry() fieldRegistry.register('animal', StringField.TYPE_NOT_STORED) self.composer = LuceneQueryComposer(unqualifiedTermFields=[("unqualified", 1.0)], luceneSettings=LuceneSettings(fieldRegistry=fieldRegistry)) self.assertConversion(exactResult, 'animal = "cats dogs"')
def testUnsupportedCQL(self): for relation in ['<>']: try: LuceneQueryComposer(unqualifiedTermFields=[("unqualified", 1.0)], luceneSettings=LuceneSettings()).compose(parseCql('index %(relation)s term' % locals())) self.fail() except UnsupportedCQL: pass
def testRemoteServiceOnBadPath(self): remote = SynchronousRemote(host='localhost', port=self.httpPort, path='/does/not/exist') self.assertRaises(IOError, lambda: remote.executeQuery(parseCql('*')))
def _makeExpression(self, input): return cqlToExpression(parseCql(input)) if isinstance(input, basestring) else input
def testRemoteServiceWithBadCore(self): remote = SynchronousRemote(host='localhost', port=self.httpPort, path='/remote') self.assertRaises(IOError, lambda: remote.executeQuery(parseCql('*'), core='doesnotexist'))
def testSimpleExpression(self): expression = cqlToExpression(parseCql('field=value')) self.assertEquals("field", expression.index) self.assertEquals("=", expression.relation) self.assertEquals("value", expression.term)
def testError(self): wq = WebQuery('\'"a') self.assertCql(parseCql('"\'" AND "a"'), wq.ast) wq = WebQuery('<?xml version="1.0" encoding="ISO-8859-1"?>\n<!DOCTYPE foo [\n<!ELEMENT foo ANY >\n<!ENTITY xxe SYSTEM "file:///etc/passwd" >]><foo>&xxe;</foo>') self.assertCql(parseCql('"<?xml version=\\"1.0\\" encoding=\\"ISO-8859-1\\"?>\n<!DOCTYPE foo [\n<!ELEMENT foo ANY >\n<!ENTITY xxe SYSTEM \\"file:///etc/passwd\\" >]><foo>&xxe;</foo>"'), wq.ast)