From f680c8ce23fab7c79b4fef5d40b09eae9809e17e Mon Sep 17 00:00:00 2001 From: sadnoodles Date: Fri, 19 May 2017 10:52:55 +0800 Subject: [PATCH 1/2] add exclude fields --- django_elasticsearch/managers.py | 6 +++++- django_elasticsearch/models.py | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/django_elasticsearch/managers.py b/django_elasticsearch/managers.py index edf2718..42ad496 100644 --- a/django_elasticsearch/managers.py +++ b/django_elasticsearch/managers.py @@ -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): """ diff --git a/django_elasticsearch/models.py b/django_elasticsearch/models.py index 39ab19f..d975b77 100644 --- a/django_elasticsearch/models.py +++ b/django_elasticsearch/models.py @@ -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 From ced3bbce363d5b2c72cd46568cdca435dcbbaf99 Mon Sep 17 00:00:00 2001 From: sadnoodles Date: Fri, 19 May 2017 12:16:14 +0800 Subject: [PATCH 2/2] Change readme. --- readme.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/readme.md b/readme.md index 55b19ba..7994e28 100644 --- a/readme.md +++ b/readme.md @@ -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