本文整理汇总了Python中watson.search.search函数的典型用法代码示例。如果您正苦于以下问题:Python search函数的具体用法?Python search怎么用?Python search使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了search函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testUnpublishedModelsNotFound
def testUnpublishedModelsNotFound(self):
# Make sure that there are four to find!
self.assertEqual(watson.search("tItle Content Description").count(), 4)
# Unpublish two objects.
self.test11.is_published = False
self.test11.save()
self.test21.is_published = False
self.test21.save()
# This should return 4, but two of them are unpublished.
self.assertEqual(watson.search("tItle Content Description").count(), 2)
示例2: test_pagesearchadapter_get_live_queryset
def test_pagesearchadapter_get_live_queryset(self):
self.assertEqual(len(search.search("Homepage", models=(Page,))), 1)
with publication_manager.select_published(True):
self.assertEqual(len(search.search("Homepage", models=(Page,))), 1)
self.homepage.is_online = False
self.homepage.save()
self.assertEqual(len(search.search("Homepage", models=(Page,))), 0)
示例3: testNestedUpdateInSkipContext
def testNestedUpdateInSkipContext(self):
with watson.skip_index_update():
self.test21.title = "baar"
self.test21.save()
with watson.update_index():
self.test11.title = "fooo"
self.test11.save()
# We should get "fooo", but not "baar"
self.assertEqual(watson.search("fooo").count(), 1)
self.assertEqual(watson.search("baar").count(), 0)
示例4: testUpdateSearchIndex
def testUpdateSearchIndex(self):
# Update a model and make sure that the search results match.
self.test11.title = "fooo"
self.test11.save()
# Test a search that should get one model.
exact_search = watson.search("fooo")
self.assertEqual(len(exact_search), 1)
self.assertEqual(exact_search[0].title, "fooo")
# Delete a model and make sure that the search results match.
self.test11.delete()
self.assertEqual(watson.search("fooo").count(), 0)
示例5: testBuildWatsonCommand
def testBuildWatsonCommand(self):
# Hack a change into the model using a bulk update, which doesn't send signals.
WatsonTestModel1.objects.filter(id=self.test11.id).update(title="fooo1")
WatsonTestModel2.objects.filter(id=self.test21.id).update(title="fooo2")
# Test that no update has happened.
self.assertEqual(watson.search("fooo1").count(), 0)
self.assertEqual(watson.search("fooo2").count(), 0)
# Run the rebuild command.
call_command("buildwatson", verbosity=0)
# Test that the update is now applied.
self.assertEqual(watson.search("fooo1").count(), 1)
self.assertEqual(watson.search("fooo2").count(), 1)
示例6: testSearchWithApostrophe
def testSearchWithApostrophe(self):
WatsonTestModel1.objects.create(
title="title model1 instance12",
content="content model1 instance13 d'Argent",
description="description model1 instance13",
)
self.assertEqual(watson.search("d'Argent").count(), 1)
示例7: search
def search(request):
""" Takes in http request and a user-entered search string, returns the search_results html
with the objects found by the search available to that template.
"""
search_str = request.GET["user_search"]
search_results = watson.search(search_str)
# Show 18 movies per page
paginator = Paginator(search_results, 24)
page = request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
results = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
results = paginator.page(paginator.num_pages)
template = loader.get_template('movies/search_results.html')
context = {
'results': results,
'search_str': search_str,
}
return HttpResponse(template.render(context, request))
示例8: testSearchWithAccent
def testSearchWithAccent(self):
WatsonTestModel1.objects.create(
title="title model1 instance12",
content="content model1 instance13 café",
description="description model1 instance13",
)
self.assertEqual(watson.search("café").count(), 1)
示例9: testExcludedModelQuerySet
def testExcludedModelQuerySet(self):
# Test a search that should get all models.
self.assertEqual(watson.search("TITLE", exclude=(WatsonTestModel1.objects.filter(title__icontains="FOOO"), WatsonTestModel2.objects.filter(title__icontains="FOOO"),)).count(), 4)
# Test a search that should get two models.
self.assertEqual(watson.search("MODEL1", exclude=(WatsonTestModel1.objects.filter(
title__icontains = "INSTANCE21",
description__icontains = "INSTANCE22",
),)).count(), 2)
self.assertEqual(watson.search("MODEL2", exclude=(WatsonTestModel2.objects.filter(
title__icontains = "INSTANCE11",
description__icontains = "INSTANCE12",
),)).count(), 2)
# Test a search that should get one model.
self.assertEqual(watson.search("INSTANCE11", exclude=(WatsonTestModel1.objects.filter(
title__icontains = "MODEL2",
),)).count(), 1)
self.assertEqual(watson.search("INSTANCE21", exclude=(WatsonTestModel2.objects.filter(
title__icontains = "MODEL1",
),)).count(), 1)
# Test a search that should get no models.
self.assertEqual(watson.search("INSTANCE11", exclude=(WatsonTestModel1.objects.filter(
title__icontains = "MODEL1",
),)).count(), 0)
self.assertEqual(watson.search("INSTANCE21", exclude=(WatsonTestModel2.objects.filter(
title__icontains = "MODEL2",
),)).count(), 0)
示例10: testSearchWithLeadingApostrophe
def testSearchWithLeadingApostrophe(self):
WatsonTestModel1.objects.create(
title="title model1 instance12",
content="'content model1 instance13",
description="description model1 instance13",
)
self.assertTrue(
watson.search("'content").exists()
) # Some database engines ignore leading apostrophes, some count them.
示例11: testCanOverridePublication
def testCanOverridePublication(self):
# Unpublish two objects.
self.test11.is_published = False
self.test11.save()
# This should still return 4, since we're overriding the publication.
self.assertEqual(watson.search(
"tItle Content Description",
models=(WatsonTestModel2, WatsonTestModel1._base_manager.all(),)
).count(), 4)
示例12: testSearchIndexUpdateAbandonedOnError
def testSearchIndexUpdateAbandonedOnError(self):
try:
with watson.update_index():
self.test11.title = "fooo"
self.test11.save()
raise Exception("Foo")
except:
pass
# Test a search that should get not model.
self.assertEqual(watson.search("fooo").count(), 0)
示例13: search
def search(request):
context = {}
q = ""
try:
if request.POST:
q = request.POST['q']
else:
q = request.GET['q']
except MultiValueDictKeyError:
pass
context['query'] = q
context['search_entry_list'] = watson.search(q)
return render(request, 'search.html', context)
示例14: testKitchenSink
def testKitchenSink(self):
"""For sanity, let's just test everything together in one giant search of doom!"""
self.assertEqual(watson.search(
"INSTANCE11",
models = (
WatsonTestModel1.objects.filter(title__icontains="INSTANCE11"),
WatsonTestModel2.objects.filter(title__icontains="TITLE"),
),
exclude = (
WatsonTestModel1.objects.filter(title__icontains="MODEL2"),
WatsonTestModel2.objects.filter(title__icontains="MODEL1"),
)
).get().title, "title model1 instance11")
示例15: post
def post(self, request):
"""Returns a Json response of a search query
Args:
request (object): HTTPRequest
Returns:
object: JsonResponse
"""
# get query out of request
query = request.POST['query']
# search for the query in the database
search_res = watson.search(query)
# no search results
if search_res.count() < 1:
response = JsonResponse({'search_res': []})
# search results
else:
# list of the search results
search_res_list = []
# go through all search results and add them to the list
for sres in search_res:
# set the sres to the real result
sres = sres.object
# result dict
res = {}
# set the values of the res to the sres
res['name'] = sres.name
res['overview'] = sres.overview
res['year'] = (sres.first_air_date.year if
sres.first_air_date else None)
res['genres'] = sres.get_genre_list()
# try to get the poster url
try:
res['poster'] = sres.poster_large.url
# no poster is present
except ValueError:
res['poster'] = False
# url of the series
res['url'] = sres.get_absolute_url()
# add the result dict to the search result list
search_res_list.append(res)
response = JsonResponse({'search_res': search_res_list})
return response