Skip to content

Commit

Permalink
Merge pull request #7160 from readthedocs/fix-project-search
Browse files Browse the repository at this point in the history
Project search: Show original description when there isn't highlight
  • Loading branch information
stsewd authored Jun 4, 2020
2 parents 27549a4 + a0e2a2a commit 16d5823
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
5 changes: 4 additions & 1 deletion readthedocs/search/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from readthedocs.projects.models import Project
from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es


log = logging.getLogger(__name__)


Expand All @@ -32,6 +31,8 @@ def index_project_save(instance, *args, **kwargs):
# Do not index if autosync is disabled globally
if DEDConfig.autosync_enabled():
index_objects_to_es.delay(**kwargs)
else:
log.info('Skipping indexing')


@receiver(pre_delete, sender=Project)
Expand All @@ -47,3 +48,5 @@ def remove_project_delete(instance, *args, **kwargs):
# Don't `delay` this because the objects will be deleted already
if DEDConfig.autosync_enabled():
delete_objects_in_es(**kwargs)
else:
log.info('Skipping indexing')
53 changes: 53 additions & 0 deletions readthedocs/search/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
from django.core.management import call_command
from django.test import TestCase, override_settings
from django_dynamic_fixture import get
from elasticsearch.exceptions import NotFoundError

from readthedocs.projects.models import Project
from readthedocs.search.documents import ProjectDocument


@pytest.mark.search
@override_settings(ELASTICSEARCH_DSL_AUTOSYNC = True)
class TestSignals(TestCase):

def setUp(self):
call_command('search_index', '--delete', '-f')
call_command('search_index', '--create')

def tearDown(self):
super().tearDown()
call_command('search_index', '--delete', '-f')

def test_project_index_is_updated(self):
project = get(
Project,
slug='testing',
name='A testing project',
description='Just another project',
)
id = project.id

# Check initial values
result = ProjectDocument.get(id=id)
self.assertEqual(project.slug, result.slug)
self.assertEqual(project.name, result.name)
self.assertEqual(project.language, result.language)
self.assertEqual(project.description, result.description)

project.name = 'A new name'
project.description = 'Just another cool project'
project.save()

# Check after update
result = ProjectDocument.get(id=id)
self.assertEqual(project.slug, result.slug)
self.assertEqual(project.name, result.name)
self.assertEqual(project.language, result.language)
self.assertEqual(project.description, result.description)

project.delete()
# Check after deletion
with pytest.raises(NotFoundError):
ProjectDocument.get(id=id)
10 changes: 7 additions & 3 deletions readthedocs/templates/search/elastic_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ <h3>
<div class="module-list-wrapper">
<ul>

{% with "100" as MAX_SUBSTRING_LIMIT %}
{% for result in results %}

<li class="module-item search-result-item">
Expand All @@ -174,11 +175,14 @@ <h3>
...{{ fragment|safe }}...
</p>
{% empty %}
<p><em>No description for project.</em></p>
{% if result.description %}
<p>{{ result.description|slice:MAX_SUBSTRING_LIMIT }}</p>
{% else %}
<p><em>No description for project.</em></p>
{% endif %}
{% endfor %}

{% elif 'page' in result.meta.index %}
{% with "100" as MAX_SUBSTRING_LIMIT %}
<a href="{% doc_url result.project|get_project result.version result.full_path %}?highlight={{ query }}">
{{ result.project }} - {% if result.meta.highlight.title %} {{ result.meta.highlight.title.0|safe }} {% else %} {{ result.title }} {% endif %}
</a>
Expand Down Expand Up @@ -235,13 +239,13 @@ <h3>
{% endif %}
{% endif %}
{% endfor %}
{% endwith %}
{% endif %}
</p>
</li>
{% empty %}
<li class="module-item"><span class="quiet">{% trans "No results found." %}</span></li>
{% endfor %}
{% endwith %}
</ul>
</div>
</div>
Expand Down

0 comments on commit 16d5823

Please sign in to comment.