1

I'm using Haystack and Whoosh with Django and I was trying to create the indexes through the code below:

class LivroIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    autor = CharField(model_attr='Autor')
    titulo =  CharField(model_attr='Titulo')
    datalivro = DateTimeField(model_attr='DataLivro')


    def index_queryset(self):
        return Livro.objects.filter(DataLivro__lte=datetime.datetime.now())


    def prepare(self, obj):
        self.prepared_data = super(LivroIndex, self).prepare(obj)
        self.prepared_data['text'] = obj.Autor
        return self.prepared_data

Livro is a class in my models file.

In this way, you can use only "autor" as a field which users can use to search content inside the application. Is there any other ways to make it accept more than one field?

Thanks in advance!

Nilesh
  • 20,521
  • 16
  • 92
  • 148

2 Answers2

1

use template and add which fields you want to be found in the search

like

{{ object.autor }}
{{ object.titulo  }}
{{ object.datalivro }}
soField
  • 2,536
  • 9
  • 36
  • 44
  • My problem is more at the methods which Haystack uses for the kernel of the search, and not at the template. This works fine and worked one time, but only if I search for the Author. If I try to search for the book's title, it doesn't work because the field is not at the "prepare" method. –  Mar 28 '12 at 19:07
0

You're supposed to concatenate all the fields you want to search on in the text field. Usually you do this via a template, rather than via the prepare method. See the tutorial for instructions.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I've seen and done this tutorial and to solve the problem where Haystack can't find the results of its own search is that you have to prepare the fields you want to search. I want an expression or command which I can prepare more than one field. –  Mar 28 '12 at 19:04