Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
 def get_query_fields(self):
     return DocumentMappingType.get_query_fields()
Exemple #7
0
 def get_query_fields(self):
     return DocumentMappingType.get_query_fields()