Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

puts searchresults objects in elasticsearch/ makes search GA #6340

Merged
merged 5 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,5 @@ def callback(request):
AVATAR_ADDRESS = env('AVATAR_ADDRESS', default='0x00De4B13153673BCAE2616b67bf822500d325Fc3')
AVATAR_PRIVATE_KEY = env('AVATAR_PRIVATE_KEY', default='0x00De4B13153673BCAE2616b67bf822500d325Fc3')


ELASTIC_SEARCH_URL = env('ELASTIC_SEARCH_URL', default='')
4 changes: 1 addition & 3 deletions app/dashboard/templates/shared/nav_auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
{% load i18n static %}
{% load kudos_extras %}
<input id="is-authenticated" type="hidden" value={{ user.is_authenticated }}>
{% if is_alpha_tester %}
<div id=search_container class=""></div>
{% endif %}
<div id=search_container class=""></div>
{% if user.is_authenticated %}
<div class="nav-item dropdown interior">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
Expand Down
Empty file.
Empty file.
30 changes: 30 additions & 0 deletions app/search/management/commands/create_search_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Copyright (C) 2019 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

'''
from django.core.management.base import BaseCommand

from search.models import SearchResult


class Command(BaseCommand):

help = 'uploads search results into elasticsearch'

def handle(self, *args, **options):
for sr in SearchResult.objects.all():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing thats actually really cool about Django querysets is that it is smart enough to not load all of these objects into memory if you back it with postgres: https://docs.djangoproject.com/en/dev/ref/models/querysets/#iterator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, so sounds like we should use .iterator() here to explicitly do this

print(sr.pk)
sr.put_on_elasticsearch()
37 changes: 37 additions & 0 deletions app/search/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils import timezone

from economy.models import SuperModel
from elasticsearch import Elasticsearch


class SearchResult(SuperModel):
Expand All @@ -21,6 +24,40 @@ def __str__(self):
return f"{self.source_type}; {self.url}"


def put_on_elasticsearch(self):
if self.visible_to:
return None

es = Elasticsearch([settings.ELASTIC_SEARCH_URL])
source_type = str(str(self.source_type).replace('token', 'kudos')).title()
full_search = f"{self.title}{self.description}{source_type}"
doc = {
'title': self.title,
'description': self.description,
'full_search': full_search,
'url': self.url,
'pk': self.pk,
'img_url': self.img_url,
'timestamp': timezone.now(),
'source_type': source_type,
}
res = es.index(index="search-index", id=self.pk, body=doc)


def search(query):
if not settings.ELASTIC_SEARCH_URL:
return {}
es = Elasticsearch([settings.ELASTIC_SEARCH_URL])
res = es.search(index="search-index", body={
"query": {
"match": {
"full_search": query,
}
}
})
return res


class ProgrammingLanguage(SuperModel):
"""Records ProgrammingLanguage - the list for programming langauges"""
val = models.CharField(max_length=255, default='')
Expand Down
12 changes: 11 additions & 1 deletion app/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
from ratelimit.decorators import ratelimit
from retail.helpers import get_ip

from .models import SearchResult
from .models import SearchResult, search


@ratelimit(key='ip', rate='30/m', method=ratelimit.UNSAFE, block=True)
def search(request):
keyword = request.GET.get('term', '')

# attempt elasticsearch first
try:
all_result_sets = search(keyword)
return_results = [ele['_source'] for ele in all_result_sets['hits']['hits']]
mimetype = 'application/json'
return HttpResponse(json.dumps(return_results), mimetype)
except Exception as e:
print(e)

all_result_sets = [SearchResult.objects.filter(title__icontains=keyword), SearchResult.objects.filter(description__icontains=keyword)]
return_results = []
exclude_pks = []
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ redis==3.3.11
pandas
wiki
django-bulk-update
elasticsearch

1 change: 1 addition & 0 deletions scripts/crontab
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us
00 10 * * 5 cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash post_leaderboard_to_slack >> /var/log/gitcoin/post_leaderboard_to_slack.log 2>&1
0 9 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash no_applicants_email >> /var/log/gitcoin/no_applicants_email.log 2>&1
0 10 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash pull_grant_twitter_info >> /var/log/gitcoin/pull_grant_twitter_info.log 2>&1
0 11 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash create_search_index >> /var/log/gitcoin/create_search_index.log 2>&1


## PERFTOOLS
Expand Down