You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on global search which has to search on all the elastic search index and show the response. i'm referring this page for developing API . API
`
class PaginatedElasticSearchAPIView(APIView, LimitOffsetPagination):
serializer_class = None
document_class = None
@abc.abstractmethod
def generate_q_expression(self, query):
"""This method should be overridden
and return a Q() expression."""
blog/documents.py
from django.contrib.auth.models import User
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from blog.models import Category, Article
@registry.register_document
class UserDocument(Document):
class Index:
name = 'users'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = User
fields = [
'id',
'first_name',
'last_name',
'username',
]
`
the above code queries only particular document class.
How to query on the multiple index and get the result similar to the below elastic search api call using django-ealsticsearch-dsl ?
In elastic search we have multi index query
GET /user,categories/_search { "query": { "match": { "user.id": "kim" } } }
It's a tricky question. Multiple indexes are not supported by this library, although they are well supported by Elasticsearch and you could simply write a custom backend to fulfill your needs. That would require some low-level digging/debugging.
Questions
I'm working on global search which has to search on all the elastic search index and show the response. i'm referring this page for developing API . API
`
class PaginatedElasticSearchAPIView(APIView, LimitOffsetPagination):
serializer_class = None
document_class = None
@abc.abstractmethod
def generate_q_expression(self, query):
"""This method should be overridden
and return a Q() expression."""
def get(self, request, query):
try:
q = self.generate_q_expression(query)
search = self.document_class.search().query(q)
response = search.execute()
except Exception as e:
return HttpResponse(e, status=500)
`
In views.py
`
class SearchUsers(PaginatedElasticSearchAPIView):
serializer_class = UserSerializer
document_class = UserDocument
def generate_q_expression(self, query):
return Q('bool',
should=[
Q('match', username=query),
Q('match', first_name=query),
Q('match', last_name=query),
], minimum_should_match=1)
class SearchCategories(PaginatedElasticSearchAPIView):
serializer_class = CategorySerializer
document_class = CategoryDocument
def generate_q_expression(self, query):
return Q(
'multi_match', query=query,
fields=[
'name',
'description',
], fuzziness='auto')
`
The elastic search document
`
blog/documents.py
from django.contrib.auth.models import User
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from blog.models import Category, Article
@registry.register_document
class UserDocument(Document):
class Index:
name = 'users'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = User
fields = [
'id',
'first_name',
'last_name',
'username',
]
`
the above code queries only particular document class.
How to query on the multiple index and get the result similar to the below elastic search api call using django-ealsticsearch-dsl ?
In elastic search we have multi index query
GET /user,categories/_search { "query": { "match": { "user.id": "kim" } } }
or
GET /*/_search { "query": { "match": { "user.id": "kim" } } }
The text was updated successfully, but these errors were encountered: