comparison custom_search/forms.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 20a3bf7a6370
children 6cc9221d04a7
comparison
equal deleted inserted replaced
942:e8b170fca581 943:cf9918328c64
63 self.cleaned_data['exclude']): 63 self.cleaned_data['exclude']):
64 raise forms.ValidationError('Please supply some search terms') 64 raise forms.ValidationError('Please supply some search terms')
65 65
66 return self.cleaned_data 66 return self.cleaned_data
67 67
68 def clean_exact(self):
69 exact_field = self.cleaned_data['exact']
70 if "'" in exact_field or '"' in exact_field:
71 raise forms.ValidationError("Quotes are not needed in this field")
72 return exact_field
73
68 def search(self): 74 def search(self):
69 if not self.is_valid(): 75 if not self.is_valid():
70 return self.no_query_found() 76 return self.no_query_found()
71 77
72 if self.user is None: 78 if self.user is None:
81 self.cleaned_data['exact'], 87 self.cleaned_data['exact'],
82 self.cleaned_data['exclude'], 88 self.cleaned_data['exclude'],
83 self.cleaned_data['models'], 89 self.cleaned_data['models'],
84 username) 90 username)
85 91
86 sqs = self.searchqueryset
87
88 # Note that in Haystack 2.x content is untrusted and is automatically 92 # Note that in Haystack 2.x content is untrusted and is automatically
89 # auto-escaped for us. 93 # auto-escaped for us.
90 # 94 #
91 # Filter on the q terms; these should be and'ed together: 95 # Gather regular search terms
92 terms = self.cleaned_data['q'].split() 96 terms = ' '.join(self.cleaned_data['q'].split())
93 for term in terms:
94 sqs = sqs.filter(content=term)
95 97
96 # Exact words or phrases: 98 # Exact words or phrases:
97 if self.cleaned_data['exact']: 99 exact = self.cleaned_data['exact'].strip()
98 sqs = sqs.filter(content__exact=self.cleaned_data['exact']) 100 if exact:
101 exact = '"{}"'.format(exact)
99 102
100 # Exclude terms: 103 # Exclude terms:
101 terms = self.cleaned_data['exclude'].split() 104 exclude = ["-{}".format(term) for term in self.cleaned_data['exclude'].split()]
102 for term in terms: 105 exclude = ' '.join(exclude)
103 sqs = sqs.exclude(content=term) 106
107 query = ' '.join([terms, exact, exclude]).strip()
108 logger.debug("auto_query: %s", query)
109
110 sqs = self.searchqueryset.auto_query(query)
104 111
105 if self.load_all: 112 if self.load_all:
106 sqs = sqs.load_all() 113 sqs = sqs.load_all()
107 114
108 # Apply model filtering 115 # Apply model filtering