How can I loop all the emails using python

Viewed 63

I have some emails need to check each email exists or not in Profile Index.

Request Payload:

{
  "emails":["jay@gmail.com", "john@gmail.com"],
}

profile(index):

{
   "settings":{
      
   },
   "mappings":{
      "profiles":{
         "properties":{
            "contact_information":{
               "type":"object",
               "properties":{
                  "email":{
                     "type":"text",
                     "similarity":"boolean"
                  },
                  "work_email":{
                     "type":"text",
                     "similarity":"boolean"
                  }
               }
            }
         }
      }
   }
}

This is my method which will take each email and passing those email to the query, If email exists I just need to capture that email passing those email to the response(as Expecting output).

def get_profile(client, sme_email_ids):

for sme_email_id in emails:
    profile = client.search(
        index='profiles', doc_type='profiles',
        scroll='5m', size=100,
        body={
            "query": {
                "bool": {
                    "must": [
                        {
                            "bool": {
                                "should": [
                                    {
                                        "term": {
                                            "contact_information.email.keyword": sme_email_id
                                        }
                                    },
                                    {
                                        "term": {
                                            "contact_information.work_email.keyword": sme_email_id
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        },
    )
    temp = []
    if not profile['hits']['hits']:
        return {"sme_status": [{'email': sme_email_id, 'status':'Email address not Found'}]}
    else:
        profile = profile['hits']['hits'][0]
        return {"sme_status": [{'email': sme_email_id, 'status':'Success'}]}

Here's query. It's working fine but is returning only one email and its status. I want to display both emails and their status as I expected.

Actual O/p:

 {
          "email_status": [
            {
              "email": "jay@gmail.com",
              "status": "Success"
            },
          ]
        }

Expecting o/p:

    {
      "email_status": [
        {
          "email": "jay@gmail.com",
          "status": "Success"
        },
        {
          "email": "john@gmail.com",
          "status": "Email Address Not Found"
        }
      ]
    }
        
0 Answers
Related