从elasticsearch返回的嵌套对象中检索特定字段

时间:2012-07-10 16:28:03

标签: django elasticsearch pyes

我是elasticsearch的新手。我试图从elasticsearch返回的嵌套对象中检索选定的字段。下面是存储在elasticsearch索引中的对象:

{
  "_index": "xxx",
  "_type": "user",
  "_id": "2",
  "_version": 1,
  "exists": true,
  "_source": {
    "user": {
      "user_auth": {
        "username": "nickcoolster@gmail.com",
        "first_name": "",
        "last_name": "",
        "is_active": false,
        "_state": {
          "adding": false,
          "db": "default"
        },
        "id": 2,
        "is_superuser": false,
        "is_staff": false,
        "last_login": "2012-07-10 21:11:53",
        "password": "sha1$a6caa$cba2f821678ccddc4d70c8bf0c8e0655ab5c279b",
        "email": "nickcoolster@gmail.com",
        "date_joined": "2012-07-10 21:11:53"
      },
      "user_account": {},
      "user_profile": {
        "username": null,
        "user_id": 2,
        "following_count": 0,
        "sqwag_count": 0,
        "pwd_reset_key": null,
        "_state": {
          "adding": false,
          "db": "default"
        },
        "personal_message": null,
        "followed_by_count": 0,
        "displayname": "nikhil11",
        "fullname": "nikhil11",
        "sqwag_image_url": null,
        "id": 27,
        "sqwag_cover_image_url": null
      }
    }
  }
}

现在我只想从user.user_auth返回某些字段(不应返回密码,超级用户等)。 我正在使用django PyES,下面是我尝试的代码:

fields = ["user.user_auth.username","user.user_auth.first_name","user.user_auth.last_name","user.user_auth.email"]
result = con.search(query=q,indices="xxx",doc_types="user",fields=fields)

但我得到的结果只是检索到电子邮件(i:e只返回最后一个字段):

{
  "user.user_auth.email": "nikhiltyagi.eng@gmail.com"
}

我希望这两个嵌套对象的抽象(i:e user_auth,user_profile)

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用最新django-haystack怎么样?它还将ElasticSearch作为后端,因此您可以将绑定良好的ElasticSearch FTS绑定到您的项目中。