Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Commit

Permalink
Add field to DataField and DataConcept for excluding from search index
Browse files Browse the repository at this point in the history
Signed-off-by: Don Naegely <[email protected]>
  • Loading branch information
naegelyd committed Sep 19, 2014
1 parent 0a6e6be commit c7e32f8
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 4 deletions.
4 changes: 3 additions & 1 deletion avocado/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def get_heuristic_flags(field):
# For strings and booleans, set the enumerable flag by default
# it below the enumerable threshold
# TextFields are typically used for free text
enumerable = is_enumerable(field)
return {
'enumerable': is_enumerable(field),
'enumerable': enumerable,
'indexable': enumerable or is_searchable(field),
}


Expand Down

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions avocado/migrations/0036_initialize_indexable.py

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions avocado/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class DataField(BasePlural, PublishArchiveMixin):
# no full text data.
enumerable = models.BooleanField(default=False)

# Set this field to False if you wish to exclude this field's data from the
# Haystack index.
indexable = models.BooleanField(default=True)

type = models.CharField(max_length=100, blank=True, null=True,
help_text='Logical type of this field. Typically '
'used downstream for defining behavior '
Expand Down Expand Up @@ -736,6 +740,10 @@ class DataConcept(BasePlural, PublishArchiveMixin):
# represents.
sortable = models.BooleanField(default=True)

# Set this field to False if you wish to exclude DataFields associated
# with this concept from the Haystack index.
indexable = models.BooleanField(default=True)

objects = managers.DataConceptManager()

def format(self, *args, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions avocado/templates/search/indexes/avocado/dataconcept_text.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
{{ object.description|default:"" }}
{{ object.keywords|default:"" }}
{% if object.category %}{% include "search/indexes/avocado/datacategory_text.txt" with object=object.category %}{% endif %}
{% for cfield in object.concept_fields.all %}{{ cfield }}{% include "search/indexes/avocado/datafield_text.txt" with object=cfield.field %}
{% endfor %}
{% if object.indexable %}
{% for cfield in object.concept_fields.all %}{{ cfield }}{% include "search/indexes/avocado/datafield_text.txt" with object=cfield.field %}{% endfor %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
{{ object.keywords|default:"" }}
{{ object.model_name }}
{% if object.category %}{% include "search/indexes/avocado/datacategory_text.txt" with object=object.category %}{% endif %}
{% if object.enumerable or object.searchable %}{% for value in object.labels %}{{ value }}
{% if object.indexable %}{% for value in object.labels %}{{ value }}
{% endfor %}{% endif %}
31 changes: 31 additions & 0 deletions tests/cases/search/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@ def setUp(self):
verbosity=0)


class ExcludedIndexSearchTest(SearchTest):
def test_field_search(self):
search = DataField.objects.search

self.assertEqual(len(search('Erick')), 1)

employee = DataField.objects.get_by_natural_key(
'search.employee.first_name')
employee.indexable = False
employee.save()

management.call_command('rebuild_index', interactive=False,
verbosity=0)

self.assertEqual(len(search('Erick')), 0)

def test_concept_search(self):
search = DataConcept.objects.search

self.assertEqual(len(search('Erick')), 1)

employee = DataConcept.objects.get(name='First Name')
employee.indexable = False
employee.save()

management.call_command('rebuild_index', interactive=False,
verbosity=0)

self.assertEqual(len(search('Erick')), 0)


class FieldSearchTest(SearchTest):
def test_empty(self):
self.assertEqual(len(DataField.objects.published()), 12)
Expand Down

0 comments on commit c7e32f8

Please sign in to comment.