Skip to content

Commit

Permalink
ingredient text search added.
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaafshar79 committed Jun 12, 2020
1 parent e453390 commit f64bc5e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 40 deletions.
6 changes: 4 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class Config(object):
"protein_shake": "https://img.icons8.com/color/7x/protein.png",
"salad": "https://img.icons8.com/color/7x/salad.png",
"pasta": "https://img.icons8.com/color/7x/spaghetti.png",
"other": "https://img.icons8.com/color/7x/cookbook.png"
"other": "https://img.icons8.com/color/7x/cookbook.png",
"ingredient": "https://img.icons8.com/color/7x/grocery-bag.png"
},
"thumbnail": {
"mostly_meat": "https://img.icons8.com/color/2x/steak.png",
Expand All @@ -62,7 +63,8 @@ class Config(object):
"protein_shake": "https://img.icons8.com/color/2x/protein.png",
"salad": "https://img.icons8.com/color/2x/salad.png",
"pasta": "https://img.icons8.com/color/2x/spaghetti.png",
"other": "https://img.icons8.com/color/2x/cookbook.png"
"other": "https://img.icons8.com/color/2x/cookbook.png",
"ingredient":"https://img.icons8.com/color/2x/grocery-bag.png"
}
}

Expand Down
38 changes: 5 additions & 33 deletions create_elastic_index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": 106,
"execution_count": 16,
"metadata": {
"pycharm": {
"is_executing": false,
Expand All @@ -211,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 107,
"execution_count": 17,
"metadata": {
"pycharm": {
"is_executing": false,
Expand All @@ -221,39 +221,11 @@
"outputs": [
{
"data": {
"text/plain": [
"{'took': 2,\n",
" 'timed_out': False,\n",
" '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},\n",
" 'hits': {'total': {'value': 2, 'relation': 'eq'},\n",
" 'max_score': 7.2770195,\n",
" 'hits': [{'_index': 'ingredients',\n",
" '_type': 'ingredient',\n",
" '_id': '164513',\n",
" '_score': 7.2770195,\n",
" '_source': {'food_name': 'Mangos',\n",
" 'id': 164513,\n",
" 'primary_thumbnail': 'https://images.eatthismuch.com/site_media/thmb/164513_simmyras_016d1a64-02af-4910-bfe4-184e12b138a9.png',\n",
" 'nutrition': {'calories': 130.0,\n",
" 'carbs': 32.0,\n",
" 'fats': 0.0,\n",
" 'proteins': 1.0}}},\n",
" {'_index': 'ingredients',\n",
" '_type': 'ingredient',\n",
" '_id': '1448',\n",
" '_score': 7.2770195,\n",
" '_source': {'food_name': 'Mangos',\n",
" 'id': 1448,\n",
" 'primary_thumbnail': 'https://images.eatthismuch.com/site_media/thmb/1448_erin_m_80d7aa63-91c7-4999-9942-d6ebea57bb2a.png',\n",
" 'nutrition': {'calories': 124.2,\n",
" 'carbs': 31.01,\n",
" 'fats': 0.79,\n",
" 'proteins': 1.7}}}]}}"
]
"text/plain": "{'took': 0,\n 'timed_out': False,\n '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},\n 'hits': {'total': {'value': 2, 'relation': 'eq'},\n 'max_score': 7.549581,\n 'hits': [{'_index': 'ingredients',\n '_type': 'ingredient',\n '_id': '164513',\n '_score': 7.549581,\n '_source': {'food_name': 'Mangos',\n 'id': 164513,\n 'primary_thumbnail': 'https://images.eatthismuch.com/site_media/thmb/164513_simmyras_016d1a64-02af-4910-bfe4-184e12b138a9.png',\n 'nutrition': {'calories': 130.0,\n 'carbs': 32.0,\n 'fats': 0.0,\n 'proteins': 1.0}}},\n {'_index': 'ingredients',\n '_type': 'ingredient',\n '_id': '1448',\n '_score': 7.549581,\n '_source': {'food_name': 'Mangos',\n 'id': 1448,\n 'primary_thumbnail': 'https://images.eatthismuch.com/site_media/thmb/1448_erin_m_80d7aa63-91c7-4999-9942-d6ebea57bb2a.png',\n 'nutrition': {'calories': 124.2,\n 'carbs': 31.01,\n 'fats': 0.79,\n 'proteins': 1.7}}}]}}"
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result",
"execution_count": 17
}
],
"source": [
Expand Down
21 changes: 18 additions & 3 deletions foods/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from elasticsearch_dsl import Q, Search


class SearchTimedOutException(Exception):
pass


class SearchableMixin(object):

@classmethod
Expand Down Expand Up @@ -76,9 +80,7 @@ def query_index(cls, query, page=1, per_page=10):
search = search.query(elastic_query)
search_results = search[from_index: from_index + size].execute().to_dict()
if search_results['timed_out']:
return {
"error": "search request timed out."
}, 408
raise SearchTimedOutException()

ids = [int(hit['_id']) for hit in search_results['hits']['hits']]
return ids, search_results['hits']['total']['value']
Expand Down Expand Up @@ -115,6 +117,19 @@ def after_commit(cls, session):
cls.remove_from_index(obj)
session._changes = None

@classmethod
def ingredient_search(cls, expression, page=1, per_page=10):
if elastic is None:
return [], 0
search_results = elastic.search(
index='ingredients',
body={'query': {'multi_match': {'query': expression, 'fields': ['food_name']}},
'from': (page - 1) * per_page, 'size': per_page})
if search_results['timed_out']:
raise SearchTimedOutException()
total = search_results['hits']['total']['value']
return [hit['_source'] for hit in search_results['hits']['hits']], total

@classmethod
def reindex(cls):
for obj in cls.query:
Expand Down
54 changes: 52 additions & 2 deletions foods/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from flask import jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity

from config import Config
from foods import foods
from foods.diet import dovade, sevade, yevade
from foods.utils import (beautify_category, get_foods_with_categories,
set_placeholder)
from utils.decorators import confirmed_only
from users.models import User

from .models import Food, DietRecord
from .models import Food, DietRecord, SearchTimedOutException
from extentions import db



def submit_diet_record(food_ids, jwt_identity):
user = User.query.filter_by(Email=jwt_identity).first()
if user is None:
Expand Down Expand Up @@ -137,14 +139,62 @@ def food_search():
'error': 'per_page should not be more than 50'
}), 422

results, count = Food.search(query, page, per_page)
try:
results, count = Food.search(query, page, per_page)
except SearchTimedOutException as e:
return jsonify({
"error": "search request timed out."
}), 408


return jsonify({
'results': [set_placeholder(result.simple_view) for result in results.all()],
'total_results_count': count
})


@foods.route('/search/ingredient', methods=['GET'])
def ingredient_search():
"""
ingredient full text search using elasticsearch
http parameters:
query: text to search
page: pagination page number
per_page: pagination per_page count
:return:
"""
query = request.args.get('query')
page = int(request.args.get('page', 1))
per_page = int(request.args.get('per_page', 10))

if query == "" or query is None:
return jsonify({
'error': "query should exist in the request"
}), 422 # invalid input error

if per_page > 50:
return jsonify({
'error': 'per_page should not be more than 50'
}), 422

try:
results, count = Food.ingredient_search(query, page, per_page)
except SearchTimedOutException as e:
return jsonify({
"error": "search request timed out."
}), 408

#setting placeholder
for result in results:
if result['primary_thumbnail'] is None:
result['primary_thumbnail'] = Config.PLACEHOLDERS['thumbnail']['ingredient']

return jsonify({
'results': results,
'total_results_count': count
})


@jwt_required
@foods.route('/diets')
def get_diet_records():
Expand Down

0 comments on commit f64bc5e

Please sign in to comment.