Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 def testAcceptsCqlAstAndQueryExpression(self):
     a = cqlToExpression('field = value')
     b = cqlToExpression(parseCql('field = value'))
     c = cqlToExpression(b)
     self.assertEqual(a, b)
     self.assertEqual(a, c)
Exemplo n.º 9
0
 def testSimpleExpression(self):
     expression = cqlToExpression(parseCql('field=value'))
     self.assertEqual("field", expression.index)
     self.assertEqual("=", expression.relation)
     self.assertEqual("value", expression.term)
Exemplo n.º 10
0
 def testAcceptsCqlAstAndQueryExpression(self):
     a = cqlToExpression('field = value')
     b = cqlToExpression(parseCql('field = value'))
     c = cqlToExpression(b)
     self.assertEquals(a, b)
     self.assertEquals(a, c)
Exemplo n.º 11
0
 def testFilterWithSpaces(self):
     wq = WebQuery('fiets')
     wq.addFilter('field', 'value with spaces')
     self.assertCql(parseCql('(fiets) AND field exact "value with spaces"'), wq.ast)
Exemplo n.º 12
0
 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))
Exemplo n.º 13
0
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)
Exemplo n.º 15
0
def luceneQueryFromCql(cqlString):
    return LuceneQueryComposer([], luceneSettings=LuceneSettings()).compose(parseCql(cqlString))
Exemplo n.º 16
0
 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)
Exemplo n.º 17
0
 def testFilterWithSpaces(self):
     wq = WebQuery('fiets')
     wq.addFilter('field', 'value with spaces')
     self.assertCql(parseCql('(fiets) AND field exact "value with spaces"'),
                    wq.ast)
Exemplo n.º 18
0
 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
Exemplo n.º 21
0
    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))
Exemplo n.º 22
0
 def testRemoteService(self):
     remote = SynchronousRemote(host='localhost',
                                port=self.httpPort,
                                path='/remote')
     response = remote.executeQuery(parseCql('*'))
     self.assertEquals(100, response.total)
Exemplo n.º 23
0
 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"')
Exemplo n.º 24
0
 def testFilter(self):
     wq = WebQuery('fiets')
     wq.addFilter('field', 'value')
     self.assertCql(parseCql('(fiets) AND field exact value'), wq.ast)
Exemplo n.º 25
0
 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'))
Exemplo n.º 29
0
 def testSimpleExpression(self):
     expression = cqlToExpression(parseCql('field=value'))
     self.assertEquals("field", expression.index)
     self.assertEquals("=", expression.relation)
     self.assertEquals("value", expression.term)
Exemplo n.º 30
0
 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)
Exemplo n.º 31
0
 def testRemoteServiceOnBadPath(self):
     remote = SynchronousRemote(host='localhost',
                                port=self.httpPort,
                                path='/does/not/exist')
     self.assertRaises(IOError, lambda: remote.executeQuery(parseCql('*')))
 def testRemoteService(self):
     remote = SynchronousRemote(host='localhost', port=self.httpPort, path='/remote')
     response = remote.executeQuery(parseCql('*'))
     self.assertEquals(100, response.total)