Reputation: 806
models.py
class job(models.Model):
job_title = models.CharField(max_length=256)
def __str__(self):
return self.job_title
documents.py
from django_elasticsearch_dsl import DocType, Index
# from seeker.models import AppliedJobs
from employer.models import PostJob
# from access.models import SeekerRegister
jobs = Index('jobs')
@jobs.doc_type
class AppliedJobsDocument(DocType):
class Meta:
model = PostJob
fields = [
'job_title',
]
views.py
from .documents import AppliedJobsDocument
from django.http import JsonResponse
def search_applied_jobs(request):
q = request.GET.get('q')
print(q)
jobs = AppliedJobsDocument.search().query("match", title=q)
lst=[]
dict ={}
for i in jobs:
print(i)
return JsonResponse(lst,safe=False)
settings.py:
ELASTICSEARCH_DSL = {
'default': {
'hosts': 'localhost:9200'
},
}
i have added 'django_elasticsearch_dsl' in the APP also
i am trying to use elastic search with django and above is my codes. i my database there is records but i am getting empty data when i am trying to print it in my console.
i have installed java and elastic search is coming in port localhost:9200 also
please have a look into my code.
Upvotes: 1
Views: 288
Reputation: 1645
You need to first index your data from database into elasticsearch. Then you will be able to execute queries on elasticsearch.
Upvotes: 1