def get_sphinx_data(sphinx_id): query = Search(indexes=['sourcecode'], config=BaseSearchConfig) query = query.filter(id__eq=sphinx_id) results = query.ask() if len(results['result']['items']) == 0: flask.abort(404) filename = results['result']['items'][0]['path'] if not os.path.isfile(filename): return filename, 'File not found. Please resphinx_id.' code = '' with open(filename) as f: code = f.read() try: # This is the line that throws the exception. lexer = get_lexer_for_filename(filename) formatter = HtmlFormatter(noclasses=True) result = highlight(code, lexer, formatter) except ClassNotFound: # Syntax highlighting not supported.' result = '<pre>{}</pre>'.format(code) url = flask.url_for('display', sphinx_id=sphinx_id) return {'body': result, 'path': filename, 'url': url}
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)
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')" )
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
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" )
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
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" )
def test_simple(self): search = Search(indexes=['company'], config=SearchConfig) search = search.match('Yandex') self.assertEqual( search.lex(), "SELECT * FROM company WHERE MATCH('Yandex')" )
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" )
def test_with_double_match(self): search = Search(['company'], config=SearchConfig) search = search.match('ОАО').match('ТНК') self.assertEqual( search.lex(), "SELECT * FROM company WHERE MATCH('ОАО ТНК')" )
def search(): term = flask.request.args.get('q') query = Search(indexes=['sourcecode'], config=BaseSearchConfig) query = query.match(term) query = query.select('id', 'path') results = query.ask() results = [get_row(result) for result in results['result']['items']] return uncached(json.dumps(results), mimetype='application/json')
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
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)
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')" )
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)
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" )
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
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
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
def get_context_data(self, **kwargs): context = super(SearchView, self).get_context_data(**kwargs) if self.request.GET.get('by') == 'resume' and self.request.GET.get('search'): search = self.request.GET.get('search') search_query = Search(indexes=['resume'], config=ResumeSearch) search_query = search_query.match(search) search_result = search_query.ask() search_results = search_result['result']['items'] results = [] for result in search_results: results.append(Resume.objects.get(id=result['id'])) context['search_results'] = results context['search'] = self.request.GET.get('search') context['url'] = 'resume' elif self.request.GET.get('by') == 'job' and self.request.GET.get('search'): search = self.request.GET.get('search') search_query = Search(indexes=['jobs'], config=JobSearch) search_query = search_query.match(search) search_result = search_query.ask() search_results = search_result['result']['items'] results = [] for result in search_results: results.append(Job.objects.get(id=result['id'])) context['search_results'] = results context['search'] = self.request.GET.get('search') context['url'] = 'job' return context
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
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
from sphinxit.core.processor import Search from sphinxit.core.helpers import BaseSearchConfig class SphinxitConfig(BaseSearchConfig): WITH_STATUS = False search_query = Search(indexes=['cigar'], config=SphinxitConfig) search_query = search_query.match("cohiba") # search_query. rs = search_query.ask() print(rs) s = '' for i in rs['result']['items']: print(i['id'], ",") s += str(i['id']) + "," print(s)
def search(name): search = Search(indexes=['person'], config=SearchConfig) search = search.match(name).limit(0, 100) result = search.ask() return result