def test_added(self): search = QuestionMappingType.search() # Create a question--that adds one document to the index. q = question(title=u'Does this test work?', save=True) self.refresh() query = dict(('%s__match' % field, 'test') for field in QuestionMappingType.get_query_fields()) eq_(search.query(should=True, **query).count(), 1) # Create an answer for the question. It shouldn't be searchable # until the answer is saved. a = answer(content=u'There\'s only one way to find out!', question=q) self.refresh() query = dict(('%s__match' % field, 'only') for field in QuestionMappingType.get_query_fields()) eq_(search.query(should=True, **query).count(), 0) a.save() self.refresh() query = dict(('%s__match' % field, 'only') for field in QuestionMappingType.get_query_fields()) eq_(search.query(should=True, **query).count(), 1) # Make sure that there's only one question document in the # index--creating an answer should have updated the existing # one. eq_(search.count(), 1)
def opensearch_suggestions(request): """A simple search view that returns OpenSearch suggestions.""" content_type = 'application/x-suggestions+json' term = request.GET.get('q') if not term: return HttpResponseBadRequest(content_type=content_type) locale = locale_or_default(request.LANGUAGE_CODE) # FIXME: Rewrite this using the simple search search business # logic. This currently returns templates (amongst other things) # which is totally wrong. try: query = dict(('%s__match' % field, term) for field in DocumentMappingType.get_query_fields()) # Upgrade the query to an analyzer-aware one. query = es_utils.es_query_with_analyzer(query, locale) wiki_s = (DocumentMappingType.search().filter( document_is_archived=False).filter( document_locale=locale).values_dict( 'document_title', 'url').query(or_=query)[:5]) query = dict(('%s__match' % field, term) for field in QuestionMappingType.get_query_fields()) question_s = (QuestionMappingType.search().filter( question_has_helpful=True).values_dict('question_title', 'url').query(or_=query)[:5]) results = list(chain(question_s, wiki_s)) except ES_EXCEPTIONS: # If we have ES problems, we just send back an empty result # set. results = [] def urlize(r): return u'%s://%s%s' % ('https' if request.is_secure() else 'http', request.get_host(), r['url'][0]) def titleize(r): # NB: Elasticsearch returns an array of strings as the value, # so we mimic that and then pull out the first (and only) # string. return r.get('document_title', r.get('question_title', [_('No title')]))[0] data = [ term, [titleize(r) for r in results], [], [urlize(r) for r in results] ] return HttpResponse(json.dumps(data), content_type=content_type)
def opensearch_suggestions(request): """A simple search view that returns OpenSearch suggestions.""" content_type = "application/x-suggestions+json" term = request.GET.get("q") if not term: return HttpResponseBadRequest(content_type=content_type) locale = locale_or_default(request.LANGUAGE_CODE) # FIXME: Rewrite this using the simple search search business # logic. This currently returns templates (amongst other things) # which is totally wrong. try: query = dict(("%s__match" % field, term) for field in DocumentMappingType.get_query_fields()) # Upgrade the query to an analyzer-aware one. query = es_utils.es_query_with_analyzer(query, locale) wiki_s = ( DocumentMappingType.search() .filter(document_is_archived=False) .filter(document_locale=locale) .values_dict("document_title", "url") .query(or_=query)[:5] ) query = dict(("%s__match" % field, term) for field in QuestionMappingType.get_query_fields()) question_s = ( QuestionMappingType.search() .filter(question_has_helpful=True) .values_dict("question_title", "url") .query(or_=query)[:5] ) results = list(chain(question_s, wiki_s)) except ES_EXCEPTIONS: # If we have ES problems, we just send back an empty result # set. results = [] def urlize(r): return u"%s://%s%s" % ("https" if request.is_secure() else "http", request.get_host(), r["url"][0]) def titleize(r): # NB: Elasticsearch returns an array of strings as the value, # so we mimic that and then pull out the first (and only) # string. return r.get("document_title", r.get("question_title", [_("No title")]))[0] data = [term, [titleize(r) for r in results], [], [urlize(r) for r in results]] return HttpResponse(json.dumps(data), content_type=content_type)
def suggestions(request): """A simple search view that returns OpenSearch suggestions.""" content_type = 'application/x-suggestions+json' term = request.GET.get('q') if not term: return HttpResponseBadRequest(content_type=content_type) site = Site.objects.get_current() locale = locale_or_default(request.LANGUAGE_CODE) try: query = dict(('{0!s}__match'.format(field), term) for field in DocumentMappingType.get_query_fields()) # Upgrade the query to an analyzer-aware one. query = es_utils.es_query_with_analyzer(query, locale) wiki_s = (DocumentMappingType.search() .filter(document_is_archived=False) .filter(document_locale=locale) .values_dict('document_title', 'url') .query(or_=query)[:5]) query = dict(('{0!s}__match'.format(field), term) for field in QuestionMappingType.get_query_fields()) question_s = (QuestionMappingType.search() .filter(question_has_helpful=True) .values_dict('question_title', 'url') .query(or_=query)[:5]) results = list(chain(question_s, wiki_s)) except ES_EXCEPTIONS: # If we have ES problems, we just send back an empty result # set. results = [] def urlize(r): return u'https://{0!s}{1!s}'.format(site, r['url']) def titleize(r): return r.get('document_title', r.get('document_title')) data = [term, [titleize(r) for r in results], [], [urlize(r) for r in results]] return HttpResponse(json.dumps(data), content_type=content_type)
def suggestions(request): """A simple search view that returns OpenSearch suggestions.""" content_type = 'application/x-suggestions+json' term = request.GET.get('q') if not term: return HttpResponseBadRequest(content_type=content_type) site = Site.objects.get_current() locale = locale_or_default(request.LANGUAGE_CODE) try: query = dict(('%s__match' % field, term) for field in DocumentMappingType.get_query_fields()) # Upgrade the query to an analyzer-aware one. query = es_utils.es_query_with_analyzer(query, locale) wiki_s = (DocumentMappingType.search() .filter(document_is_archived=False) .filter(document_locale=locale) .values_dict('document_title', 'url') .query(or_=query)[:5]) query = dict(('%s__match' % field, term) for field in QuestionMappingType.get_query_fields()) question_s = (QuestionMappingType.search() .filter(question_has_helpful=True) .values_dict('question_title', 'url') .query(or_=query)[:5]) results = list(chain(question_s, wiki_s)) except ES_EXCEPTIONS: # If we have ES problems, we just send back an empty result # set. results = [] def urlize(r): return u'https://%s%s' % (site, r['url']) def titleize(r): return r.get('document_title', r.get('document_title')) data = [term, [titleize(r) for r in results], [], [urlize(r) for r in results]] return HttpResponse(json.dumps(data), content_type=content_type)
def suggestions(request): """A simple search view that returns OpenSearch suggestions.""" mimetype = 'application/x-suggestions+json' term = request.GET.get('q') if not term: return HttpResponseBadRequest(mimetype=mimetype) site = Site.objects.get_current() locale = locale_or_default(request.LANGUAGE_CODE) try: query = dict(('%s__text' % field, term) for field in DocumentMappingType.get_query_fields()) wiki_s = (DocumentMappingType.search() .filter(document_is_archived=False) .filter(document_locale=locale) .values_dict('document_title', 'url') .query(or_=query)[:5]) query = dict(('%s__text' % field, term) for field in QuestionMappingType.get_query_fields()) question_s = (QuestionMappingType.search() .filter(question_has_helpful=True) .values_dict('question_title', 'url') .query(or_=query)[:5]) results = list(chain(question_s, wiki_s)) except ES_EXCEPTIONS: # If we have ES problems, we just send back an empty result # set. results = [] urlize = lambda r: u'https://%s%s' % (site, r['url']) titleize = lambda r: (r['document_title'] if 'document_title' in r else r['question_title']) data = [term, [titleize(r) for r in results], [], [urlize(r) for r in results]] return HttpResponse(json.dumps(data), mimetype=mimetype)
def get_query_fields(self): return QuestionMappingType.get_query_fields()