def search_results(request): se = SearchEngineFactory().create() search_results_object = {"query": Query(se)} include_provisional = get_provisional_type(request) permitted_nodegroups = get_permitted_nodegroups(request.user) search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err, status=500) dsl = search_results_object.pop("query", None) dsl.include("graph_id") dsl.include("root_ontology_class") dsl.include("resourceinstanceid") dsl.include("points") dsl.include("geometries") dsl.include("displayname") dsl.include("displaydescription") dsl.include("map_popup") dsl.include("provisional_resource") if request.GET.get("tiles", None) is not None: dsl.include("tiles") results = dsl.search(index="resources") ret = {} if results is not None: # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret["results"] = results for key, value in list(search_results_object.items()): ret[key] = value ret["reviewer"] = request.user.groups.filter( name="Resource Reviewer").exists() ret["timestamp"] = datetime.now() ret["total_results"] = dsl.count(index="resources") return JSONResponse(ret) else: ret = { "message": _("There was an error retrieving the search results") } return JSONResponse(ret, status=500)
def search_results(request): se = SearchEngineFactory().create() search_results_object = {'query': Query(se)} include_provisional = get_provisional_type(request) permitted_nodegroups = get_permitted_nodegroups(request.user) search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [('search-results', '')]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err, status=500) dsl = search_results_object.pop('query', None) dsl.include('graph_id') dsl.include('root_ontology_class') dsl.include('resourceinstanceid') dsl.include('points') dsl.include('geometries') dsl.include('displayname') dsl.include('displaydescription') dsl.include('map_popup') dsl.include('provisional_resource') if request.GET.get('tiles', None) is not None: dsl.include('tiles') results = dsl.search(index='resources') if results is not None: # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [('search-results', '')]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret = {} ret['results'] = results for key, value in list(search_results_object.items()): ret[key] = value ret['reviewer'] = request.user.groups.filter( name='Resource Reviewer').exists() ret['timestamp'] = datetime.now() ret['total_results'] = dsl.count(index='resources') return JSONResponse(ret) else: return HttpResponseNotFound( _("There was an error retrieving the search results"))
def get(self, request, componentname): search_filter_factory = SearchFilterFactory(request) search_filter = search_filter_factory.get_filter(componentname) if search_filter: return JSONResponse(search_filter.view_data()) return JSONResponse(status=404)
def search_results(request): for_export = request.GET.get("export") total = int(request.GET.get("total", "0")) se = SearchEngineFactory().create() search_results_object = {"query": Query(se)} include_provisional = get_provisional_type(request) permitted_nodegroups = get_permitted_nodegroups(request.user) search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONErrorResponse(message=err) dsl = search_results_object.pop("query", None) dsl.include("graph_id") dsl.include("root_ontology_class") dsl.include("resourceinstanceid") dsl.include("points") dsl.include("geometries") dsl.include("displayname") dsl.include("displaydescription") dsl.include("map_popup") dsl.include("provisional_resource") if request.GET.get("tiles", None) is not None: dsl.include("tiles") if for_export is True: results = dsl.search(index="resources", scroll="1m") scroll_id = results["_scroll_id"] if total <= settings.SEARCH_EXPORT_LIMIT: pages = (total // settings.SEARCH_RESULT_LIMIT) + 1 if total > settings.SEARCH_EXPORT_LIMIT: pages = int(settings.SEARCH_EXPORT_LIMIT // settings.SEARCH_RESULT_LIMIT) - 1 for page in range(pages): results_scrolled = dsl.se.es.scroll(scroll_id=scroll_id, scroll="1m") results["hits"]["hits"] += results_scrolled["hits"]["hits"] else: results = dsl.search(index="resources") ret = {} if results is not None: # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret["results"] = results for key, value in list(search_results_object.items()): ret[key] = value ret["reviewer"] = user_is_resource_reviewer(request.user) ret["timestamp"] = datetime.now() ret["total_results"] = dsl.count(index="resources") return JSONResponse(ret) else: ret = { "message": _("There was an error retrieving the search results") } return JSONResponse(ret, status=500)
def search_results(self, request): has_filters = True if len(list( request.GET.items())) > 1 is not None else False request.GET = request.GET.copy() se = SearchEngineFactory().create() search_results_object = {"query": Query(se)} include_provisional = get_provisional_type(request) permitted_nodegroups = self.get_permitted_nodegroups(request.user) # get a list of resourceIds of type Person et al that meet # the search query then use those ids to search for Person instances related to Physical Thing instances request.GET[ "resource-type-filter"] = '[{"graphid":"f71f7b9c-b25b-11e9-901e-a4d18cec433a","name":"Person","inverted":false}]' search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err.message, status=500) dsl = search_results_object.pop("query", None) dsl.exclude("*") results = dsl.search(index="resources", limit=1000) resourceIds = [hit["_id"] for hit in results["hits"]["hits"]] search_results_object = {"query": Query(se)} request.GET[ "resource-type-filter"] = '[{"graphid":"9519cb4f-b25b-11e9-8c7b-a4d18cec433a","name":"Physical Thing","inverted":false}]' search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err.message, status=500) # only search for realted instance references when a filter is applied (aside from the paging filter) if has_filters: # the resource id query can only return resources of type Physical Thing; this is to handle for the scenario # where a Person (or other relatable model added to filter) is related to something other than a Person resource_id_query = Bool() resource_id_query.must( Nested(path="ids", query=Terms(field="ids.id", terms=resourceIds))) resource_id_query.must( Terms(field="graph_id", terms="9519cb4f-b25b-11e9-8c7b-a4d18cec433a")) # we need to wrap the existing query (search_results_object['query']) in a Bool query along with # the resource id query above outer_query = Bool() outer_query.should(resource_id_query) dsl = search_results_object.pop("query", None) outer_query.should(dsl.dsl.pop("query", None)) dsl.dsl["query"] = {} search_results_object["query"] = dsl search_results_object["query"].add_query(outer_query) dsl = search_results_object.pop("query", None) dsl.include("graph_id") dsl.include("root_ontology_class") dsl.include("resourceinstanceid") dsl.include("points") dsl.include("geometries") dsl.include("displayname") dsl.include("displaydescription") dsl.include("map_popup") dsl.include("provisional_resource") dsl.include("domains") # if request.GET.get('tiles', None) is not None: # dsl.include('tiles') dsl.include("tiles") results = dsl.search(index="resources") # print JSONSerializer().serialize(dsl.dsl) if results is not None: # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret = {} ret["results"] = results for key, value in list(search_results_object.items()): ret[key] = value ret["reviewer"] = request.user.groups.filter( name="Resource Reviewer").exists() ret["timestamp"] = datetime.now() ret["total_results"] = dsl.count(index="resources") return JSONResponse(ret) else: return HttpResponseNotFound( _("There was an error retrieving the search results"))
def search_results(request): has_filters = True if len(list( request.GET.items())) > 1 is not None else False request.GET = request.GET.copy() se = SearchEngineFactory().create() search_results_object = {'query': Query(se)} include_provisional = get_provisional_type(request) permitted_nodegroups = get_permitted_nodegroups(request.user) # get a list of resourceIds of type GLHER_Application_Area and GLHER_Heritage_Asset that meet # the search query then use those ids to search for related GLHER_Consultation instances request.GET[ 'resource-type-filter'] = '[{"graphid":"336d34e3-53c3-11e9-ba5f-dca90488358a","name":"GLHER_Application_Area","inverted":false}, {"graphid":"076f9381-7b00-11e9-8d6b-80000b44d1d9","name":"GLHER_Heritage_Asset","inverted":false}]' search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [('search-results', '')]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err.message, status=500) dsl = search_results_object.pop('query', None) dsl.exclude('*') results = dsl.search(index='resources', limit=1000) resourceIds = [hit['_id'] for hit in results['hits']['hits']] search_results_object = {'query': Query(se)} request.GET[ 'resource-type-filter'] = '[{"graphid":"8d41e49e-a250-11e9-9eab-00224800b26d","name":"GLHER_Consultation","inverted":false}]' search_filter_factory = SearchFilterFactory(request) try: for filter_type, querystring in list( request.GET.items()) + [('search-results', '')]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) except Exception as err: return JSONResponse(err.message, status=500) # only search for realted instance references when a filter is applied (aside from the paging filter) if has_filters: # the resource id query can only return resources of type GLHER_Consultation this is to handle for the scenario # where an GLHER_Application_Area (or GLHER_Heritage_Asset) is related to something other than a GLHER_Consultation resource_id_query = Bool() resource_id_query.must( Nested(path='ids', query=Terms(field='ids.id', terms=resourceIds))) resource_id_query.must( Terms(field='graph_id', terms="8d41e49e-a250-11e9-9eab-00224800b26d")) # we need to wrap the existing query (search_results_object['query']) in a Bool query along with # the resource id query above outer_query = Bool() outer_query.should(resource_id_query) dsl = search_results_object.pop('query', None) outer_query.should(dsl.dsl.pop('query', None)) dsl.dsl['query'] = {} search_results_object['query'] = dsl search_results_object['query'].add_query(outer_query) dsl = search_results_object.pop('query', None) dsl.include('graph_id') dsl.include('root_ontology_class') dsl.include('resourceinstanceid') dsl.include('points') dsl.include('geometries') dsl.include('displayname') dsl.include('displaydescription') dsl.include('map_popup') dsl.include('provisional_resource') dsl.include('domains') # if request.GET.get('tiles', None) is not None: # dsl.include('tiles') dsl.include('tiles') results = dsl.search(index='resources') #print JSONSerializer().serialize(dsl.dsl) if results is not None: # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [('search-results', '')]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret = {} ret['results'] = results for key, value in list(search_results_object.items()): ret[key] = value ret['reviewer'] = request.user.groups.filter( name='Resource Reviewer').exists() ret['timestamp'] = datetime.now() ret['total_results'] = dsl.count(index='resources') return JSONResponse(ret) else: return HttpResponseNotFound( _("There was an error retrieving the search results"))
def search_results(request, returnDsl=False): for_export = request.GET.get("export") pages = request.GET.get("pages", None) total = int(request.GET.get("total", "0")) resourceinstanceid = request.GET.get("id", None) load_tiles = request.GET.get("tiles", False) if load_tiles: try: load_tiles = json.loads(load_tiles) except TypeError: pass se = SearchEngineFactory().create() permitted_nodegroups = get_permitted_nodegroups(request.user) include_provisional = get_provisional_type(request) search_filter_factory = SearchFilterFactory(request) search_results_object = {"query": Query(se)} try: for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.append_dsl(search_results_object, permitted_nodegroups, include_provisional) append_instance_permission_filter_dsl(request, search_results_object) except Exception as err: logger.exception(err) return JSONErrorResponse(message=err) dsl = search_results_object.pop("query", None) if returnDsl: return dsl dsl.include("graph_id") dsl.include("root_ontology_class") dsl.include("resourceinstanceid") dsl.include("points") dsl.include("permissions.users_without_read_perm") dsl.include("permissions.users_without_edit_perm") dsl.include("permissions.users_without_delete_perm") dsl.include("permissions.users_with_no_access") dsl.include("geometries") dsl.include("displayname") dsl.include("displaydescription") dsl.include("map_popup") dsl.include("provisional_resource") if load_tiles: dsl.include("tiles") if for_export or pages: results = dsl.search(index=RESOURCES_INDEX, scroll="1m") scroll_id = results["_scroll_id"] if not pages: if total <= settings.SEARCH_EXPORT_LIMIT: pages = (total // settings.SEARCH_RESULT_LIMIT) + 1 if total > settings.SEARCH_EXPORT_LIMIT: pages = int(settings.SEARCH_EXPORT_LIMIT // settings.SEARCH_RESULT_LIMIT) - 1 for page in range(int(pages)): results_scrolled = dsl.se.es.scroll(scroll_id=scroll_id, scroll="1m") results["hits"]["hits"] += results_scrolled["hits"]["hits"] else: results = dsl.search(index=RESOURCES_INDEX, id=resourceinstanceid) ret = {} if results is not None: if "hits" not in results: if "docs" in results: results = {"hits": {"hits": results["docs"]}} else: results = {"hits": {"hits": [results]}} # allow filters to modify the results for filter_type, querystring in list( request.GET.items()) + [("search-results", "")]: search_filter = search_filter_factory.get_filter(filter_type) if search_filter: search_filter.post_search_hook(search_results_object, results, permitted_nodegroups) ret["results"] = results for key, value in list(search_results_object.items()): ret[key] = value ret["reviewer"] = user_is_resource_reviewer(request.user) ret["timestamp"] = datetime.now() ret["total_results"] = dsl.count(index=RESOURCES_INDEX) ret["userid"] = request.user.id return JSONResponse(ret) else: ret = { "message": _("There was an error retrieving the search results") } return JSONResponse(ret, status=500)