def getCandidateScore(reqId, isSubmitted, masterSupplierId, minScore):
   try:
      start_time = datetime.now()
      beginTime = datetime.now().isoformat()

      inputObj = InputParamsObj(reqId, isSubmitted, masterSupplierId, minScore)
      req = db.requisition.find_one( { "requisition_id" : int(reqId) } )
      if req is None:
         summaryObj = ZcSummaryObj(-1, False, "Invalid Requisition Id")
         return(summaryObj.toJson())


      ### Search for all submitted candidates
      if (int(reqId) > 0 and isSubmitted and int(masterSupplierId) <= 0):
          query_dict = {
                          "requisition_id" : int(reqId),
                          "dnr_flag" : False
                       }
          candidateList = db.requisition_candidate.find( query_dict, { "loaded_date" : 0, "updated_date" : 0, "dnr_flag" : 0, "is_hired" : 0, "etl_process_flag" :0, "_id" : 0 } )
          appendReq = False
          mspFlag = False
          
      ### Search submitted candidates with the provided master supplier id
      elif int(reqId) > 0 and isSubmitted and int(masterSupplierId) > 0:
          query_dict = {
              "requisition_id" : int(reqId),
              "dnr_flag" : False,
              "master_supplier_id" : int(masterSupplierId)
          }
          candidateList = db.requisition_candidate.find( query_dict, { "loaded_date" : 0, "updated_date" : 0, "dnr_flag" : 0, "is_hired" : 0, "etl_process_flag" :0, "_id" : 0 } )
          appendReq = False
          mspFlag = False

      #print(reqId, isSubmitted, masterSupplierId, minScore)

      ### Search all candidates with the provided master supplier id whether submitted or not.
      elif int(reqId) > 0 and (not isSubmitted) and int(masterSupplierId) > 0:
         print(reqId, isSubmitted, masterSupplierId, minScore)
         clientId = req["client_id"]
         exclude_list = {
                "_id": 0,
                "address1": 0,
                "address2": 0,
                "allow_talent_cloud_search_for_all_division": 0,
                "opt_in_talent_search": 0,
                "unique_resource_id": 0,
                "candidate_divisions": 0,
                "update_date" : 0,
                "dnrs": 0,
                "job_skill_names.job_skill_id" : 0,
                "job_skill_names.competency_years_experience" : 0,
                "job_skill_names.competency_level" : 0,
                "job_certificate_names.job_certification_id" : 0,
                "etl_process_flag" : 0
         }
         candidateList = db.candidate.find( { "dnrs.client_id" : {"$ne" : clientId}, "master_supplier_id" : int(masterSupplierId) }, exclude_list )
         appendReq = True
         mspFlag = True

      else:
         summaryObj = ZcSummaryObj(0, True, "Invalid search parameters received")
         candidateList = None

      #logger.log(candidateList)
      cnt = 0 
      if (candidateList is not None):
         for c in candidateList:
            cnt += 1
      ### Now, if we have a condidate list, then process them with scoring
      if (cnt > 0):
         zindexScores = list(candidateScorer.scoreCandidates(req, candidateList, appendReq, score_collection_name, mspFlag, searchAndScoreFlag))
         #logger.log(str(zindexScores))
         ### Now, remove from the list who scored below minScore specified
         if minScore is not None and minScore is not "":
            zindexScoresMinScore = []
            for candidate in zindexScores:
               if candidate["zindex_score"] >=  int(minScore):
                  zindexScoresMinScore.append(candidate)
            zindexScores = zindexScoresMinScore

         ### Now, get the summary for requisition search
         summary = getZindexSummary(db, reqId, zindexScores, searchAndScoreFlag)
         
         ### Add input parameters to the results
         for zindex in zindexScores:
            zindex["input_is_submitted"] = int(isSubmitted) 
            zindex["input_master_supplier_id"] = int(masterSupplierId)
            zindex["input_min_score"] = int(minScore)

         summary["input_is_submitted"] = int(isSubmitted) 
         summary["input_master_supplier_id"] = int(masterSupplierId)
         summary["input_min_score"] = int(minScore)

         ### Finally write the results to the DB
         clean_zindex_scores = cleanScores(zindexScores)
         summaryDocId = writeCandidateScoreToSqlServer(int(reqId), int(isSubmitted), int(masterSupplierId), clean_zindex_scores, summary)
         if summaryDocId > 0:
            summaryObj = ZcSummaryObj(summaryDocId, True, "Search request completed successfully")
         else:
            summaryObj = ZcSummaryObj(summaryDocId, False, "Error in SQL Server write function")

         auditWriteResult = writeScoresToAudit(db, zindexScores, summary, inputObj, start_time)
      else:
         summaryObj = ZcSummaryObj(0, True, "No candidates found with the specified search criteria")
      
      print("%s" % summaryObj.toJson())

   except Exception as e:
      DebugException(e)
      summaryObj = ZcSummaryObj(-1, False, e)
      print("%s" % summaryObj.toJson())
      raise

   elapsed = datetime.now() - start_time
   endTime = datetime.now().isoformat()
   logger.log("[DebugInfo] -- [CandidateScore] Process Start [" + beginTime + "]  End [" + endTime + "]   Elapsed [" + str(elapsed) + "] seconds")

   return (summaryObj.toJson())
