Skip to content

Feature/exclude fields #71

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion django_elasticsearch/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ def get_fields(self):
model_fields = [f.name for f in self.model._meta.fields +
self.model._meta.many_to_many]

return self.model.Elasticsearch.fields or model_fields
ret = self.model.Elasticsearch.fields or model_fields
excludes = self.model.Elasticsearch.exclude_fields
if excludes:
ret = [i for i in ret if i not in excludes]
return ret

def make_mapping(self):
"""
Expand Down
1 change: 1 addition & 0 deletions django_elasticsearch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Elasticsearch:
mapping = None
serializer_class = EsJsonSerializer
fields = None
exclude_fields = None
facets_limit = 10
facets_fields = None
# http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-term.html
Expand Down
18 changes: 18 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ Each EsIndexable model receive an Elasticsearch class that contains its options
Defaults to None
The fields to be indexed by elasticsearch, if left to None, all models fields will be indexed.

* **exclude_fields**
Defaults to None
Exclude those fields to index, if left to None, exclude is disabled.
Example:

```python
MyModel(EsIndexable, models.Model):
useful_for_search = models.CharField(max_length=64)
no_use_for_search = models.CharField(max_length=64)

class Elasticsearch(EsIndexable.Elasticsearch):
fields = ['useful_for_search']
exclude_fields = {'no_use_for_search',} #allow set, list, tuple. set is the best.
```




* **mappings**
Defaults to None
You can override some or all of the fields mapping with this dictionary
Expand Down