def elastic(request): """Set up a elasticsearch connection reset and ready to roll. This will attempt to connect to the default elasticsearch instance on http://localhost:9200. Its not configurable yet. """ from nozama.cloudsearch.data.db import init_es, get_es init_es(dict(es_namespace="ut_")) get_es().hard_reset()
def remove_all(): """Remove all store documents. """ log = get_log("remove_all") conn = db().conn() conn.documents.drop() conn.documents_removed.drop() log.warn("all documents have been removed.") # Remove al of the documents from elasticsearch as well. get_es().hard_reset() log.warn("All indexes removed from elasticsearch.")
def remove_all(): """Remove all store documents. """ log = get_log('remove_all') conn = db().conn() conn.documents.drop() conn.documents_removed.drop() log.warn("all documents have been removed.") # Remove al of the documents from elasticsearch as well. get_es().hard_reset() log.warn("All indexes removed from elasticsearch.")
def elastic(logger, request): """Set up a elasticsearch connection reset and ready to roll. This will attempt to connect to the default elasticsearch instance on http://localhost:9200. Its not configurable yet. """ from nozama.cloudsearch.data import db from nozama.cloudsearch.service import environ_settings cfg = dict(es_endpoint="http://{}:{}".format( environ_settings.ELASTICSEARCH_HOST(), environ_settings.ELASTICSEARCH_PORT())) logger.debug("ElasticSearch config<{0}>".format(cfg)) db.init_es(cfg) db.get_es().hard_reset()
def search(query={}): """Perform a search across text fields. :returns: A dict compatible with an Amazon CloudSearch response. """ log = get_log('search') es = get_es() qstring = query.get('q', '') log.debug("searching query '{0}'".format(query)) formatType = query.get('format', '') # try: if qstring: query = {"query": {"query_string": {"query": u"{0}*".format(qstring)}}} results = es.conn.search(index=es.index, body=query) else: query = {"query": {"match_all": {}}} results = es.conn.search(index=es.index, body=query) # except ElasticHttpNotFoundError: # # No documents present in store. Don't worry about it there's nothing # # to search # results = dict( # hits=dict(hits=[], total=0), # took=0, # ) hit = [] conn = db().conn() for i in results['hits']['hits']: query = dict(_id=i['_id']) fields = conn.documents.find_one(query)['fields'] if formatType == u'sdk': for key, value in fields.items(): if not isinstance(value, list): fields[key] = [value] hit.append({'id': i['_id'], 'fields': fields}) rc = { "rank": "-text_relevance", "match-expr": u"(label '{0}')".format(qstring), "hits": { "found": results['hits']['total']['value'], "start": 0, "hit": hit }, "info": { "rid": binascii.hexlify(os.urandom(40)).decode(), "time-ms": results['took'], "cpu-time-ms": 0 } } log.debug("found '{0}'".format(rc)) return rc
def remove_from_elasticsearch(doc): """Remove this document from the index. """ log = get_log("remove_from_elasticsearch") es = get_es() log.debug("remove doc <{0}>".format(doc["id"])) result = es.conn.delete(es.index, es.doc_type, id=doc["_id"]) es.conn.refresh(es.index) log.debug("doc <{0}> remove result: {1}".format(doc["id"], result))
def remove_from_elasticsearch(doc): """Remove this document from the index. """ log = get_log('remove_from_elasticsearch') es = get_es() log.debug("remove doc <{0}>".format(doc['id'])) result = es.conn.delete(es.index, doc['id']) es.conn.indices.refresh(index=es.index) log.debug("doc <{0}> remove result: {1}".format(doc['id'], result))
def add_to_elasticsearch(doc): """This indexes the fields and puts them into cloud search for later searching. """ log = get_log("add_to_elasticsearch") es = get_es() log.debug("adding doc <{0}>".format(doc["id"])) result = es.conn.index(es.index, es.doc_type, doc["fields"], id=doc["_id"]) es.conn.refresh(es.index) log.debug("doc <{0}> add result: {1}".format(doc["id"], result))
def add_to_elasticsearch(doc): """This indexes the fields and puts them into cloud search for later searching. """ log = get_log('add_to_elasticsearch') es = get_es() log.debug("adding doc <{0}>".format(doc['id'])) result = es.conn.index(es.index, es.doc_type, doc['fields'], id=doc['_id']) es.conn.refresh(es.index) log.debug("doc <{0}> add result: {1}".format(doc['id'], result))
def search(query={}): """Perform a search across text fields. :returns: A dict compatible with an Amazon CloudSearch response. """ log = get_log("search") es = get_es() qstring = query.get("q", "") log.debug("searching query '{0}'".format(query)) try: if qstring: query = {"query": {"query_string": {"query": "{0}*".format(qstring)}}} results = es.conn.search(query, index=es.index) else: query = {"query": {"match_all": {}}} results = es.conn.search(query, index=es.index) except ElasticHttpNotFoundError: # No documents present in store. Don't worry about it there's nothing # to search results = dict(hits=dict(hits=[], total=0), took=0) rc = { "rank": "-text_relevance", "match-expr": "(label '{0}')".format(qstring), "hits": { "found": results["hits"]["total"], "start": 0, "hit": [{"id": i["_id"]} for i in results["hits"]["hits"]], }, "info": {"rid": os.urandom(40).encode("hex"), "time-ms": results["took"], "cpu-time-ms": 0}, } log.debug("found '{0}'".format(rc)) return rc
def search(query={}): """Perform a search across text fields. :returns: A dict compatible with an Amazon CloudSearch response. """ log = get_log('search') es = get_es() qstring = query.get('q', '') log.debug("searching query '{0}'".format(query)) formatType = query.get('format', '') try: if qstring: query = { "query": { "query_string": { "query": u"{0}*".format(qstring) } } } results = es.conn.search(query, index=es.index) else: query = {"query": {"match_all": {}}} results = es.conn.search(query, index=es.index) except ElasticHttpNotFoundError: # No documents present in store. Don't worry about it there's nothing # to search results = dict( hits=dict(hits=[], total=0), took=0, ) hit = [] conn = db().conn() for i in results['hits']['hits']: query = dict(_id=i['_id']) fields = conn.documents.find_one(query)['fields'] if formatType == u'sdk': for key, value in fields.items(): if not isinstance(value, list): fields[key] = [value] hit.append({'id': i['_id'], 'fields': fields}) rc = { "rank": "-text_relevance", "match-expr": u"(label '{0}')".format(qstring), "hits": { "found": results['hits']['total'], "start": 0, "hit": hit }, "info": { "rid": os.urandom(40).encode('hex'), "time-ms": results['took'], "cpu-time-ms": 0 } } log.debug("found '{0}'".format(rc)) return rc