Skip to content

Commit

Permalink
Add fuzzy matching
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed May 4, 2015
1 parent e00a411 commit e2ca3f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mapit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
url(r'^areas/(?P<type>[A-Z0-9,]*[A-Z0-9]+)%s$' % format_end, areas.areas_by_type),
url(r'^areas/(?P<name>.+?)%s$' % format_end, areas.areas_by_name),
url(r'^areas$', areas.deal_with_POST, {'call': 'areas'}),

url(r'^fuzzyareas/(?P<name>.+?)%s$' % format_end, areas.areas_by_name_fuzzy),

url(r'^code/(?P<code_type>[^/]+)/(?P<code_value>[^/]+?)%s$' % format_end, areas.area_from_code),
]

Expand Down
11 changes: 11 additions & 0 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
'django.contrib.admin',
'django.contrib.gis',
'django.contrib.staticfiles',
'djorm_pgtrgm',
'mapit',
]
if django.get_version() < '1.7':
Expand Down

0 comments on commit e2ca3f9

Please sign in to comment.