In this section, we'll create a view that will let users search our Question
s and will display the matching results. To achieve this result, we will do the following things:
- Add a
search_for_question()
function to ourelasticsearch
service - Make a search view
- Make a template to display search results
- Update the base template to have search available everywhere
Let's start by adding search to our elasticsearch
service.
The responsibility for querying our Elasticsearch server for a list of questions matching the user's query lies with our elasticsearch
service.
Let's add a function that will send a search query and parse the results to django/qanda/service/elasticsearch.py
:
def search_for_questions(query): client = get_client() result = client.search(index=settings.ES_INDEX, body={ 'query': { 'match': { 'text': query, }, }, }) return (h['_source'] for h in result['hits']['hits'])
After we connect...