Commits

Alexander Solovyov committed 98fd4af

haystack updated to tip

Comments (0)

Files changed (6)

 ^doc-build/
 ^local/
 ^fabfile
+^\.search/

compat/haystack/backends/whoosh_backend.py

         # It's important to grab the hits first before slicing. Otherwise, this
         # can cause pagination failures.
         hits = len(raw_results)
-        raw_results = raw_results[start_offset:end_offset]
+        sliced_results = raw_results[start_offset:end_offset]
         
         facets = {}
         spelling_suggestion = None
         indexed_models = site.get_indexed_models()
         
-        for doc_offset, raw_result in enumerate(raw_results):
+        for doc_offset, raw_result in enumerate(sliced_results):
+            score = raw_results.score(doc_offset + start_offset) or 0
             raw_result = dict(raw_result)
             app_label, model_name = raw_result['django_ct'].split('.')
             additional_fields = {}
                         self.content_field_name: [highlight(additional_fields.get(self.content_field_name), terms, sa, ContextFragmenter(terms), UppercaseFormatter())],
                     }
                 
-                if hasattr(raw_results, 'score'):
-                    score = raw_results.score(doc_offset)
-                else:
-                    score = None
-                
-                if score is None:
-                    score = 0
-                
                 result = SearchResult(app_label, model_name, raw_result['django_id'], score, **additional_fields)
                 results.append(result)
             else:

compat/haystack/management/commands/clear_index.py

+from optparse import make_option
 import sys
-from django.core.management.base import NoArgsCommand
+from django.core.management.base import BaseCommand
 
 
-class Command(NoArgsCommand):
+class Command(BaseCommand):
     help = "Clears out the search index completely."
+    base_options = (
+        make_option('--noinput', action='store_false', dest='interactive', default=True,
+            help='If provided, no prompts will be issued to the user and the data will be wiped out.'
+        ),
+    )
+    option_list = BaseCommand.option_list + base_options
     
-    def handle_noargs(self, **options):
+    def handle(self, **options):
         """Clears out the search index completely."""
         # Cause the default site to load.
         from haystack import site
         
-        print
-        print "WARNING: This will irreparably remove EVERYTHING from your search index."
-        print "Your choices after this are to restore from backups or rebuild via the `rebuild_index` command."
-        
-        yes_or_no = raw_input("Are you sure you wish to continue? [y/N] ")
-        print
-        
-        if not yes_or_no.lower().startswith('y'):
-            print "No action taken."
-            sys.exit()
+        if options.get('interactive', True):
+            print
+            print "WARNING: This will irreparably remove EVERYTHING from your search index."
+            print "Your choices after this are to restore from backups or rebuild via the `rebuild_index` command."
+            
+            yes_or_no = raw_input("Are you sure you wish to continue? [y/N] ")
+            print
+            
+            if not yes_or_no.lower().startswith('y'):
+                print "No action taken."
+                sys.exit()
         
         print "Removing all documents from your index because you said so."
         

compat/haystack/management/commands/rebuild_index.py

 from django.conf import settings
 from django.core.management import call_command
-from django.core.management.base import NoArgsCommand, CommandError
+from django.core.management.base import BaseCommand, CommandError
+from haystack.management.commands.clear_index import Command as ClearCommand
+from haystack.management.commands.update_index import Command as UpdateCommand
 
 
-class Command(NoArgsCommand):
+class Command(BaseCommand):
     help = "Completely rebuilds the search index by removing the old data and then updating."
+    option_list = BaseCommand.option_list + ClearCommand.base_options + UpdateCommand.base_options
     
-    def handle_noargs(self, **options):
-        call_command('clear_index')
-        call_command('update_index')
+    def handle(self, **options):
+        call_command('clear_index', **options)
+        call_command('update_index', **options)

compat/haystack/management/commands/update_index.py

 
 class Command(AppCommand):
     help = "Freshens the index for the given app(s)."
-    option_list = AppCommand.option_list + (
+    base_options = (
         make_option('-a', '--age', action='store', dest='age',
             default=DEFAULT_AGE, type='int',
             help='Number of hours back to consider objects new.'
             type='string', help='The site object to use when reindexing (like `search_sites.mysite`).'
         ),
     )
+    option_list = AppCommand.option_list + base_options
     
     # Django 1.0.X compatibility.
     verbosity_present = False

compat/haystack/utils.py

         # counting the number of found offsets (-1 to fit in the window).
         highest_density = 0
         
+        if words_found[:-1][0] > self.max_length:
+            best_start = words_found[:-1][0]
+            best_end = best_start + self.max_length
+        
         for count, start in enumerate(words_found[:-1]):
             current_density = 1