def _search_suggestions(request, text, locale, product_slugs):
    """Return an iterable of the most relevant wiki pages and questions.

    :arg text: full text to search on
    :arg locale: locale to limit to
    :arg product_slugs: list of product slugs to filter articles on
        (["desktop", "mobile", ...])

    Items are dicts of::

        {
            'type':
            'search_summary':
            'title':
            'url':
            'object':
        }

    :returns: up to 3 wiki pages, then up to 3 questions.

    """
    # TODO: this can be reworked to pull data from ES rather than
    # hit the db.
    question_s = QuestionMappingType.search()
    wiki_s = DocumentMappingType.search()

    # Max number of search results per type.
    WIKI_RESULTS = QUESTIONS_RESULTS = 3
    default_categories = settings.SEARCH_DEFAULT_CATEGORIES

    # Apply product filters
    if product_slugs:
        wiki_s = wiki_s.filter(product__in=product_slugs)
        question_s = question_s.filter(product__in=product_slugs)

    results = []
    try:
        # Search for relevant KB documents.
        query = dict(('%s__match' % field, text)
                     for field in DocumentMappingType.get_query_fields())
        query.update(dict(('%s__match_phrase' % field, text)
                     for field in DocumentMappingType.get_query_fields()))
        query = es_query_with_analyzer(query, locale)
        filter = F()
        filter |= F(document_locale=locale)
        filter |= F(document_locale=settings.WIKI_DEFAULT_LANGUAGE)
        filter &= F(document_category__in=default_categories)
        filter &= F(document_is_archived=False)

        raw_results = (
            wiki_s.filter(filter)
                  .query(or_=query)
                  .values_list('id')[:WIKI_RESULTS])

        raw_results = [result[0][0] for result in raw_results]

        for id_ in raw_results:
            try:
                doc = (Document.objects.select_related('current_revision')
                                       .get(pk=id_))
                results.append({
                    'search_summary': clean_excerpt(
                        doc.current_revision.summary),
                    'url': doc.get_absolute_url(),
                    'title': doc.title,
                    'type': 'document',
                    'object': doc,
                })
            except Document.DoesNotExist:
                pass

        # Search for relevant questions.
        query = dict(('%s__match' % field, text)
                     for field in QuestionMappingType.get_query_fields())
        query.update(dict(('%s__match_phrase' % field, text)
                     for field in QuestionMappingType.get_query_fields()))

        # Filter questions by language. Questions should be either in English
        # or in the locale's language. This is because we have some questions
        # marked English that are really in other languages. The assumption
        # being that if a native speakers submits a query in given language,
        # the items that are written in that language will automatically match
        # better, so questions incorrectly marked as english can be found too.
        question_filter = F(question_locale=locale)
        question_filter |= F(question_locale=settings.WIKI_DEFAULT_LANGUAGE)
        question_filter &= F(question_is_archived=False)

        raw_results = (question_s.query(or_=query)
                       .filter(question_filter)
                       .values_list('id')[:QUESTIONS_RESULTS])
        raw_results = [result[0][0] for result in raw_results]

        for id_ in raw_results:
            try:
                q = Question.objects.get(pk=id_)
                results.append({
                    'search_summary': clean_excerpt(q.content[0:500]),
                    'url': q.get_absolute_url(),
                    'title': q.title,
                    'type': 'question',
                    'object': q,
                    'last_updated': q.updated,
                    'is_solved': q.is_solved,
                    'num_answers': q.num_answers,
                    'num_votes': q.num_votes,
                    'num_votes_past_week': q.num_votes_past_week
                })
            except Question.DoesNotExist:
                pass

    except ES_EXCEPTIONS as exc:
        statsd.incr('questions.suggestions.eserror')
        log.debug(exc)

    return results
dataset/ETHPy150Open mozilla/kitsune/kitsune/questions/views.py/_search_suggestions/original