Exemplo n.º 1
0
 def test_with_raw_attr(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').select(RawAttr('@weight*10', 'skey'))
     self.assertEqual(
         search.lex(),
         "SELECT @weight*10 AS skey FROM company WHERE MATCH('Yandex')"
     )
Exemplo n.º 2
0
 def test_with_options(self):
     search = Search(['company'], config=SearchConfig)
     search = (
         search
         .match('Yandex')
         .select('id', 'name')
         .options(
             ranker='proximity',
             max_matches=100,
             field_weights={'name': 100},
         )
         .order_by('name', 'desc')
     )
     correct_options_qls = [
         "max_matches=100, ranker=proximity, field_weights=(name=100)",
         "max_matches=100, field_weights=(name=100), ranker=proximity",
         "ranker=proximity, field_weights=(name=100), max_matches=100",
         "ranker=proximity, max_matches=100, field_weights=(name=100)",
         "field_weights=(name=100), max_matches=100, ranker=proximity",
         "field_weights=(name=100), ranker=proximity, max_matches=100",
     ]
     correct_qls = [
         " ".join((
             "SELECT id, name FROM company WHERE MATCH('Yandex') ORDER BY name DESC OPTION",
             opt
         ))
         for opt in correct_options_qls
     ]
     self.assertIn(search.lex(), correct_qls)
Exemplo n.º 3
0
 def test_simple(self):
     search = Search(indexes=['company'], config=SearchConfig)
     search = search.match('Yandex')
     self.assertEqual(
         search.lex(),
         "SELECT * FROM company WHERE MATCH('Yandex')"
     )
Exemplo n.º 4
0
 def test_with_params(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').limit(0, 100).order_by('name', 'desc')
     self.assertEqual(
         search.lex(),
         "SELECT * FROM company WHERE MATCH('Yandex') ORDER BY name DESC LIMIT 0,100"
     )
Exemplo n.º 5
0
 def test_with_grouping(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').select(Count()).group_by('date_created')
     self.assertEqual(
         search.lex(),
         "SELECT COUNT(*) AS num FROM company WHERE MATCH('Yandex') GROUP BY date_created"
     )
Exemplo n.º 6
0
 def test_with_double_match(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('ОАО').match('ТНК')
     self.assertEqual(
         search.lex(),
         "SELECT * FROM company WHERE MATCH('ОАО ТНК')"
     )
Exemplo n.º 7
0
 def test_update_syntax(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').update(products=(5,2)).filter(id__gt=1)
     self.assertEqual(
         search.lex(),
         "UPDATE company SET products=(5,2) WHERE MATCH('Yandex') AND id>1"
     )
Exemplo n.º 8
0
def json_search(request):
    search_query = Search(indexes=['rt_main'], config=SphinxitConfig)
    keyword = request.GET['keyword']
    start = request.GET.get('start', 0)
    count = request.GET.get('count', 10)
    sort = request.GET.get('sort', '')
    category = request.GET.get('category', '')
    if request.GET.get('base64') == '1':
        keyword = keyword.decode('base64').decode('utf8')

    mckey = str(
        binascii.crc32(
            (u'%s%s%s%s%s' %
             (keyword, start, count, sort, category)).encode('utf8'))
        & 0xFFFFFFFFL)
    cache = mc.get(mckey)
    if cache:
        print 'bingo', keyword.encode('utf8'), mckey
        return HttpResponse(cache)

    q = search_query.match(keyword)
    if category:
        q = q.filter(category__eq=binascii.crc32(category) & 0xFFFFFFFFL)
    if sort == 'create_time': q = q.order_by('create_time', 'desc')
    if sort == 'length': q = q.order_by('length', 'desc')
    q = q.limit(start, count)
    q2 = search_query.match(keyword).select(
        'category', Count()).group_by('category').named('cats')
    res = q.ask(subqueries=[q2])

    jsp = JsonResponse(res)
    mc.set(mckey, jsp.content)
    return jsp
Exemplo n.º 9
0
 def test_with_or_filters_and_fields(self):
     correct_qls = [
         "SELECT id, (id>=100 OR id=1) AS cnd FROM company WHERE MATCH('Yandex') AND cnd>0",
         "SELECT id, (id=1 OR id>=0) AS cnd FROM company WHERE MATCH('Yandex') AND cnd>0",
     ]
     search = Search(['company'], config=SearchConfig).select('id')
     search = search.match('Yandex').filter(OR(id__gte=100, id__eq=1))
     self.assertIn(search.lex(), correct_qls)
Exemplo n.º 10
0
 def test_with_select(self):
     search = Search(indexes=['company'], config=SearchConfig)
     search = search.select('id', 'date_created')
     search = search.match('Yandex')
     self.assertEqual(
         search.lex(),
         "SELECT id, date_created FROM company WHERE MATCH('Yandex')"
     )
Exemplo n.º 11
0
 def search(query):
     search = Search(indexes=['events'], config=SearchConfig)
     search = search.match(query)
     # sphinxit uses set - an unordered data structure - for storing query order params
     search = search.limit(0, 50).order_by('@weight DESC, event_date',
                                           'DESC')
     result = search.ask()
     return result
Exemplo n.º 12
0
 def test_with_time_filter(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').filter(date_created__lte=datetime.date.today())
     today = datetime.date.today()
     sxql = (
         "SELECT * FROM company WHERE MATCH('Yandex') "
         "AND date_created<=%s" % unix_timestamp(today)
     )
     self.assertEqual(search.lex(), sxql)
Exemplo n.º 13
0
 def test_with_multiindex(self):
     search = Search(
         indexes=['company', 'company_delta'],
         config=SearchConfig
     )
     search = search.filter(id__gte=100)
     self.assertEqual(
         search.lex(),
         "SELECT * FROM company, company_delta WHERE id>=100"
     )
Exemplo n.º 14
0
 def search(self, keyword, start=0, count=10, category=None, sort=None):
     search_query = Search(indexes=['rt_main'], config=SphinxitConfig)
     q = search_query.match(keyword)
     if category: q = q.filter(category__eq=binascii.crc32(category)&0xFFFFFFFFL)
     if sort == 'create_time': q = q.order_by('create_time', 'desc')
     if sort == 'length': q = q.order_by('length', 'desc')
     q = q.limit(start, count)
     q2 = search_query.match(keyword).select('category', Count()).group_by('category').named('cats')
     res = q.ask(subqueries=[q2])
     return res
Exemplo n.º 15
0
 def search(query, eventType_id=None, contract_id=None, speciality_id=None):
     search = Search(indexes=['event_service'], config=SearchConfig)
     search = search.match(query)
     if eventType_id:
         search = search.filter(eventType_id__eq=int(eventType_id))
     if contract_id:
         search = search.filter(contract_id__eq=int(contract_id))
     if speciality_id:
         search = search.filter(speciality_id__in=[0, int(speciality_id)])
     search = search.limit(0, 100)
     result = search.ask()
     return result
Exemplo n.º 16
0
 def search(name):
     search = Search(indexes=['patient'], config=SearchConfig)
     search = search.match(name).limit(0, 100)
     search = search.options(field_weights={'code': 100,
                                            'lastName': 90,
                                            'birthDate_f1': 80,
                                            'birthDate_f2': 80,
                                            'firstName': 70,
                                            'patrName': 60,
                                            'SNILS': 50,
                                            'document': 50,
                                            'policy': 50})
     #fixme: after sphinxit merge https://github.com/semirook/sphinxit/pull/20
     search = search.order_by('@weight desc, lastName asc, firstName asc, patrName', 'asc')
     result = search.ask()
     return result
Exemplo n.º 17
0
def json_search(request):
    search_query = Search(indexes=['rt_main'], config=SphinxitConfig)
    keyword = request.GET['keyword']
    start = request.GET.get('start', 0)
    count = request.GET.get('count', 10)
    if request.GET.get('base64') == '1':
        keyword = keyword.decode('base64').decode('utf8')

    mckey = str(
        binascii.crc32((u'%s%s%s' % (keyword, start, count)).encode('utf8'))
        & 0xFFFFFFFFL)
    cache = mc.get(mckey)
    if cache:
        print 'bingo', keyword.encode('utf8'), mckey
        return HttpResponse(cache)

    q = search_query.match(keyword).limit(start, count).order_by(
        'create_time', 'desc')
    res = q.ask()

    jsp = JsonResponse(res)
    mc.set(mckey, jsp.content)
    return jsp
Exemplo n.º 18
0
 def search(name):
     search = Search(indexes=['person'], config=SearchConfig)
     search = search.match(name).limit(0, 100)
     result = search.ask()
     return result