def getSearchAndScore(reqId, minScore):
	start_time = datetime.now()
	beginTime = datetime.now().isoformat()

	inputObj = InputParamsObj(int(reqId), None, None, int(minScore))

	scored_candidate_list = []
	relevant_vendor_ids = []
	try:
		reqObj = getRequisitionObject(int(reqId))
		if reqObj is None:
			summaryObj = ZcSummaryObj(-1, False, "Invalid Requisition Id")
			return(summaryObj.toJson())

		client_id = reqObj["client_id"]
		category_id = reqObj["new_global_job_category_id"]
		relevant_candidate_ids = getRelevantCandidates(category_id)

		#if relevant_candidate_ids is None or len(relevant_candidate_ids) == 0:
			#summaryObj = ZcSummaryObj(0, True, "No candidates found under relevant job classification")
			#return(summaryObj.toJson())	

		#queryParams = QueryParams(client_id)
		#relevant_vendor_ids = getRelevantVendorIds(queryParams)
		#candidate_list = getCandidateList(queryParams, relevant_vendor_ids, category_id, relevant_candidate_ids)

		### Get prefiltered candidates from SQL Server
		#preFilterCandidateList = getPreFilterCandidateList(client_id)
		preFilterCandidateList = mongoPreFilter(client_id)

		### Get submitted candidates list from requisition_candidate collection
		submittedCandidateList = getSubmittedCandidates(reqId)

		### Convert the candidate lists into sets
		relevantCandidateSet = set(relevant_candidate_ids)
		preFilterCandidateSet = set(preFilterCandidateList)

		### get the intersection list of the two lists
		filteredCandidateList = list(preFilterCandidateSet.intersection(relevantCandidateSet) )

		### Finally, add the submitted candidate list to the filtered list
		finalCandidateList = filteredCandidateList + submittedCandidateList

		### Get the candidates info from candidate collection using the finalCandidateList
		candidate_list = getCandidateList2(finalCandidateList)

		if candidate_list is None:
			summaryObj = ZcSummaryObj(0, True, "No candidates found with the specified search criteria")
			return(summaryObj.toJson())	

		scored_candidate_list = candidateScorer.scoreCandidates(reqObj, candidate_list, True, score_collection_name, mspFlag, searchAndScoreFlag)

		if scored_candidate_list is not None and minScore is not None and minScore is not "":
			scored_list_minScore = []
			for candidate in scored_candidate_list:
				if candidate["zindex_score"] >= int(minScore):
					scored_list_minScore.append(candidate)
			scored_candidate_list = scored_list_minScore

		if scored_candidate_list is not None:
			summary = getZindexSummary(db, int(reqId), scored_candidate_list, searchAndScoreFlag)
			db.candidate_zindex_score.delete_many({"requisition_id": int(reqId)})
			db.candidate_zindex_score.insert_many(scored_candidate_list)

			db.candidate_zindex_score_summary.replace_one( {"requisition_id" : int(reqId)}, summary, True )
			summaryDoc = db.candidate_zindex_score_summary.find_one( {"requisition_id" : int(reqId)}, { "_id" : 1} )
			summaryDocId = str((summaryDoc["_id"]))
			summaryObj = ZcSummaryObj(summaryDocId, True, "Search request completed successfully")
			auditWriteResult = writeScoresToAudit(db, scored_candidate_list, summary, inputObj, start_time)
		else:
			summaryObj = ZcSummaryObj(0, True, "No candidates found with the specified search criteria")
			return(summaryObj.toJson())	

	except Exception as e:
		DebugException(e)
		summaryObj = ZcSummaryObj(-1, False, e)
		raise

	elapsed = datetime.now() - start_time
	endTime = datetime.now().isoformat()
	print("---Total Time: %s seconds ---" % (datetime.now() - start_time))
	logger.log("[DebugInfo] -- [SearchAndScore] RequisitionId [" + str(reqId) + "]   Process Start [" + beginTime + "]  End [" + endTime + "]   Elapsed [" + str(elapsed) + "] seconds")
	return (summaryObj.toJson())