Example #1
0
def test_es_sanitize():
    test_string = '+|()[]{}?"<>\\'
    processed_string = es_sanitize(test_string)
    assert processed_string == ""
    test_string = "!-^~/&:*"
    processed_string = es_sanitize(test_string)
    assert processed_string == r"\!\-\^\~\/\&\:\*"
Example #2
0
    def generate_elasticsearch_query(cls,
                                     filter_values: List[str],
                                     query_type: _QueryType,
                                     nested_path: str = "") -> ES_Q:
        keyword_queries = []
        fields = [
            "recipient_name",
            "naics_description",
            "product_or_service_description",
            "transaction_description",
            "piid",
            "fain",
            "uri",
            "recipient_unique_id",
            "parent_recipient_unique_id",
            "description",
        ]
        for v in filter_values:
            query = es_sanitize(v) + "*"
            if "\\" in es_sanitize(v):
                query = es_sanitize(v) + r"\*"
            keyword_queries.append(
                ES_Q("query_string",
                     query=query,
                     default_operator="AND",
                     fields=fields))

        return ES_Q("dis_max", queries=keyword_queries)
Example #3
0
    def generate_elasticsearch_query(cls,
                                     filter_values: List[str],
                                     query_type: _QueryType,
                                     nested_path: str = "") -> ES_Q:
        recipient_search_query = []
        fields = ["recipient_name"]

        for v in filter_values:
            upper_recipient_string = es_sanitize(v.upper())
            query = es_sanitize(upper_recipient_string) + "*"
            if "\\" in es_sanitize(upper_recipient_string):
                query = es_sanitize(upper_recipient_string) + r"\*"
            recipient_name_query = ES_Q("query_string",
                                        query=query,
                                        default_operator="AND",
                                        fields=fields)

            if len(upper_recipient_string
                   ) == 9 and upper_recipient_string[:5].isnumeric():
                recipient_duns_query = ES_Q(
                    "match", recipient_unique_id=upper_recipient_string)
                recipient_search_query.append(
                    ES_Q("dis_max",
                         queries=[recipient_name_query, recipient_duns_query]))
            else:
                recipient_search_query.append(recipient_name_query)

        return ES_Q("bool",
                    should=recipient_search_query,
                    minimum_should_match=1)
def test_es_sanitize():
    test_string = '+&|()[]{}*?:"<>\\'
    processed_string = es_sanitize(test_string)
    assert processed_string == ""
    test_string = "!-^~/"
    processed_string = es_sanitize(test_string)
    assert processed_string == r"\!\-\^\~\/"
Example #5
0
    def generate_elasticsearch_query(cls, filter_values: List[str],
                                     query_type: _QueryType) -> ES_Q:
        award_ids_query = []

        for v in filter_values:
            if v and v.startswith('"') and v.endswith('"'):
                v = v[1:-1]
                award_ids_query.append(
                    ES_Q("term", display_award_id={"value": es_sanitize(v)}))
            else:
                v = es_sanitize(v)
                v = " +".join(v.split())
                award_ids_query.append(
                    ES_Q("regexp", display_award_id={"value": v}))

        return ES_Q("bool", should=award_ids_query, minimum_should_match=1)
Example #6
0
 def generate_elasticsearch_query(cls, filter_values: dict,
                                  query_type: _QueryType) -> ES_Q:
     query_text = es_sanitize(filter_values["text"])
     query_fields = filter_values["fields"]
     return ES_Q("multi_match",
                 query=query_text,
                 type="phrase_prefix",
                 fields=query_fields)
    def generate_elasticsearch_query(cls, filter_values: List[dict],
                                     query_type: _QueryType) -> ES_Q:
        award_ids_query = []

        for v in filter_values:
            award_ids_query.append(
                ES_Q("match", display_award_id=es_sanitize(v)))

        return ES_Q("bool", should=award_ids_query, minimum_should_match=1)
Example #8
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
    ]
Example #9
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()}})

    search = TransactionSearch().filter(ES_Q("bool", **query))
    return search
def test_es_sanitize():
    test_string = '+-&|!()[]{}^~*?:"/<>\\'
    processed_string = es_sanitize(test_string)
    assert processed_string == ""