本文整理汇总了Python中wagtail.wagtailsearch.backends.get_search_backend函数的典型用法代码示例。如果您正苦于以下问题:Python get_search_backend函数的具体用法?Python get_search_backend怎么用?Python get_search_backend使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_search_backend函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_backend_loader
def test_backend_loader(self):
# Test DB backend import
db = get_search_backend(backend='wagtail.wagtailsearch.backends.db.DBSearch')
self.assertIsInstance(db, DBSearch)
# Test Elastic search backend import
elasticsearch = get_search_backend(backend='wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch')
self.assertIsInstance(elasticsearch, ElasticSearch)
# Test loading a non existant backend
self.assertRaises(InvalidSearchBackendError, get_search_backend, backend='wagtail.wagtailsearch.backends.doesntexist.DoesntExist')
# Test something that isn't a backend
self.assertRaises(InvalidSearchBackendError, get_search_backend, backend="I'm not a backend!")
示例2: update_backend
def update_backend(self, backend_name, object_list):
# Print info
self.stdout.write("Updating backend: " + backend_name)
# Get backend
backend = get_search_backend(backend_name)
# Reset the index
self.stdout.write(backend_name + ": Reseting index")
backend.reset_index()
for model, queryset in object_list:
self.stdout.write(backend_name + ": Indexing model '%s.%s'" % (
model._meta.app_label,
model.__name__,
))
# Add type
backend.add_type(model)
# Add objects
backend.add_bulk(model, queryset)
# Refresh index
self.stdout.write(backend_name + ": Refreshing index")
backend.refresh_index()
示例3: search
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, cls, fields=fields, filters=filters, prefetch_related=prefetch_related)
示例4: setUp
def setUp(self):
self.backend = get_search_backend('elasticsearch')
self.backend.rebuilder_class = self.backend.atomic_rebuilder_class
self.es = self.backend.es
self.rebuilder = self.backend.get_rebuilder()
self.backend.reset_index()
示例5: search
def search(request):
do_json = 'json' in request.GET
search_query = request.GET.get('query', None)
page = request.GET.get('page', 1)
# Search
if search_query:
page_alias_content_type = ContentType.objects.get_for_model(PageAlias)
search_results = (
Page.objects.live()
# exclude root and home pages
.filter(depth__gt=2)
# exclude PageAlias pages
.exclude(content_type=page_alias_content_type)
.search(search_query)
)
query = Query.get(search_query)
# log the query so Wagtail can suggest promoted results
query.add_hit()
# promoted search results
promoted_page_ids = [
pick.page.id for pick in query.editors_picks.all()
]
promoted_results = Page.objects.filter(pk__in=promoted_page_ids)
# search Person snippets
search_backend = get_search_backend()
people_results = search_backend.search(
search_query, Person.objects.all()
)
else:
search_results = Page.objects.none()
promoted_results = Page.objects.none()
people_results = Person.objects.none()
# Pagination
paginator = Paginator(search_results, 10)
try:
search_results = paginator.page(page)
except PageNotAnInteger:
search_results = paginator.page(1)
except EmptyPage:
search_results = paginator.page(paginator.num_pages)
response = {
'search_query': search_query,
'search_results': search_results,
'promoted_results': promoted_results,
'people_results': people_results,
}
if do_json:
return JsonResponse(get_results_json(response))
else:
return render(request, 'search/search.html', response)
示例6: search
def search(self, query_string, fields=None,
operator=None, order_by_relevance=True, backend='default'):
"""
This runs a search query on all the items in the QuerySet
"""
search_backend = get_search_backend(backend)
return search_backend.search(query_string, self, fields=fields,
operator=operator, order_by_relevance=order_by_relevance)
示例7: handle
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.get_indexed_objects():
# 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()
示例8: find_backend
def find_backend(cls):
if not hasattr(settings, 'WAGTAILSEARCH_BACKENDS'):
if cls == DBSearch:
return 'default'
else:
return
for backend in settings.WAGTAILSEARCH_BACKENDS.keys():
if isinstance(get_search_backend(backend), cls):
return backend
示例9: list
def list(request, app_label, model_name):
model = get_snippet_model_from_url_params(app_label, model_name)
permissions = [
get_permission_name(action, model)
for action in ['add', 'change', 'delete']
]
if not any([request.user.has_perm(perm) for perm in permissions]):
return permission_denied(request)
items = model.objects.all()
# Preserve the snippet's model-level ordering if specified, but fall back on PK if not
# (to ensure pagination is consistent)
if not items.ordered:
items = items.order_by('pk')
# Search
is_searchable = class_is_indexed(model)
is_searching = False
search_query = None
if is_searchable and 'q' in request.GET:
search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': model._meta.verbose_name_plural
})
if search_form.is_valid():
search_query = search_form.cleaned_data['q']
search_backend = get_search_backend()
items = search_backend.search(search_query, items)
is_searching = True
else:
search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': model._meta.verbose_name_plural
})
paginator, paginated_items = paginate(request, items)
# Template
if request.is_ajax():
template = 'wagtailsnippets/snippets/results.html'
else:
template = 'wagtailsnippets/snippets/type_index.html'
return render(request, template, {
'model_opts': model._meta,
'items': paginated_items,
'can_add_snippet': request.user.has_perm(get_permission_name('add', model)),
'is_searchable': is_searchable,
'search_form': search_form,
'is_searching': is_searching,
'query_string': search_query,
})
示例10: choose
def choose(request, app_label, model_name):
model = get_snippet_model_from_url_params(app_label, model_name)
items = model.objects.all()
# Search
is_searchable = class_is_indexed(model)
is_searching = False
search_query = None
if is_searchable and "q" in request.GET:
search_form = SearchForm(
request.GET, placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name}
)
if search_form.is_valid():
search_query = search_form.cleaned_data["q"]
search_backend = get_search_backend()
items = search_backend.search(search_query, items)
is_searching = True
else:
search_form = SearchForm(
placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name}
)
# Pagination
paginator, paginated_items = paginate(request, items, per_page=25)
# If paginating or searching, render "results.html"
if request.GET.get("results", None) == "true":
return render(
request,
"wagtailsnippets/chooser/results.html",
{
"model_opts": model._meta,
"items": paginated_items,
"query_string": search_query,
"is_searching": is_searching,
},
)
return render_modal_workflow(
request,
"wagtailsnippets/chooser/choose.html",
"wagtailsnippets/chooser/choose.js",
{
"model_opts": model._meta,
"items": paginated_items,
"is_searchable": is_searchable,
"search_form": search_form,
"query_string": search_query,
"is_searching": is_searching,
},
)
示例11: list
def list(request, content_type_app_name, content_type_model_name):
content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
model = content_type.model_class()
permissions = [
get_permission_name(action, model)
for action in ['add', 'change', 'delete']
]
if not any([request.user.has_perm(perm) for perm in permissions]):
return permission_denied(request)
snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)
items = model.objects.all()
# Search
is_searchable = class_is_indexed(model)
is_searching = False
search_query = None
if is_searchable and 'q' in request.GET:
search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': snippet_type_name_plural
})
if search_form.is_valid():
search_query = search_form.cleaned_data['q']
search_backend = get_search_backend()
items = search_backend.search(search_query, items)
is_searching = True
else:
search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': snippet_type_name_plural
})
paginator, paginated_items = paginate(request, items)
# Template
if request.is_ajax():
template = 'wagtailsnippets/snippets/results.html'
else:
template = 'wagtailsnippets/snippets/type_index.html'
return render(request, template, {
'content_type': content_type,
'snippet_type_name': snippet_type_name,
'snippet_type_name_plural': snippet_type_name_plural,
'items': paginated_items,
'can_add_snippet': request.user.has_perm(get_permission_name('add', model)),
'is_searchable': is_searchable,
'search_form': search_form,
'is_searching': is_searching,
'query_string': search_query,
})
示例12: setUp
def setUp(self):
# Search WAGTAILSEARCH_BACKENDS for an entry that uses the given backend path
for backend_name, backend_conf in settings.WAGTAILSEARCH_BACKENDS.items():
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()
示例13: choose
def choose(request, content_type_app_name, content_type_model_name):
content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
model = content_type.model_class()
snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)
items = model.objects.all()
# Search
is_searchable = class_is_indexed(model)
is_searching = False
search_query = None
if is_searchable and 'q' in request.GET:
search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': snippet_type_name_plural
})
if search_form.is_valid():
search_query = search_form.cleaned_data['q']
search_backend = get_search_backend()
items = search_backend.search(search_query, items)
is_searching = True
else:
search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % {
'snippet_type_name': snippet_type_name_plural
})
# Pagination
paginator, paginated_items = paginate(request, items, per_page=25)
# If paginating or searching, render "results.html"
if request.GET.get('results', None) == 'true':
return render(request, "wagtailsnippets/chooser/results.html", {
'content_type': content_type,
'snippet_type_name': snippet_type_name,
'items': paginated_items,
'query_string': search_query,
'is_searching': is_searching,
})
return render_modal_workflow(
request,
'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
{
'content_type': content_type,
'snippet_type_name': snippet_type_name,
'items': paginated_items,
'is_searchable': is_searchable,
'search_form': search_form,
'query_string': search_query,
'is_searching': is_searching,
}
)
示例14: list
def list(request, app_label, model_name):
model = get_snippet_model_from_url_params(app_label, model_name)
permissions = [get_permission_name(action, model) for action in ["add", "change", "delete"]]
if not any([request.user.has_perm(perm) for perm in permissions]):
return permission_denied(request)
items = model.objects.all()
# Search
is_searchable = class_is_indexed(model)
is_searching = False
search_query = None
if is_searchable and "q" in request.GET:
search_form = SearchForm(
request.GET,
placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name_plural},
)
if search_form.is_valid():
search_query = search_form.cleaned_data["q"]
search_backend = get_search_backend()
items = search_backend.search(search_query, items)
is_searching = True
else:
search_form = SearchForm(
placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name_plural}
)
paginator, paginated_items = paginate(request, items)
# Template
if request.is_ajax():
template = "wagtailsnippets/snippets/results.html"
else:
template = "wagtailsnippets/snippets/type_index.html"
return render(
request,
template,
{
"model_opts": model._meta,
"items": paginated_items,
"can_add_snippet": request.user.has_perm(get_permission_name("add", model)),
"is_searchable": is_searchable,
"search_form": search_form,
"is_searching": is_searching,
"query_string": search_query,
},
)
示例15: get_elasticsearch_backend
def get_elasticsearch_backend(self):
from django.conf import settings
from wagtail.wagtailsearch.backends import get_search_backend
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch'
# Search WAGTAILSEARCH_BACKENDS for an entry that uses the given backend path
for backend_name, backend_conf in settings.WAGTAILSEARCH_BACKENDS.items():
if backend_conf['BACKEND'] == backend_path:
return get_search_backend(backend_name)
else:
# no conf entry found - skip tests for this backend
raise unittest.SkipTest("No WAGTAILSEARCH_BACKENDS entry for the backend %s" % backend_path)