Avid Coder
Avid Coder

Reputation: 18387

django-haystack -- how to bring few apps under the same search page?

Django-haystack has examples of how to make one app searchable. We'll that is great! However, when you have more than one app and each one is related to the User, how would you go about having haystack (faceted) to allow you search for what you want. Let's say on all three of these models.

Example: Show me all male users who have a keyword of "experienced" in their description who also have a skill with the name "Analyst" whose Info keywords contains "bla".

I googled with no result. So, I am looking at bringing few apps under the same search page.

class UserProfile(models.Model):

    GENDER_MALE = 1
    GENDER_FEMALE = 2
    GENDER_CHOICES = (
        (GENDER_MALE, 'Male'),
        (GENDER_FEMALE, 'Female'),
    )

    user = models.OneToOneField(User, related_name="%(class)s", unique=True)

    full_name = models.CharField(
                _("Full name"), 
                max_length=200, 
                blank=True, 
    )

    gender = models.IntegerField(
                _('Gender'),
                choices=GENDER_CHOICES,
                blank=False,
                null=True,
    )

    # common
    country = CountryField(
                _('Country'),
                null=True,
                blank=False,
    )

    # common
    about = models.TextField(
                _('About Me'), 
                blank=True, 
                validators=[MaxLengthValidator(400)],
    )

    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)


    def __unicode__(self):
        return u'%s' % (self.user.username)


class Skill(models.Model):

    user = models.ForeignKey(
                User, 
                related_name="%(class)s"
    )

    name = models.CharField(
                _('Skill Name'),
                max_length=70,
                null=False
    )

    category = models.ForeignKey(
                'self',
                blank=True,
                null=True
    )

    is_active = models.BooleanField(
                default=True
    )

    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    def __unicode__(self):
        return u'%s' % (self.name)

class Info(models.Model):
    user = models.ForeignKey(
                User,
                related_name="%(class)s",
                null=False
    )

    description = models.TextField(
                blank=False, 
    )

    keywords = models.CharField(
                blank=True,
                null=True, 
                max_length=56,
    )

    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    def __unicode__(self):
        return u'%s' % self.title

Upvotes: 1

Views: 210

Answers (1)

Amarpreet Singh
Amarpreet Singh

Reputation: 2260

I think if i am not wrong your question is to how to make facets work for multiple apps.

In url.py try something like

sqs = SearchQuerySet().facet('field1').facet('field2').facet('field3') 

urlpatterns += patterns('haystack.views',
url(r'^$', FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs), name='haystack_search'),) 

In search.html

{% if facets.fields.field1 %} {% for type1 in facets.fields.field1 %} 
{{ type1.0 }} ({{ type1.1 }}) {% endfor %} {% else %} 
No type1 facets.

{% endif %} 
{% if facets.fields.field2 %} {% for author in facets.fields.field2 %} 
{{ type2.0 }} ({{ type2.1 }}) {% endfor %} {% else %} 
No type2 facets.

{% endif %} 
{% if facets.fields.field3 %} {% for author in facets.fields.field3 %} 
{{ type3.0 }} ({{ type3.1 }}) {% endfor %} {% else %} 
No type3 facets.

{% endif %} 

Your Apps should have individual search_index files .

Check the output of rebuild_index and update_index, whther indexing is done properly or not.

Upvotes: 1

Related Questions