0
votes

I'm new to whole search stuff and having a hard time learning haystack.(solr as backend)

Here is my understanding of haystack/solr.
Please comment on it if my understanding is wrong.

Solr
solr is document search engine. (opposed to RDBMS)
It allows user to quickly search document for a given term.(inverted-index may be used for this)
solr also allows facet-search given that documents are well structured.(as in xml)

Haystack
For some applications(such as eCommerse site), it would be a good idea to store product data in a RDBMS.(Well xml could also work, but I just guess that RDBMS is better at updating than xml)
If we store product data in RDBMS, we could write queries to fetch relavant data for user query, but it's troublesome.
Haystack can convert data in RDBMS to document so that solr can index it. After solr indexes data, most of search can be delegated to solr.

Here comes the question.

  1. Can I create DB schema as I want and still use the haystack/solr?

  2. suppose I have DB model as follows, What would my search_indexes.py look like to enable faceted search for any category?

...

class Category(models.Model):   
    name = models.CharField(max_length=200)  
    parentCategory = models.ForeignKey('self', null=True, blank=True)

class Item(models.Model):   
    name = models.CharField(max_length=200)  
    categorys = models.ManyToManyField(Category)  
    details = models.CharField(max_length=1024)

Note, Category can be nested(category can have a parent category).

1
Before the code, start a new paragraph w/ content or <p></p> to separate the list and the code; or indent the code twice (8 spaces)okm
@okm: Thanks for the editing fix!eugene

1 Answers

1
votes

Haystack is for, as its brief says, modular search for Django. It focuses to ease the way to construct site search and maintain indexes and does very well. There is no meaning to compare it w/ general-purpose Django ORM, although their searching functions overlap to some extent.
For your model, since there are more than one category an Item() belongs to, you could try FacetMultiValueField:

# in models.py
class Category(models.Model):
    # add __unicode__ method
    def __unicode__(self):
        return self.name

# in search_indexes.py
from haystack.indexes import *
class ItemIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    name = CharField(model_attr='name')
    categories = FacetMultiValueField() # or MultiValueField w/ facet=True

    def prepare_categories(self, obj):
        return obj.categories.all()

And follow http://django-haystack.readthedocs.org/en/latest/faceting.html to operate SearchQuerySet in the view.