def search(): """ Takes url params as arguments. One of the arguments must be named "query_string". This argument is the actual query entered into the search bar. The rest of the arguments are optional and can be named anything. These arguments must contain single item json, i.e. {term:filter} where term is the name of the field to be filtered, and filter is the required string for that field. For example, /api/v1/search?query_string=hardware%20java%20ibm&role=programmer&firstName=tanner&results_per_page=5&page=7 will search for all users that have the words hardware, java, and ibm anywhere in their information, and it will then filter those results to only results that have the role set to "programmer" and the firstName set to "tanner". Setting the results_per_page and page behaves as you would expect it to. Indexing for pagination starts at 0. Results are returned in the form of a list of json results. """ error = None list_filters = [] try: query_string = request.args.get('query_string') results_per_page = request.args.get('results_per_page') page = request.args.get('page') keyed_queries = {} for constraint, value in request.args.items(): if constraint == 'query_string' or constraint == 'results_per_page' or constraint == 'page': continue keyed_queries[constraint] = value.lower() if query_string is not None: query_string = query_string.lower() if results_per_page is not None: results_per_page = int(results_per_page) if page is not None: page = int(page) if keyed_queries: results = search_functions.filtered_search_users(query_string, [{'term':keyed_queries}], results_per_page, page) else: #call basic search results = search_functions.simple_search_users(query_string, results_per_page, page) except Exception as e: return jsonify(error=str(e)), HTTP_400_BAD_REQUEST ## now turn the queries into basic users user_ids = map(ObjectId, (entry['_id'] for entry in results.data)) #TODO: Why can't we inject endorsements into the objects returned from elastic? basic_users = user.get_basic_info_from_ids(user_ids, keep_order=True) endorsement.populate_counts(basic_users) return dumps({'results': basic_users, 'metadata': results.metadata, 'error': None})
def search_startups(): """ Example Usage: GET http://localhost:5000/api/v1/search/startups?rank=trending&results_per_page=10&page=2 This will return the 10 results on the 2nd page (indexing starts at 0), ordered by number of endorsements. To full text search just omit the ranking parameter, and pass a query string. For example, use GET http://localhost:5000/api/v1/search/startups?query_string=some text to search for&results_per_page=10&page=2 """ query_string = request.args.get('query_string') results_per_page = request.args.get('results_per_page') page = request.args.get('page') rank = request.args.get('rank') try: if query_string is not None: query_string = query_string.lower() if results_per_page is not None: results_per_page = int(results_per_page) if page is not None: page = int(page) if rank == 'trending': #get results from mongo for now results = endorsement.get_entities_by_num_endorsements( 'startup', page, results_per_page) return dumps({'results': results, 'metadata': None, 'error': None}) else: results = startup.simple_search(query_string, results_per_page, page) startup_ids = map(ObjectId, (entry['_id'] for entry in results.data)) #TODO: Why can't we inject endorsements into the objects returned from elastic? basic_startups = map(startup.get_basic_startup_by_id, startup_ids) endorsement.populate_counts(basic_startups) return dumps({ 'results': basic_startups, 'metadata': results.metadata, 'error': None }) except Exception as e: return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
def search_startups(): """ Example Usage: GET http://localhost:5000/api/v1/search/startups?rank=trending&results_per_page=10&page=2 This will return the 10 results on the 2nd page (indexing starts at 0), ordered by number of endorsements. To full text search just omit the ranking parameter, and pass a query string. For example, use GET http://localhost:5000/api/v1/search/startups?query_string=some text to search for&results_per_page=10&page=2 """ query_string = request.args.get('query_string') results_per_page = request.args.get('results_per_page') page = request.args.get('page') rank = request.args.get('rank') try: if query_string is not None: query_string = query_string.lower() if results_per_page is not None: results_per_page = int(results_per_page) if page is not None: page = int(page) if rank=='trending': #get results from mongo for now results = endorsement.get_entities_by_num_endorsements('startup', page, results_per_page) return dumps({'results': results, 'metadata': None, 'error': None}) else: results = startup.simple_search(query_string, results_per_page, page) startup_ids = map(ObjectId, (entry['_id'] for entry in results.data)) #TODO: Why can't we inject endorsements into the objects returned from elastic? basic_startups = map(startup.get_basic_startup_by_id, startup_ids) endorsement.populate_counts(basic_startups) return dumps({'results': basic_startups, 'metadata': results.metadata, 'error': None}) except Exception as e: return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
def search(): """ Takes url params as arguments. One of the arguments must be named "query_string". This argument is the actual query entered into the search bar. The rest of the arguments are optional and can be named anything. These arguments must contain single item json, i.e. {term:filter} where term is the name of the field to be filtered, and filter is the required string for that field. For example, /api/v1/search?query_string=hardware%20java%20ibm&role=programmer&firstName=tanner&results_per_page=5&page=7 will search for all users that have the words hardware, java, and ibm anywhere in their information, and it will then filter those results to only results that have the role set to "programmer" and the firstName set to "tanner". Setting the results_per_page and page behaves as you would expect it to. Indexing for pagination starts at 0. Results are returned in the form of a list of json results. """ error = None list_filters = [] try: query_string = request.args.get('query_string') results_per_page = request.args.get('results_per_page') page = request.args.get('page') keyed_queries = {} for constraint, value in request.args.items(): if constraint == 'query_string' or constraint == 'results_per_page' or constraint == 'page': continue keyed_queries[constraint] = value.lower() if query_string is not None: query_string = query_string.lower() if results_per_page is not None: results_per_page = int(results_per_page) if page is not None: page = int(page) if keyed_queries: results = search_functions.filtered_search_users( query_string, [{ 'term': keyed_queries }], results_per_page, page) else: #call basic search results = search_functions.simple_search_users( query_string, results_per_page, page) except Exception as e: return jsonify(error=str(e)), HTTP_400_BAD_REQUEST ## now turn the queries into basic users user_ids = map(ObjectId, (entry['_id'] for entry in results.data)) #TODO: Why can't we inject endorsements into the objects returned from elastic? basic_users = user.get_basic_info_from_ids(user_ids, keep_order=True) endorsement.populate_counts(basic_users) return dumps({ 'results': basic_users, 'metadata': results.metadata, 'error': None })