<commit_before># search/controllers.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-

from candidate.models import CandidateCampaignManager
from config.base import get_environment_variable
from elasticsearch import Elasticsearch
from organization.models import OrganizationManager
from voter.models import fetch_voter_id_from_voter_device_link
import wevote_functions.admin
from wevote_functions.functions import is_voter_device_id_valid, positive_value_exists

logger = wevote_functions.admin.get_logger(__name__)
ELASTIC_SEARCH_CONNECTION_STRING = get_environment_variable("ELASTIC_SEARCH_CONNECTION_STRING")


def search_all_for_api(text_from_search_field, voter_device_id):
    """

    :param text_from_search_field:
    :param voter_device_id:
    :return:
    """
    if not positive_value_exists(text_from_search_field):
        results = {
            'status':                   'TEXT_FROM_SEARCH_FIELD_MISSING',
            'success':                  True,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    if not positive_value_exists(ELASTIC_SEARCH_CONNECTION_STRING):
        results = {
            'status':                   'MISSING_ELASTIC_SEARCH_CONNECTION_STRING',
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    # Get voter_id from the voter_device_id so we can know who is doing the bookmarking
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        results = {
            'status':                   'VALID_VOTER_DEVICE_ID_MISSING',
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    if not positive_value_exists(voter_id):
        results = {
            'status':                   "VALID_VOTER_ID_MISSING",
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    elastic_search_object = Elasticsearch([ELASTIC_SEARCH_CONNECTION_STRING],
                                          timeout=2, max_retries=1, retry_on_timeout=True)

    # query = {"query": {"match": {"candidate_name": text_from_search_field}}}
    # 2016-08-27 No longer searching politician table -- candidate only
    query = {"query": {"multi_match": {"type": "phrase_prefix",
                                       "query": text_from_search_field,
                                       "fields": ["election_name^3", "google_civic_election_id",
                                                  "candidate_name", "candidate_twitter_handle", "twitter_name",
                                                  "measure_subtitle", "measure_text", "measure_title",
                                                  "office_name",
                                                  "party", "organization_name", "organization_twitter_handle",
                                                  "twitter_description", "state_name"],
                                       "slop": 5}},
             "sort": [{"election_day_text": {"order": "desc"}},
                      {"_score": {"order": "desc"}}]}

    # Example of querying ALL indexes
    search_results = []
    search_count = 0
    candidate_manager = CandidateCampaignManager()
    organization_manager = OrganizationManager()
    try:
        res = elastic_search_object.search(body=query)
        # See bottom of this file for example results from Elastic Search

        search_results_elastic_search_raw = res['hits']['hits']

        for hit in search_results_elastic_search_raw:
            one_search_result_type = hit['_type']
            one_search_result_id = hit['_id']<commit_msg>#621 Modification in elastic search query for sort search results based on election date<commit_after># search/controllers.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-

from candidate.models import CandidateCampaignManager
from config.base import get_environment_variable
from elasticsearch import Elasticsearch
from organization.models import OrganizationManager
from voter.models import fetch_voter_id_from_voter_device_link
import wevote_functions.admin
from wevote_functions.functions import is_voter_device_id_valid, positive_value_exists

logger = wevote_functions.admin.get_logger(__name__)
ELASTIC_SEARCH_CONNECTION_STRING = get_environment_variable("ELASTIC_SEARCH_CONNECTION_STRING")


def search_all_for_api(text_from_search_field, voter_device_id):
    """

    :param text_from_search_field:
    :param voter_device_id:
    :return:
    """
    if not positive_value_exists(text_from_search_field):
        results = {
            'status':                   'TEXT_FROM_SEARCH_FIELD_MISSING',
            'success':                  True,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    if not positive_value_exists(ELASTIC_SEARCH_CONNECTION_STRING):
        results = {
            'status':                   'MISSING_ELASTIC_SEARCH_CONNECTION_STRING',
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    # Get voter_id from the voter_device_id so we can know who is doing the bookmarking
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        results = {
            'status':                   'VALID_VOTER_DEVICE_ID_MISSING',
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    if not positive_value_exists(voter_id):
        results = {
            'status':                   "VALID_VOTER_ID_MISSING",
            'success':                  False,
            'text_from_search_field':   text_from_search_field,
            'voter_device_id':          voter_device_id,
            'search_results_found':     False,
            'search_results':           [],
        }
        return results

    elastic_search_object = Elasticsearch([ELASTIC_SEARCH_CONNECTION_STRING],
                                          timeout=2, max_retries=1, retry_on_timeout=True)

    # query = {"query": {"match": {"candidate_name": text_from_search_field}}}
    # 2016-08-27 No longer searching politician table -- candidate only
    query = {"query": {"multi_match": {"type": "phrase_prefix",
                                       "query": text_from_search_field,
                                       "fields": ["election_name^3", "google_civic_election_id",
                                                  "candidate_name", "candidate_twitter_handle", "twitter_name",
                                                  "measure_subtitle", "measure_text", "measure_title",
                                                  "office_name",
                                                  "party", "organization_name", "organization_twitter_handle",
                                                  "twitter_description", "state_name"],
                                       "slop": 5}},
             "sort": [{"election_day_text": {"missing" : "_last", "order": "desc"}},
                      {"_score": {"order": "desc"}}]}

    # Example of querying ALL indexes
    search_results = []
    search_count = 0
    candidate_manager = CandidateCampaignManager()
    organization_manager = OrganizationManager()
    try:
        res = elastic_search_object.search(body=query)
        # See bottom of this file for example results from Elastic Search

        search_results_elastic_search_raw = res['hits']['hits']

        for hit in search_results_elastic_search_raw:
            one_search_result_type = hit['_type']
            one_search_result_id = hit['_id']
{'fraction_non_alphanumeric': 0.08252747252747253, 'fraction_numerical': 0.003406593406593407, 'mean_word_length': 1.992765537652088, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6776084', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2393, 'n_words': 458}