from django.shortcuts import render
from elasticsearch import Elasticsearch
from django.http import HttpResponse

def search(request):
    if request.method == 'GET':
        term = request.GET.get('term_search')

        if term == None:
            term = ""

        response = search_term(term)
        pages = []

        for hit in response['hits']['hits']:
            x = {'source': hit["_source"], 'highlight': hit["highlight"]["text"][0]}
            pages.append(x)

        return render(request, 'view_athena/index.html', {'pages':pages,'term_search':term})

def search_term(term):
    es = Elasticsearch()

    res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match": { "title": "\"" + str(term) + "\"" }},
                                                                         { "match": { "text": "\"" + str(term) + "\"" }},
                                                                         { "match": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})

    return res

from django.shortcuts import render
from elasticsearch import Elasticsearch
from django.http import HttpResponse

def search(request):
    if request.method == 'GET':
        term = request.GET.get('term_search')

        if term == None:
            term = ""

        response = search_term(term)
        pages = []

        for hit in response['hits']['hits']:
            x = {'source': hit["_source"], 'highlight': hit["highlight"]["text"][0]}
            pages.append(x)

        return render(request, 'view_athena/index.html', {'pages':pages,'term_search':term})

def search_term(term):
    es = Elasticsearch()

    res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match_phrase": { "title": "\"" + str(term) + "\"" }},
                                                                         { "match_phrase": { "text": "\"" + str(term) + "\"" }},
                                                                         { "match_phrase": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})

    return res

Update 'search_term' functon. Add 'match_phrase' function.
Update 'search_term' functon. Add 'match_phrase' function.

  from django.shortcuts import render
  from elasticsearch import Elasticsearch
  from django.http import HttpResponse
  
  def search(request):
      if request.method == 'GET':
          term = request.GET.get('term_search')
  
          if term == None:
              term = ""
  
          response = search_term(term)
          pages = []
  
          for hit in response['hits']['hits']:
              x = {'source': hit["_source"], 'highlight': hit["highlight"]["text"][0]}
              pages.append(x)
  
          return render(request, 'view_athena/index.html', {'pages':pages,'term_search':term})
  
  def search_term(term):
      es = Elasticsearch()
  
-     res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match": { "title": "\"" + str(term) + "\"" }},
+     res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match_phrase": { "title": "\"" + str(term) + "\"" }},
-                                                                          { "match": { "text": "\"" + str(term) + "\"" }},
+                                                                          { "match_phrase": { "text": "\"" + str(term) + "\"" }},
-                                                                          { "match": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})
+                                                                          { "match_phrase": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})
  
      return res
  
Update 'search_term' functon. Add 'match_phrase' function.
## Code Before:
from django.shortcuts import render
from elasticsearch import Elasticsearch
from django.http import HttpResponse

def search(request):
    if request.method == 'GET':
        term = request.GET.get('term_search')

        if term == None:
            term = ""

        response = search_term(term)
        pages = []

        for hit in response['hits']['hits']:
            x = {'source': hit["_source"], 'highlight': hit["highlight"]["text"][0]}
            pages.append(x)

        return render(request, 'view_athena/index.html', {'pages':pages,'term_search':term})

def search_term(term):
    es = Elasticsearch()

    res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match": { "title": "\"" + str(term) + "\"" }},
                                                                         { "match": { "text": "\"" + str(term) + "\"" }},
                                                                         { "match": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})

    return res

## Instruction:
Update 'search_term' functon. Add 'match_phrase' function.
## Code After:
from django.shortcuts import render
from elasticsearch import Elasticsearch
from django.http import HttpResponse

def search(request):
    if request.method == 'GET':
        term = request.GET.get('term_search')

        if term == None:
            term = ""

        response = search_term(term)
        pages = []

        for hit in response['hits']['hits']:
            x = {'source': hit["_source"], 'highlight': hit["highlight"]["text"][0]}
            pages.append(x)

        return render(request, 'view_athena/index.html', {'pages':pages,'term_search':term})

def search_term(term):
    es = Elasticsearch()

    res = es.search(index="athena", body={"query": {"bool": {"should": [ { "match_phrase": { "title": "\"" + str(term) + "\"" }},
                                                                         { "match_phrase": { "text": "\"" + str(term) + "\"" }},
                                                                         { "match_phrase": { "description": "\"" + str(term) + "\"" }}]}},"highlight": {"fields" : {"text" : {}}}})

    return res
