Multiple location fields

94 views
Skip to first unread message

Mario Menezes

unread,
Jun 29, 2012, 1:50:07 PM6/29/12
to django-...@googlegroups.com
Hi,

  I'm using django-haystack and ElasticSearch to index Stores.

  Until now, each store had one lat,long coordinate pair; we had to change this to represent the fact that one store can deliver products to very different regions (disjunct) I've added up to ten locations (lat,long pairs) to then.

  When using one location field everything was working fine and I got right results. Now, with multiple location fields, I can't get any results, not even the previuos one, for the same user and store coordinates.

  My Index is as following:

class StoreIndex(indexes.SearchIndex,indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True,
                             template_name='search/indexes/store/store_text.txt')
    location0 = indexes.LocationField()
    location1 = indexes.LocationField()   
    location2 = indexes.LocationField()
    location3 = indexes.LocationField()
    location4 = indexes.LocationField()
    location5 = indexes.LocationField()
    location6 = indexes.LocationField()
    location7 = indexes.LocationField()
    location8 = indexes.LocationField()
    location9 = indexes.LocationField()

    def get_model(self):
        return Store

    def prepare_location0(self, obj):
        # If you're just storing the floats...
        return "%s,%s" % (obj.latitude, obj.longitude)

    # .....   up to prepare_location9
    def prepare_location9(self, obj):
        # If you're just storing the floats...
        return "%s,%s" % (obj.latitude_9, obj.longitude_9)
 



   Is this the correct way to build my index?

   Then, I try to query this way:

   sqs0 = SearchQuerySet().dwithin('location0', usuario, max_dist).distance('location0',usuario).using('stores')

   where:
     usuario  is a Point instance representing the user trying to find stores near his position and
     max_dist is a D instance,

   If comment out the fields location1-9 from the StoreIndex class everything works fine, but if I leave them to get  multiple location points, I get no results for the same query (user position).

   Of course, I rebuild_index after any change in StoreIndex.

   Any help is appreciate.

  Thanks in advance
Reply all
Reply to author
Forward
0 new messages