Skip to content

Commit

Permalink
Merge pull request #4408 from safwanrahman/project_search
Browse files Browse the repository at this point in the history
[Fix #4407] Port Project Search for Elasticsearch 6.x
  • Loading branch information
ericholscher committed Jul 24, 2018
2 parents 39ada00 + 7993f80 commit 665cc08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
49 changes: 7 additions & 42 deletions readthedocs/projects/views/public.py
Expand Up @@ -28,6 +28,7 @@
from readthedocs.builds.models import Version
from readthedocs.builds.views import BuildTriggerMixin
from readthedocs.projects.models import ImportedFile, Project
from readthedocs.search.documents import PageDocument
from readthedocs.search.indexes import PageIndex
from readthedocs.search.views import LOG_TEMPLATE

Expand Down Expand Up @@ -205,6 +206,7 @@ def elastic_project_search(request, project_slug):
project = get_object_or_404(queryset, slug=project_slug)
version_slug = request.GET.get('version', LATEST)
query = request.GET.get('q', None)
results = None
if query:
user = ''
if request.user.is_authenticated():
Expand All @@ -220,48 +222,11 @@ def elastic_project_search(request, project_slug):
))

if query:

kwargs = {}
body = {
'query': {
'bool': {
'should': [
{'match': {'title': {'query': query, 'boost': 10}}},
{'match': {'headers': {'query': query, 'boost': 5}}},
{'match': {'content': {'query': query}}},
]
}
},
'highlight': {
'fields': {
'title': {},
'headers': {},
'content': {},
}
},
'fields': ['title', 'project', 'version', 'path'],
'filter': {
'and': [
{'term': {'project': project_slug}},
{'term': {'version': version_slug}},
]
},
'size': 50, # TODO: Support pagination.
}

# Add routing to optimize search by hitting the right shard.
kwargs['routing'] = project_slug

results = PageIndex().search(body, **kwargs)
else:
results = {}

if results:
# pre and post 1.0 compat
for num, hit in enumerate(results['hits']['hits']):
for key, val in list(hit['fields'].items()):
if isinstance(val, list):
results['hits']['hits'][num]['fields'][key] = val[0]
req = PageDocument.simple_search(query=query)
filtered_query = (req.filter('term', project=project.slug)
.filter('term', version=version_slug))
paginated_query = filtered_query[:50]
results = paginated_query.execute()

return render(
request,
Expand Down
12 changes: 7 additions & 5 deletions readthedocs/templates/search/elastic_project_search.html
Expand Up @@ -44,14 +44,16 @@ <h3>{% blocktrans with query=query|default:"" %}Results for {{ query }}{% endblo
<div class="module-list-wrapper">

<ul>
{% for result in results.hits.hits %}
{% for result in results %}
<li class="module-item">
<p class="module-item-title">
<a href="{% doc_url project result.fields.version result.fields.path %}?highlight={{ query }}">{{ result.fields.project }} - {{ result.fields.title|safe }}</a>
</p>
<p>
{{ result.highlight.content.0|safe }}
<a href="{% doc_url project result.version result.path %}?highlight={{ query }}">{{ result.project }} - {{ result.title|safe }}</a>
</p>
{% for fragment in result.meta.highlight.content|slice:":3" %}
<p>
{{ fragment|safe }}
</p>
{% endfor %}
</li>
{% empty %}
<li class="module-item"><span class="quiet">{% trans "No results found. Bummer." %}</span></li>
Expand Down

0 comments on commit 665cc08

Please sign in to comment.