python - django elasticsearch 在数据库有记录时给出空记录

标签 python django elasticsearch

模型.py

class job(models.Model):
    job_title                   = models.CharField(max_length=256)

    def __str__(self):
        return self.job_title

文档.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',

        ]

View .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)

设置.py:

ELASTICSEARCH_DSL = {
    'default': {
        'hosts': 'localhost:9200'
    },
}

i have added 'django_elasticsearch_dsl' in the APP also

我正在尝试将 Elasticsearch 与 django 一起使用,上面是我的代码。 我的数据库有记录,但当我尝试打印时却得到空数据 它在我的控制台中。

我已经安装了java,并且elasticsearch也进入了端口localhost:9200

请查看我的代码。

最佳答案

您需要首先将数据库中的数据索引到elasticsearch中。然后您将能够在elasticsearch上执行查询。

关于python - django elasticsearch 在数据库有记录时给出空记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372109/

相关文章:

python - 这是依赖注入(inject)和/或单例模式吗?

python - 从 Python 中的字符串中去除除数字和小数点以外的所有内容的最佳方法

Django静态页面?

caching - 像redis一样使用Elasticsearch作为键值缓存是否有意义

java - Spring-data-elasticsearch 嵌套查询不起作用

python-pptx – 如何在图表中单独隐藏/显示数据标签

python - 从 python 调用 awk

django - 如何在 Windows 中退出 "python manage.py runserver"

javascript - Django:如何让表单字段描述显示在文本/输入框内部而不是外部? (内图)

java - 如何使用 Elasticsearch async api 控制响应