Comments (3)
hello,
I am not sure to understand your problem but if your goal is to index in es your Item Model with its tags the simplest solution is to store the tags as elastisearch object which lead to something like this.
models.py
class Tag(models.Model):
name = models.CharField("Name", max_length=5000, blank=True)
taglevel = models.IntegerField("Tag level", null=True, blank=True)
class Item(models.Model):
title = models.CharField("Title", max_length=10000, blank=True)
tag = models.ManyToManyField('Tag', blank=True)
def tags(self):
return self.tag.all()
documents.py
item = Index('test_item')
item.settings(
number_of_shards=1,
number_of_replicas=0
)
@item.doc_type
class ItemDocument(DocType):
tags = fields.ObjectField(properties={
'name': fields.StringField(),
'taglevel': fields.IntegerField(),
})
class Meta:
model = Item
fields = [
'title',
]
With this, your items will be indexed at each save on the django Item model.
And for example you can query elasticsearch like this:
results = ItemDocument.search().filter('term', **{'tags.taglevel': 4}
for doc in results:
print(doc.title, doc.tags)
To get only items that have a tag with a taglevel of 4.
from django-elasticsearch-dsl.
Thanks but I'm not following how to implement that code.
I created a file called "documents.py" and added import models
to it.
But I do not know where the following belongs:
results = ItemDocument.search().filter('term', **{'tags.taglevel': 4}
for doc in results:
print(doc.title, doc.tags)
Can you please advise? Also- I want it to return all levels of tags
from django-elasticsearch-dsl.
I found the solution- it was to use signals.
m2m_changed.connect(update_search, sender=Item.tag.through)
from django-elasticsearch-dsl.
Related Issues (20)
- NestedField does not generate a nested mapping HOT 2
- Silencing of `TypeError`s during value retrieval
- Make the search_index command resumable
- 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
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.