def sph_buildExcerpts(docs, index, words, opts=None): docs = docs words = words index = index if opts: opts = opts else: opts = { 'before_match': '<em>', 'after_match': '</em>', 'chunk_separator': ' ... ', 'limit': 400, 'around': 15 } cl = sph.SphinxClient() cl.SetServer(host, port) res = cl.BuildExcerpts(docs, index, words, opts) error = None if not res: error = cl.GetLastError() return False, error else: return True, [r.decode("utf-8") for r in res]
def view_recipes(self, request): index_updated = int(os.stat(INDEX_FILE).st_mtime) if request.args.get('index_updated') != str(index_updated): return Response( status=302, headers={ 'Location': '/recipes?' + urlencode([ ('index_updated', index_updated) ] + [(k, v) for k, v in url_decode(request.query_string, cls=iter) if k != 'index_updated'], doseq=True), }) ingredients = [ s for s in request.args.getlist('ingredient') if s.strip() ] cocktails = [] if ingredients: try: offset = int(request.args['offset']) except (ValueError, KeyError): offset = 0 sphinx = sphinxapi.SphinxClient() sphinx.SetServer(SPHINX_HOST, SPHINX_PORT) sphinx.Open() try: result = self.query(sphinx, ingredients, offset) finally: sphinx.Close() if result is None: raise InternalServerError(sphinx.GetLastError()) for group in result: recipes = [] for match in group: recipes.append({ 'title': match['attrs']['title'], 'ingredients': match['attrs']['ingredients_text'].splitlines(), 'url': match['attrs']['url'], 'picture_url': match['attrs']['picture'], 'source': match['attrs']['source'], }) cocktails.append({'recipes': recipes}) return Response(json.dumps({ 'cocktails': cocktails, 'index_updated': index_updated, }), headers={'Cache-Control': 'public, max-age=31536000'}, mimetype='application/json')
def search(self, query, dimension=None, locale=None): """Peform search using Sphinx. If `dimension` is set then only the one dimension will be searched.""" self.logger.debug("search in '%s' for '%s', locale '%s'" % (str(dimension), query, locale)) locale_tag = get_locale_tag(locale, self.locales) sphinx = sphinxapi.SphinxClient() if self.host: sphinx.SetServer(self.host, self.port) if dimension: tag = self._dimension_tag(dimension) if tag is None: raise Exception("No dimension %s" % dimension) sphinx.SetFilter("dimension_tag", [tag]) if locale_tag is not None: sphinx.SetFilter("locale_tag", [locale_tag]) # FIXME: Quick hack for Matej Kurian sphinx.SetLimits(0, 1000) index_name = self.browser.cube.name sphinx.SetSortMode(sphinxapi.SPH_SORT_ATTR_ASC, "attribute_value") results = sphinx.Query(query, index=str(index_name)) error = sphinx.GetLastError() if error: self.logger.error("sphinx error: %s" % error) warning = sphinx.GetLastWarning() if warning: self.logger.warning("sphinx warning: %s" % warning) result = SphinxSearchResult(self.browser) if not results: return result result.total_found = results["total_found"] grouped = collections.OrderedDict() result.matches = [match["attrs"] for match in results["matches"]] result.error = sphinx.GetLastError() result.warning = sphinx.GetLastWarning() return result
def open_tickets_for_url(url): global sphinx_server global sphinx_port # Sphinx client = sphinxapi.SphinxClient() client.SetServer(sphinx_server, sphinx_port) client.SetMatchMode(2) q = "\"%s\"" % url res = 0 tickets = [] result = client.Query(q) for match in result['matches']: res = is_ticket_open(match['id']) return res
def search(keyword): import sphinxapi cl = sphinxapi.SphinxClient() cl.SetServer('10.10.64.15',9312) #cl.SetConnectTimeout(3) cl.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED) cl.SetLimits(0,100) res = cl.Query(keyword,'*') if not res: return [] if res.has_key('matches'): return [match["id"] for match in res['matches']] return []
def __init__(self): self.sphinx = sphinx.SphinxClient() if os.environ.get('DJANGO_ENVIRONMENT') == 'test': self.sphinx.SetServer(settings.SPHINX_HOST, settings.TEST_SPHINX_PORT) else: # pragma: nocover self.sphinx.SetServer(settings.SPHINX_HOST, settings.SPHINX_PORT) self.index = 'opinions' self.meta = {} self.queries = {} self.query_index = 0 self.meta_filters = {} self.total_found = 0
def search(keyword): #import logging #log = logging.getLogger('XieYin.app') #log.debug('enter corseek') cl = sphinxapi.SphinxClient() cl.SetServer('10.10.64.15',9312) #cl.SetConnectTimeout(3) cl.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED) cl.SetLimits(0,100) res = cl.Query(keyword,'*') if not res: return [] if res.has_key('matches'): return [match["id"] for match in res['matches']] return []
def runStatus(): s = status() if s != 'start': return mw.returnJson(False, '没有启动程序') sys.path.append(getPluginDir() + "/class") import sphinxapi sh = sphinxapi.SphinxClient() port = getPort() sh.SetServer('127.0.0.1', port) info_status = sh.Status() rData = {} for x in range(len(info_status)): rData[info_status[x][0]] = info_status[x][1] return mw.returnJson(True, 'ok', rData)
def sphinxsearch(server, port, url, query): if not sphinx: return None cached = _cache_get(query, 'sphinx') if cached is not None: return cached client = sphinxapi.SphinxClient() client.SetServer(server, port) client.SetMatchMode(2) client.SetConnectTimeout(5.0) result = [] res = client.Query(query) if res.get("matches") is not None: for ticket in res["matches"]: ticket_id = ticket["id"] ticket_link = url + str(ticket_id) result.append(ticket_link) _cache_set(query, result, 'sphinx') return result
def search_dis(lat, lon, kw='', radius=10000.): ''' search from NewEventTable by the circle whose center is ('lat', 'lon'), half radius is 'radius' lat & lon: float; unit:radians radius: float; unit:meter ''' cl = sphinxapi.SphinxClient() cl.SetServer('10.10.43.180', 9313) cl.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED) #cl.SetMatchMode(sphinxapi.SPH_MATCH_FULLSCAN) cl.SetGeoAnchor('lat', 'lon', lat, lon) cl.SetSortMode(sphinxapi.SPH_SORT_EXTENDED, '@geodist asc') cl.SetFilterFloatRange('@geodist', 0.0, radius) cl.SetLimits(0, 100) res = cl.Query(kw, '*') if res.has_key('matches'): return [match["id"] for match in res['matches']] else: return []
def sphinx_search(query, offset, count): client = sphinxapi.SphinxClient() client.SetServer('localhost', 9312) client.SetWeights([100, 1]) client.SetSortMode(sphinxapi.SPH_SORT_EXTENDED, 'date DESC, channel ASC, @id DESC') client.SetLimits(offset, count, 100000) client.SetRankingMode(sphinxapi.SPH_RANK_PROXIMITY_BM25) client.SetMatchMode(sphinxapi.SPH_MATCH_BOOLEAN) result = client.Query(query, '*') if result and 'matches' in result: messages = [] for msg in result['matches']: attrs = msg['attrs'] channel = attrs['channel'] t = time.localtime(attrs['time']) d = str(attrs['date']) date = datetime.datetime.strptime(d, '%Y%m%d').date() key = (channel, date) messages.append((key, { 'type': 'privmsg', 'channel': channel, 'no': attrs['no'], 'time': datetime.datetime(*t[:7]), 'nick': attrs['nick'].decode('utf-8'), 'text': attrs['content'].decode('utf-8'), 'is_bot': attrs['bot'], })) m = [] for k, v in itertools.groupby(messages, lambda x: x[0]): channel, date = k m.append((Log(channel, date), list(i[1] for i in v))) return { 'total': result['total'], 'messages': m, }
def __init__(self): self.sphinx = sphinx.SphinxClient() if os.environ.get('DJANGO_ENVIRONMENT') == 'test': self.sphinx.SetServer(settings.SPHINX_HOST, settings.TEST_SPHINX_PORT) else: self.sphinx.SetServer(settings.SPHINX_HOST, settings.SPHINX_PORT) self.weight_field = ('@weight + IF(addon_status=%d, 3500, 0) + ' 'IF(locale_ord=%d, 29, 0) + ' 'sqrt(weeklydownloads) * 0.4 ' 'AS myweight ' % (amo.STATUS_PUBLIC, get_locale_ord())) # Store meta data about our queries: self.meta = {} self.queries = {} self.query_index = 0 self.meta_filters = {} # TODO(davedash): make this less arbitrary # Unique ID used for logging self.id = int(random.random() * 10**5)
rt_url = cfg.rt_url rt_user = cfg.rt_user rt_pass = cfg.rt_pass sphinx_server = cfg.sphinx_server sphinx_port = cfg.sphinx_port excludelist = cfg.known_good_excludelist debug = False # RT logger = logging.getLogger('rt') tracker = rt.Rt(rt_url, rt_user, rt_pass, verify_cert=False) tracker.login() # Sphinx client = sphinxapi.SphinxClient() client.SetServer(sphinx_server, sphinx_port) client.SetMatchMode(2) def is_ticket_open(id): status = False try: rt_response = tracker.get_ticket(id) ticket_status = rt_response['Status'] if ticket_status == "open" or ticket_status == "new": status = id except Exception: return False return status
import tkinter as tk import sphinxapi as spx import math res_d = None match_str = None n = 1 sp = spx.SphinxClient() sp.SetServer('localhost', 9312) class ResultBox(tk.Frame): def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.title_text = tk.Text(self, fg='blue', border=0, height=1, font=("DefaultFont", 14)) self.content_text = tk.Text(self, border=0, wrap=tk.WORD, height=5, font=("DefaultFont", 12)) self.book_text = tk.Text(self, border=0, height=1, width=15, font=("DefaultFont", 12)) self.author_text = tk.Text(self, border=0,
def sph_search(query, index="*", offset=0, limit=20): q = query index = index mode = sph.SPH_MATCH_ALL filtercol = 'group_id' filtervals = [] sortby = '' groupby = '' groupsort = '@group desc' offset = offset limit = limit # Start sphinx cl = sph.SphinxClient() cl.SetServer(host, port) #cl.SetFieldWeights({"subject": 500, "message": 100}) cl.SetMatchMode(mode) if filtervals: cl.SetFilter(filtercol, filtervals) if groupby: cl.SetGroupBy(groupby, sph.SPH_GROUPBY_ATTR, groupsort) if sortby: cl.SetSortMode(sph.SPH_SORT_EXTENDED, sortby) if limit: cl.SetLimits(offset, limit, max(limit, 1000)) res = cl.Query(q, index) # Format the result error = "" warning = "" ret_format = {} if not res: error = cl.GetLastError() return False, error if cl.GetLastWarning(): warning = cl.GetLastWarning() ret_format["query"] = q ret_format["total"] = res['total'] ret_format["total_found"] = res['total_found'] ret_format["time"] = res['time'] #print 'Query stats:' if res.has_key('words'): qstatus = {} #for info in res['words']: #print '\t\'%s\' found %d times in %d documents' % (info['word'], info['hits'], info['docs']) ret_format["words"] = res["words"] if res.has_key('matches'): matches = [] n = offset + 1 #print '\nMatches:' for match in res['matches']: #attrsdump = '' attrs_list = [] match_each = {} for attr in res['attrs']: attr_map = {} attrname = attr[0] attrtype = attr[1] value = match['attrs'][attrname] if attrtype == sph.SPH_ATTR_TIMESTAMP: value = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(value)) #attrsdump = '%s, %s=%s' % ( attrsdump, attrname, value ) attr_map["name"] = attrname attr_map["value"] = value attrs_list.append(attr_map) #print '%d. docid=%s, weight=%d%s' % (n, match['id'], match['weight'], attrsdump) match_each["id"] = n match_each["docid"] = match["id"] match_each["weight"] = match["weight"] match_each["attrs"] = attrs_list matches.append(match_each) n += 1 ret_format["matches"] = matches ret_format["error"] = error ret_format["warning"] = warning return True, ret_format
def connect(self): client = sphinxsearch.SphinxClient() client.SetServer(current_app.config['SPHINX_HOST'], current_app.config['SPHINX_PORT']) return client
import datetime import logging import sphinxapi from django.conf import settings from django.http import JsonResponse from documents.models import CodeOKVED, CodeOKS from documents.models import Documents from documents.serializers import DocumentsSerializer from search.models import Search, AutoCompletion, SearchHistory from search.serializers import SearchOptionsSerializer LOGGER = logging.getLogger('django') if settings.SEARCH_ENGINE.get('name') == 'sphinx': CLIENT = sphinxapi.SphinxClient() CLIENT.SetServer(settings.SEARCH_ENGINE.get('HOST'), settings.SEARCH_ENGINE.get('PORT')) print(f"client = {CLIENT.Query('акустика')}") # def timing(f): # def wrap(*args): # time1 = time.time() # ret = f(*args) # time2 = time.time() # print('{:s} время выполнения {:.3f} ms'.format(f.__name__, (time2 - time1) * 1000.0)) # # return ret # # return wrap