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
我正在尝试对django使用弹性搜索,以上是我的代码。 我的数据库中有记录,但是尝试打印时我得到的数据为空 在我的控制台中。
我已经安装了Java,并且弹性搜索也在端口localhost:9200中进来
请看一下我的代码。
答案 0 :(得分:1)
您需要首先将数据库中的数据索引到elasticsearch中。然后,您将能够在elasticsearch上执行查询。