def testUnsupportedCQL(self):
     printer=SolrLuceneQueryComposer(unqualifiedTermFields=[("all", 1)])
     ast = parseString("field any term")
     try:
         printer.compose(ast)
         self.fail("must raise UnsupportedCQL")
     except UnsupportedCQL, e:
         self.assertEquals("Only =, ==, exact, <, <=, > and >= are supported.", str(e))
    def testBoost(self):
        self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("__all__", 4.0)])
        self.assertEquals('__all__:"term"^4.0', self.cql2lucene("term"))

        self.assertEquals('field:"term"^3.5', self.cql2lucene("field=/boost=3.5 term"))

        self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("__all__", 4.0), ("__extra__", 2.0), ("__uri__", 1.0)])
        self.assertEquals('(__all__:"term"^4.0 OR __extra__:"term"^2.0 OR __uri__:"term")', self.cql2lucene("term"))

        self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("all", 1)])
        self.assertEquals('field:"term"^2.0', self.cql2lucene("field exact/boost=2 term"))
 def testRangeQuery(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[])
     self.assertEquals('field:{* TO 5}', self.cql2lucene("field<5"))
     self.assertEquals('field:[* TO 5]', self.cql2lucene("field<=5"))
     self.assertEquals('field:[5 TO *]', self.cql2lucene("field>=5"))
     self.assertEquals('field:{5 TO *}', self.cql2lucene("field>5"))
     self.assertEquals('(field:{5 TO *} AND field:[* TO 10])', self.cql2lucene("field>5 AND field<=10"))
class Cql2SolrLuceneQuery(Observable, Logger):
    def __init__(self, unqualifiedFields, name=None):
        Observable.__init__(self, name=name)
        self._cqlComposer = SolrLuceneQueryComposer(unqualifiedFields)

    def executeQuery(self, cqlAbstractSyntaxTree, *args, **kwargs):
        filterQueries = kwargs.get('filterQueries', [])
        filterQueries = [self._convert(fq) for fq in filterQueries]
        response = yield self.any.executeQuery(luceneQueryString=self._convert(cqlAbstractSyntaxTree), *args, **kwargs)
        raise StopIteration(response)

    def _convert(self, ast):
        ClauseCollector(ast, self.log).visit()
        return self._cqlComposer.compose(ast)
 def testOne(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("__all__", 1.0)])
     self.assertEquals('__all__:"term"', self.cql2lucene("term"))
     self.assertEquals('(__all__:"fiets" AND __all__:"auto")', self.cql2lucene("fiets AND auto"))
     self.assertEquals('__all__:"fiets auto"', self.cql2lucene('"fiets auto"'))
     self.assertEquals('__all__:fi*', self.cql2lucene('"fi*"'))
     self.assertEquals('__all__:fi*', self.cql2lucene('fi*'))
     self.assertEquals('__all__:f*ts', self.cql2lucene('"f*ts"'))
     self.assertEquals('__all__:*ts', self.cql2lucene('*ts'))
     self.assertEquals('(__all__:"fiets" AND (__all__:"auto" OR __all__:"boot"))', self.cql2lucene('fiets AND (auto OR boot)'))
     self.assertEquals('((__all__:"fiets" AND __all__:"auto") OR __all__:"boot")', self.cql2lucene('fiets AND auto OR boot'))
     self.assertEquals('(__all__:"fiets" AND (__all__:"auto" OR __all__:"boot"))', self.cql2lucene('fiets AND (auto OR boot)'))
     self.assertEquals('((__all__:"fiets" AND __all__:"auto") OR (__all__:"boot" AND __all__:"other"))', self.cql2lucene('(fiets AND auto) OR (boot AND other)'))
     self.assertEquals('(__all__:"fiets" OR (__all__:"auto" AND __all__:"boot"))', self.cql2lucene('fiets OR auto AND boot'))
 def testSolrKeywords(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[])
     self.assertEquals('field:"NOT"', self.cql2lucene('field="NOT"'))
     self.assertEquals('field:"AND"', self.cql2lucene('field="AND"'))
     self.assertEquals('field:"OR"', self.cql2lucene('field="OR"'))
     # Escaping Special Characters
     # Lucene supports escaping special characters that are part of the query syntax. The current list special characters are
     # + - && || ! ( ) { } [ ] ^ " ~ * ? : \
     # To escape these character use the \ before the character.
     self.assertEquals('field:"-"', self.cql2lucene('field=-'))
     self.assertEquals('field:"+"', self.cql2lucene('field=+'))
     self.assertEquals('field:"!"', self.cql2lucene('field="!"'))
     self.assertEquals('field:"!"', self.cql2lucene('field=!'))
     self.assertEquals('field:"&&"', self.cql2lucene('field="&&"'))
     self.assertEquals(r'field:"\""', self.cql2lucene(r'field="\""'))
     self.assertEquals(r'field:"fiets!"', self.cql2lucene(r'field="fiets!"'))
     self.assertEquals(r'field:"fiets!"', self.cql2lucene(r'field=fiets!'))
Example #7
0
class Cql2SolrLuceneQuery(Observable, Logger):
    def __init__(self, unqualifiedFields, name=None):
        Observable.__init__(self, name=name)
        self._cqlComposer = SolrLuceneQueryComposer(unqualifiedFields)

    def executeQuery(self, cqlAbstractSyntaxTree, *args, **kwargs):
        filterQueries = kwargs.get('filterQueries', [])
        filterQueries = [self._convert(fq) for fq in filterQueries]
        response = yield self.any.executeQuery(
            luceneQueryString=self._convert(cqlAbstractSyntaxTree),
            *args,
            **kwargs)
        raise StopIteration(response)

    def _convert(self, ast):
        ClauseCollector(ast, self.log).visit()
        return self._cqlComposer.compose(ast)
 def __init__(self, unqualifiedFields, name=None):
     Observable.__init__(self, name=name)
     self._cqlComposer = SolrLuceneQueryComposer(unqualifiedFields)
 def testMultipleUnqualifiedTermFields(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("__all__", 1.0), ("__extra__", 1.0)])
     self.assertEquals('(__all__:"term" OR __extra__:"term")', self.cql2lucene("term"))
     self.assertEquals('((__all__:"term" OR __extra__:"term") AND (__all__:"otherterm" OR __extra__:"otherterm"))', self.cql2lucene("term AND otherterm"))
 def testPrefixQuery(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[("__all__", 1.0)])
     self.assertEquals('__all__:term*', self.cql2lucene('term*'))
     self.assertEquals('__all__:term*', self.cql2lucene('TERM*'))
     self.assertEquals('*:*', self.cql2lucene('*'))
 def testEscaping(self):
     self.printer = SolrLuceneQueryComposer(unqualifiedTermFields=[])
     self.assertEquals('field:"term:term1"', self.cql2lucene('field="term:term1"'))
     self.assertEquals('field:"term:term1"', self.cql2lucene('field exact "term:term1"'))
     self.assertEquals('field:"term"', self.cql2lucene('field exact term'))
     self.assertEquals(r'dc\:title:"term"', self.cql2lucene('dc:title exact term'))
Example #12
0
 def __init__(self, unqualifiedFields, name=None):
     Observable.__init__(self, name=name)
     self._cqlComposer = SolrLuceneQueryComposer(unqualifiedFields)