Skip to content

Commit

Permalink
Add fuzzy matching. Fuzzy matching should be a param for /areas inste…
Browse files Browse the repository at this point in the history
…ad of a distinct endpoint.
  • Loading branch information
chris48s committed Nov 17, 2015
1 parent 4c043c5 commit ac07be6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,27 @@ def areas_by_type(request, type, format='json'):

@ratelimit(minutes=3, requests=100)
def areas_by_name(request, name, format='json'):
fuzzy = request.GET.get('fuzzy', '')
if fuzzy.lower() in ['true', '1']:
return areas_by_name_fuzzy(request, name, format)

args = query_args(request, format)
args['name__istartswith'] = name
areas = Area.objects.filter(**args)
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 areas_polygon(request, area_ids, srid='', format='kml'):
area_ids = area_ids.split(',')
Expand Down
1 change: 1 addition & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
'django.contrib.admin',
'django.contrib.gis',
'django.contrib.staticfiles',
'djorm_pgtrgm',
'mapit',
]

Expand Down

0 comments on commit ac07be6

Please sign in to comment.