Пример #1
0
def prepare_search_terms(request_data):
    fields = [
        request_data["search_text"], request_data["filter"]["country_code"],
        request_data["filter"]["state_code"]
    ]

    return [
        es_sanitize(field).upper() if isinstance(field, str) else field
        for field in fields
    ]
Пример #2
0
def create_es_search(scope, search_text, country=None, state=None):
    """
        Providing the parameters, create a dictionary representing the bool-query conditional clauses for elasticsearch

        Args:
            scope: which city field was chosen for searching `pop` (place of performance) or `recipient_location`
            search_text: the text the user is typing in and sent to the backend
            country: optional country selected by user
            state: optional state selected by user
    """
    # The base query that will do a wildcard term-level query
    query = {"must": [{"wildcard": {"{}_city_name.keyword".format(scope): search_text + "*"}}]}
    if country != "USA":
        # A non-USA selected country
        if country != ALL_FOREIGN_COUNTRIES:
            query["must"].append({"match": {"{scope}_country_code".format(scope=scope): country}})
        # Create a "Should Not" query with a nested bool, to get everything non-USA
        query["should"] = [
            {
                "bool": {
                    "must": {"exists": {"field": "{}_country_code".format(scope)}},
                    "must_not": [
                        {"match": {"{}_country_code".format(scope): "USA"}},
                        {"match_phrase": {"{}_country_code".format(scope): "UNITED STATES"}},
                    ],
                }
            }
        ]

        query["minimum_should_match"] = 1
    else:
        # USA is selected as country
        query["should"] = [build_country_match(scope, "USA"), build_country_match(scope, "UNITED STATES")]
        query["should"].append({"bool": {"must_not": {"exists": {"field": "{}_country_code".format(scope)}}}})
        query["minimum_should_match"] = 1
        # null country codes are being considered as USA country codes

    if state:
        # If a state was provided, include it in the filter to limit hits
        query["must"].append({"match": {"{}_state_code".format(scope): es_sanitize(state).upper()}})

    return query
Пример #3
0
def test_sanitizer():
    test_string = '+-&|!()[]{}^~*?:"/<>\\'
    processed_string = es_sanitize(test_string)
    assert processed_string == ''