def getDocsFromTopic(keyword):
    paperIds = []
    #Find the topic that the keyword is in
    topics = Topics().populateTopics()
    topicNum = -1
    for i in range(len(topics)):
        if keyword in topics[i]:
            topicNum = i

    #Find documents that have that topic
    for key in docsTopicMapping:
        if topicNum in docsTopicMapping[key]:
            paperIds.append(key)

    return paperIds
def results(page):
    global temp_sypm
    global temp_race
    global temp_topic
    global t_setQuestion
    global g_results

    if type(page) is not int:
        page = int(page.encode('utf-8'))

    if request.method == 'POST':

        symp = request.form['symptom']
        race_q = request.form['race']
        topic = request.form['topic']
        question = request.form['set']
        if len(symp) == 0 or (symp == 'None'):
            temp_sypm = ""
            symp = ""
        else:
            temp_sypm = symp

        if len(race_q) == 0 or (race_q == 'None'):
            temp_race = ""
            race_q = ""
        else:
            temp_race = race_q

        if len(question) != 0 or (question != 'None'):
            t_setQuestion = question

        if len(topic) == 0 or (topic == 'None'):
            temp_topic = ""
            topic = ""
        else:
            temp_topic = topic
    else:
        symp = temp_sypm
        race_q = temp_race
        question = t_setQuestion
        topic = temp_topic

    docs = {}
    docs['symp'] = symp
    docs['race'] = race_q
    docs['topic'] = topic
    docs['question'] = question

    search = Search(index='covid_19_index')

    s = None

    if len(symp) > 0:
        full_query = "risk factors " + symp
        s = search.query('multi_match',
                         query=full_query,
                         type='cross_fields',
                         fields=['title', 'abstract', 'body_text'])
    if len(race_q) > 0:
        full_query = "risk " + race_q
        s = search.query('multi_match',
                         query=full_query,
                         type='cross_fields',
                         fields=['title', 'abstract', 'body_text'])
    if len(topic) > 0:
        s = search.query('ids', values=getDocsFromTopic(topic))
    if len(question) > 0 & (question != 'None'):
        s = search.query('multi_match',
                         query=question,
                         type='cross_fields',
                         fields=['title', 'abstract', 'body_text'])

    start = 0 + (page - 1) * 10
    end = 10 + (page - 1) * 10

    topicsObj = Topics()

    if s is None:
        return render_template('results.html',
                               results={},
                               res_num=0,
                               page_num=0,
                               total=0,
                               queries=docs,
                               topics=topicsObj.startingTopics())
    # execute search and return results in specified range.
    response = s[start:end].execute()
    result_list = {}

    for hit in response.hits:
        result = {}
        result_list['score'] = hit.meta.score
        result['title'] = hit.title
        result['abstract'] = hit.abstract

        result['text'] = hit.body_text
        result_list[hit.meta.id] = result

    g_results = result_list
    num_results = response.hits.total['value']

    if num_results > 0:
        rem = num_results % 10
        total_pages = num_results / 10
        if rem > 0:
            total_pages = total_pages + 1
        return render_template(
            'results.html',
            results=result_list,
            res_num=num_results,
            page_num=page,
            total=total_pages,
            queries=docs,
            recommendedTopics=topicsObj.recommendedTopics(topic),
            topics=topicsObj.startingTopics())
    else:
        message = []
        message.append('Cannot formulate results')

    return render_template(
        'results.html',
        results=message,
        res_num=num_results,
        page_num=page,
        queries=docs,
        recommendedTopics=topicsObj.recommendedTopics(topic),
        topics=topicsObj.startingTopics())
def search():
    topics = Topics()
    return render_template('home_page.html', topics=topics.startingTopics())