Skip to content

Commit

Permalink
search queries added.
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaafshar79 committed Jun 11, 2020
1 parent 74c343b commit 224b235
Showing 1 changed file with 155 additions and 24 deletions.
179 changes: 155 additions & 24 deletions create_elastic_index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {
"pycharm": {
"is_executing": false
Expand All @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"pycharm": {
"is_executing": false,
Expand Down Expand Up @@ -193,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 106,
"metadata": {
"pycharm": {
"is_executing": false,
Expand All @@ -202,31 +202,68 @@
},
"outputs": [],
"source": [
"indexed_ids = elastic.search(body={ \n",
" \"query\" : { \n",
" \"match_all\" : {} \n",
" },\n",
" \"fields\": ['name']\n",
"},index='foods')"
"query = \"Mangos\" \n",
"#elastic ingredient search\n",
"search = elastic.search(\n",
" index='ingredients',\n",
" body={'query': {'multi_match': {'query': query, 'fields': ['food_name']}},\n",
" 'from': 0, 'size': 20})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 107,
"metadata": {
"pycharm": {
"is_executing": false,
"name": "#%%\n"
}
},
"outputs": [],
"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}}}]}}"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ids , total = Food.search(\"pasta olive oil\")\n"
"search"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"pycharm": {
"is_executing": false,
Expand All @@ -235,34 +272,128 @@
},
"outputs": [],
"source": [
"ids.all()\n"
"query = \"pasta olive\"\n",
"# search = elastic.search(\n",
"# index='foods_new',\n",
"# body={\n",
"# \"explain\": True,\n",
"# \"query\": {\n",
"# \"bool\": {\n",
"# \"should\": [\n",
"# {\n",
"# \"multi_match\": {\n",
"# \"query\": query,\n",
"# \"fields\": [\n",
"# \"name^4.0\",fr\n",
"# \"category^1.0\",\n",
"# \"description^3.0\"\n",
"# \"tag_cloud^4.0\",\n",
"# \"ingredients^2.0\",\n",
"# \"directions^1.5\",\n",
"# \"author^1.0\"\n",
"# ],\n",
"# \"type\": \"phrase_prefix\",\n",
"# \"lenient\": \"true\"\n",
"# }\n",
"# }\n",
"# ],\n",
"# \"boost\": 1,\n",
"# \"minimum_should_match\": 1\n",
"# }\n",
"# },\n",
"# \"from\": 0,\n",
"# \"size\": 20}\n",
"# )\n",
"q = Q('bool',\n",
"# should=[\n",
"# {\n",
"# \"multi_match\": {\n",
"# \"query\": query,\n",
"# \"fields\": [\n",
"# \"name^6.0\",\n",
"# \"category^1.0\",\n",
"# \"description^3.0\",\n",
"# \"tag_cloud^3.0\",\n",
"# \"ingredients^2.0\",\n",
"# \"directions^1.5\",\n",
"# \"author^1.0\"\n",
"# ],\n",
"# \"type\": \"phrase_prefix\",\n",
"# \"lenient\": \"true\"\n",
"# }\n",
"# },\n",
" \n",
"# ],\n",
" must=[\n",
" {\n",
" \"range\":{\n",
" \"nutrition.calories\":{\n",
" \"gte\":2800\n",
" }\n",
" }\n",
" }\n",
"# ,{\n",
"# \"range\":{\n",
"# \"prep_time\":{\n",
"# \"gte\":15\n",
"# }\n",
"# }\n",
"# }\n",
" ],boost=1,\n",
" minimum_should_match=1)\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"app.elastic.ping()\n"
"from elasticsearch_dsl import Q,Search\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'took': 3,\n",
" 'timed_out': False,\n",
" '_shards': {'total': 16, 'successful': 16, 'skipped': 0, 'failed': 0},\n",
" 'hits': {'total': {'value': 0, 'relation': 'eq'},\n",
" 'max_score': None,\n",
" 'hits': []}}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"indexed_ids = elastic.search(body={ \n",
" \"query\" : { \n",
" \"match_all\" : {} \n",
" },\n",
" \"fields\": ['name']\n",
"},index='foods')"
"s = Search(using=elastic)\n",
"s = s.query(q)\n",
"for idd in [4914 ,2057,2042]:\n",
" s = s.filter('term',**{\n",
" \"ingredient_ids\": idd\n",
" })\n",
"s[0].execute().to_dict()"
]
},
{
Expand Down

0 comments on commit 224b235

Please sign in to comment.