def search(cls, q, results_per_page=None, page=1, prefetch_tags=False, filters={}): # Run search query search_backend = get_search_backend() if prefetch_tags: results = search_backend.search( q, cls, prefetch_related=['tagged_items__tag'], filters=filters) else: results = search_backend.search(q, cls, filters=filters) # If results_per_page is set, return a paginator if results_per_page is not None: paginator = Paginator(results, results_per_page) try: return paginator.page(page) except PageNotAnInteger: return paginator.page(1) except EmptyPage: return paginator.page(paginator.num_pages) else: return results
def search(cls, query_string, show_unpublished=False, search_title_only=False, extra_filters={}, prefetch_related=[], path=None): # Filters filters = extra_filters.copy() if not show_unpublished: filters['live'] = True # Path if path: filters['path__startswith'] = path # Fields fields = None if search_title_only: fields = ['title'] # Search s = get_search_backend() return s.search(query_string, model=cls, fields=fields, filters=filters, prefetch_related=prefetch_related)
def search( cls, query_string, show_unpublished=False, search_title_only=False, extra_filters={}, prefetch_related=[], path=None, ): # Filters filters = extra_filters.copy() if not show_unpublished: filters["live"] = True # Path if path: filters["path__startswith"] = path # Fields fields = None if search_title_only: fields = ["title"] # Search s = get_search_backend() return s.search(query_string, model=cls, fields=fields, filters=filters, prefetch_related=prefetch_related)
def handle(self, **options): # Print info self.stdout.write("Getting object list") # Get list of indexed models indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)] # Object set object_set = {} # Add all objects to object set and detect any duplicates # Duplicates are caused when both a model and a derived model are indexed # Eg, if BlogPost inherits from Page and both of these models are indexed # If we were to add all objects from both models into the index, all the BlogPosts will have two entries for model in indexed_models: # Get toplevel content type toplevel_content_type = model.indexed_get_toplevel_content_type() # Loop through objects for obj in model.objects.all(): # Get key for this object key = toplevel_content_type + ':' + str(obj.pk) # Check if this key already exists if key in object_set: # Conflict, work out who should get this space # The object with the longest content type string gets the space # Eg, "wagtailcore.Page-myapp.BlogPost" kicks out "wagtailcore.Page" if len(obj.indexed_get_content_type()) > len(object_set[key].indexed_get_content_type()): # Take the spot object_set[key] = obj else: # Space free, take it object_set[key] = obj # Search backend if 'backend' in options: s = options['backend'] else: s = get_search_backend() # Reset the index self.stdout.write("Reseting index") s.reset_index() # Add types self.stdout.write("Adding types") for model in indexed_models: s.add_type(model) # Add objects to index self.stdout.write("Adding objects") for result in s.add_bulk(object_set.values()): self.stdout.write(result[0] + ' ' + str(result[1])) # Refresh index self.stdout.write("Refreshing index") s.refresh_index()
def test_elasticsearch_backend_import(self): try: from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch except ImportError: # skip this test if we don't have the requirements for ElasticSearch installed raise unittest.SkipTest elasticsearch = get_search_backend(backend='wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch') self.assertIsInstance(elasticsearch, ElasticSearch)
def setUp(self): # Search WAGTAILSEARCH_BACKENDS for an entry that uses the given backend path for (backend_name, backend_conf) in settings.WAGTAILSEARCH_BACKENDS.iteritems(): if backend_conf['BACKEND'] == self.backend_path: self.backend = get_search_backend(backend_name) break else: # no conf entry found - skip tests for this backend raise unittest.SkipTest("No WAGTAILSEARCH_BACKENDS entry for the backend %s" % self.backend_path) self.load_test_data()
def test_elasticsearch_backend_import(self): try: from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch except ImportError: # skip this test if we don't have the requirements for ElasticSearch installed raise unittest.SkipTest elasticsearch = get_search_backend( backend='wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch' ) self.assertIsInstance(elasticsearch, ElasticSearch)
def find_elasticsearch_backend(self): if hasattr(settings, 'WAGTAILSEARCH_BACKENDS'): for backend in settings.WAGTAILSEARCH_BACKENDS.keys(): # Check that the backend is an elastic search backend if not isinstance(get_search_backend(backend), ElasticSearch): continue # Check that tests are allowed on this backend if 'RUN_TESTS' not in settings.WAGTAILSEARCH_BACKENDS[backend]: continue if settings.WAGTAILSEARCH_BACKENDS[backend]['RUN_TESTS'] is not True: continue return backend # Backend not found raise unittest.SkipTest("Could not find an ElasticSearch backend")
def search(cls, q, results_per_page=None, page=1, prefetch_tags=False, filters={}): # Run search query search_backend = get_search_backend() if prefetch_tags: results = search_backend.search(q, cls, prefetch_related=['tagged_items__tag'], filters=filters) else: results = search_backend.search(q, cls, filters=filters) # If results_per_page is set, return a paginator if results_per_page is not None: paginator = Paginator(results, results_per_page) try: return paginator.page(page) except PageNotAnInteger: return paginator.page(1) except EmptyPage: return paginator.page(paginator.num_pages) else: return results
def find_elasticsearch_backend(self): try: from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch except ImportError: # skip this test if we don't have the requirements for ElasticSearch installed raise unittest.SkipTest if hasattr(settings, 'WAGTAILSEARCH_BACKENDS'): for backend in settings.WAGTAILSEARCH_BACKENDS.keys(): # Check that the backend is an elastic search backend if not isinstance(get_search_backend(backend), ElasticSearch): continue # Check that tests are allowed on this backend if 'RUN_TESTS' not in settings.WAGTAILSEARCH_BACKENDS[backend]: continue if settings.WAGTAILSEARCH_BACKENDS[backend]['RUN_TESTS'] is not True: continue return backend # Backend not found raise unittest.SkipTest("Could not find an ElasticSearch backend")
def find_elasticsearch_backend(self): try: from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch except ImportError: # skip this test if we don't have the requirements for ElasticSearch installed raise unittest.SkipTest if hasattr(settings, 'WAGTAILSEARCH_BACKENDS'): for backend in settings.WAGTAILSEARCH_BACKENDS.keys(): # Check that the backend is an elastic search backend if not isinstance(get_search_backend(backend), ElasticSearch): continue # Check that tests are allowed on this backend if 'RUN_TESTS' not in settings.WAGTAILSEARCH_BACKENDS[backend]: continue if settings.WAGTAILSEARCH_BACKENDS[backend][ 'RUN_TESTS'] is not True: continue return backend # Backend not found raise unittest.SkipTest("Could not find an ElasticSearch backend")
def setUp(self): self.backend = get_search_backend('wagtail.wagtailsearch.backends.db.DBSearch') self.load_test_data()
def test_import_by_name(self): db = get_search_backend(backend='default') self.assertIsInstance(db, DBSearch)
def test_db_backend_import(self): db = get_search_backend( backend='wagtail.wagtailsearch.backends.db.DBSearch') self.assertIsInstance(db, DBSearch)
def setUp(self): self.backend = get_search_backend(self.find_elasticsearch_backend()) self.load_test_data()
def setUp(self): self.backend = get_search_backend( 'wagtail.wagtailsearch.backends.db.DBSearch') self.load_test_data()
def test_db_backend_import(self): db = get_search_backend(backend='wagtail.wagtailsearch.backends.db.DBSearch') self.assertIsInstance(db, DBSearch)
def test_elasticsearch_backend_import(self): elasticsearch = get_search_backend(backend='wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch') self.assertIsInstance(elasticsearch, ElasticSearch)