Comments (6)
Update: I actually just got pretty far trashing dupe fields with this:
def get_properties(doctype):
return dict(
id=fields.StringField(),
**doctype()._doc_type._fields(),
)
# ...
fields.ObjectField(properties=get_properties(AssetFileDocument))
An added benefit of using documents here is that you can also rely on the type inference for fields taken from the models, I am most using the Meta fields
list now in place of the properties dict.
from django-elasticsearch-dsl.
Another thought related to above, it would be handy to be able to build ObjectFields out of models using a fields
list – right now I have documents I never intend to index alone, but use as ObjectFields using above get_properties
call – which could be simplified this way.
from django-elasticsearch-dsl.
If you want to feedback on the approach of any of the above @sabricot I'll take a look at implementing and submit a PR.
from django-elasticsearch-dsl.
Slight improvement:
from django_elasticsearch_dsl import fields
class DocumentField(fields.ObjectField):
"""
An ObjectField that inherits its properties from a DocType class
https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html#object
"""
def __init__(self, doctype, *args, **kwargs):
kwargs['properties'] = doctype()._doc_type._fields()
super().__init__(*args, **kwargs)
def get_value_from_instance(self, instance):
"""Store nulls in Elasticsearch instead of empty objects"""
objs = super().get_value_from_instance(instance)
if objs == {}:
return None
return objs
quotes = fields.ListField(DocumentField(NestedPressQuoteDocument))
upload = DocumentField(UploadDocument, attr='get_upload')
Edit: I've updated the above with a more recent working example
from django-elasticsearch-dsl.
I've updated the example above to match what I've been using for a while now, works well
I couple this with some helpers for building out simple DocTypes with less boilerplate
utils.py
from django_elasticsearch_dsl import DocType
def fields_to_index(model, exclude=[]):
"""Returns list of field names to index, given a Django model class"""
return [
field.name for field in model._meta.get_fields()
if field.name not in ['id', *exclude] and field.concrete and not field.is_relation
]
def model_to_doctype(model, bases=(DocType,), fields={}):
"""An extension of the type built-in for creating django_elasticsearch_dsl DocTypes"""
return type(model.__name__ + 'DocType', bases, {
**fields,
'Meta': type('Meta', (), {
'model': model,
'fields': fields_to_index(model, exclude=fields.keys()),
})
})
This can brings a pretty elaborate nested document structure down to under a 40 LOC – I only use full class declarations for the actual (root) documents that are indexed, and all of the DocType's used for DocumentField's are magicked up using these helpers
from django-elasticsearch-dsl.
Thanks @stevelacey,
If you want to make a PR for this it will be with pleasure.
from django-elasticsearch-dsl.
Related Issues (20)
- How to connect Django Backend and Elastic Search under ver 8.8.1? HOT 1
- Multi model single index possiblity HOT 2
- Multi Index Elastic Search HOT 1
- Add knn documentation HOT 2
- range fields support
- Error: Unexpected queryset model
- Showing Progress During Populate or Rebuild Commands HOT 1
- Save Only in Elasticsearch HOT 1
- ModuleNotFoundError: No module named 'MyIndex'
- elasticsearch.helpers.BulkIndexError: 1 document(s) failed to index
- How to add a mapping on the Document class HOT 1
- Cleaning up failed indexes when using `--use-alias`
- Question : Save without indexing, index a list of objects HOT 1
- Shards/Replicas setting defaults to one when populate index command without creating the index first
- failed to index
- I get connection timeout, however ES logs seems okay
- Deprecation Warning for ES v7
- Invalid metadata HOT 2
- Compatibility with django-countries HOT 3
- Data filtering function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-elasticsearch-dsl.