From e2ca3f905dd28700f53d2d8765c61cc37dada96d Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 4 May 2015 14:41:23 +0100 Subject: [PATCH] Add fuzzy matching --- mapit/urls.py | 3 +++ mapit/views/areas.py | 11 +++++++++++ project/settings.py | 1 + 3 files changed, 15 insertions(+) diff --git a/mapit/urls.py b/mapit/urls.py index cf3c2990..8c652385 100644 --- a/mapit/urls.py +++ b/mapit/urls.py @@ -49,6 +49,9 @@ url(r'^areas/(?P[A-Z0-9,]*[A-Z0-9]+)%s$' % format_end, areas.areas_by_type), url(r'^areas/(?P.+?)%s$' % format_end, areas.areas_by_name), url(r'^areas$', areas.deal_with_POST, {'call': 'areas'}), + + url(r'^fuzzyareas/(?P.+?)%s$' % format_end, areas.areas_by_name_fuzzy), + url(r'^code/(?P[^/]+)/(?P[^/]+?)%s$' % format_end, areas.area_from_code), ] diff --git a/mapit/views/areas.py b/mapit/views/areas.py index bf04b191..f71a0cae 100644 --- a/mapit/views/areas.py +++ b/mapit/views/areas.py @@ -252,6 +252,17 @@ def areas_by_name(request, name, format='json'): return output_areas(request, 'Areas starting with %s' % name, format, areas) +@ratelimit(minutes=3, requests=100) +def areas_by_name_fuzzy(request, name, format='json'): + args = query_args(request, format) + args['name__similar'] = name + areas = Area.objects.filter(**args)\ + .extra(select={'distance': "similarity(mapit_area.name, %s)"}, + select_params=[name])\ + .order_by('-distance') + return output_areas(request, 'Areas matching %s' % name, format, areas) + + @ratelimit(minutes=3, requests=100) def area_geometry(request, area_id): area = _area_geometry(area_id) diff --git a/project/settings.py b/project/settings.py index 56a0f0a5..a9863fb0 100644 --- a/project/settings.py +++ b/project/settings.py @@ -231,6 +231,7 @@ 'django.contrib.admin', 'django.contrib.gis', 'django.contrib.staticfiles', + 'djorm_pgtrgm', 'mapit', ] if django.get_version() < '1.7':