def add_to_index(self, model):
        if self.is_indexed(model):
            self.remove_from_index(model)

        try:
            text = model.get_text_only()
        except AttributeError:
            raise NotImplementedError(unicode(model) + " must implement get_text_only()")

        p = settings.SEARCH_STEMMER()
        stemmed_text = [p.stem(s.lower()) for s in self.__separate_words(text) if s != ""]

        for i in range(len(stemmed_text)):
            word = stemmed_text[i]
            if word in IGNORE_WORDS:
                continue

            try:
                word = Word.objects.get(word=word, namespace=self.model.db_table)
            except Word.DoesNotExist:
                word = Word(word=word, namespace=self.model.db_table)
                word.save()

            word_location = WordLocation(document_id=model.id, word=word, location=i)
            word_location.save()
Ejemplo n.º 2
0
	def test_is_indexed(self):
		model = TestModel()
		model.id = 55

		w = Word(word='word', namespace=model.db_table)
		w.save()

		word = WordLocation(document_id=55, word=w, location=1)
		word.save()

		self.assert_(self.manager.is_indexed(model))