def add_search_string(self: Any, param: str) -> Any:
     self.query["dis_max"]["queries"].append(
         query_utils.title_exact_match_query(
             fields=title_fields(
                 [IndicesKey.EVENTS, IndicesKey.PUBLIC_SERVICES]),
             search_string=param,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.title_query(
             fields=title_fields(
                 [IndicesKey.EVENTS, IndicesKey.PUBLIC_SERVICES]),
             search_string=param,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.organization_and_keyword_query(param))
     self.query["dis_max"]["queries"].append(
         query_utils.description_query(
             fields=description_fields(
                 [IndicesKey.EVENTS, IndicesKey.PUBLIC_SERVICES]),
             search_string=param,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.simple_query_string(
             fields=fulltext_fields(
                 [IndicesKey.EVENTS, IndicesKey.PUBLIC_SERVICES]),
             search_string=param,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.query_string(
             fields=fulltext_fields(
                 [IndicesKey.EVENTS, IndicesKey.PUBLIC_SERVICES]),
             search_string=param,
         ))
 def add_search_string(self: Any, search_string: str) -> Any:
     self.query["dis_max"]["queries"].append(
         query_utils.title_exact_match_query(fields=title_fields(
             [IndicesKey.CONCEPTS]),
                                             search_string=search_string))
     self.query["dis_max"]["queries"].append(
         query_utils.title_query(
             fields=title_fields([IndicesKey.CONCEPTS]),
             search_string=search_string,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.organization_and_keyword_query(search_string))
     self.query["dis_max"]["queries"].append(
         query_utils.description_query(
             fields=description_fields([IndicesKey.CONCEPTS]),
             search_string=search_string,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.simple_query_string(
             fields=fulltext_fields([IndicesKey.CONCEPTS]),
             search_string=search_string,
         ))
     self.query["dis_max"]["queries"].append(
         query_utils.query_string(
             fields=fulltext_fields([IndicesKey.CONCEPTS]),
             search_string=search_string,
         ))
def test_words_only_string_in_title_query():
    expected = {
        "query_string": {
            "query": "*some* *query* *to* *be* *queried*",
            "fields": ["title.raw"],
        }
    }

    expected_one_word = {
        "query_string": {
            "query": "*nothing*",
            "fields": ["title.raw"],
        }
    }

    title_fields = ["title"]

    string_with_special_char = title_query(
        fields=title_fields, search_string="]some query to be queried [")
    string_with_special_char2 = title_query(
        fields=title_fields, search_string="some - query to be ()queried")
    string_with_special_char3 = title_query(
        fields=title_fields,
        search_string="+some + + query to be ! queried?   ",
    )
    string_with_special_char4 = title_query(
        fields=title_fields,
        search_string="+some + + -query to be queried !    ",
    )
    string_with_four_words = title_query(
        fields=title_fields, search_string="some query to be queried")
    string_with_one_word = title_query(fields=title_fields,
                                       search_string="nothing")
    string_with_one_word_and_special_char = title_query(
        fields=title_fields, search_string="nothing-")
    assert json.dumps(string_with_special_char["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected)
    assert json.dumps(string_with_special_char2["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected)
    assert json.dumps(string_with_special_char3["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected)
    assert json.dumps(string_with_special_char4["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected)
    assert json.dumps(string_with_four_words["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected)
    assert json.dumps(string_with_one_word["bool"]["must"]["dis_max"]
                      ["queries"][1]) == json.dumps(expected_one_word)
    assert json.dumps(
        string_with_one_word_and_special_char["bool"]["must"]["dis_max"]
        ["queries"][1]) == json.dumps(expected_one_word)
def test_match_in_title_info_model():
    expected = {
        "bool": {
            "must": {
                "dis_max": {
                    "queries": [
                        {
                            "multi_match": {
                                "query":
                                "RA-05 string",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.en.ngrams",
                                    "title.en.ngrams.2_gram",
                                    "title.en.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "multi_match": {
                                "query":
                                "RA-05 string",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.nb.ngrams",
                                    "title.nb.ngrams.2_gram",
                                    "title.nb.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "multi_match": {
                                "query":
                                "RA-05 string",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.nn.ngrams",
                                    "title.nn.ngrams.2_gram",
                                    "title.nn.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "multi_match": {
                                "query":
                                "RA-05 string",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.no.ngrams",
                                    "title.no.ngrams.2_gram",
                                    "title.no.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "query_string": {
                                "query":
                                "*RA* *05* *string*",
                                "fields": [
                                    "title.en.raw",
                                    "title.nb.raw",
                                    "title.nn.raw",
                                    "title.no.raw",
                                ],
                            }
                        },
                    ],
                }
            },
            "should": [
                {
                    "match": {
                        "provenance.code": "NASJONAL"
                    }
                },
                {
                    "term": {
                        "nationalComponent": "true"
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "accessRights.code.keyword": "PUBLIC"
                                }
                            },
                            {
                                "term": {
                                    "distribution.openLicense": "true"
                                }
                            },
                        ]
                    }
                },
            ],
            "boost":
            10,
        }
    }
    result = title_query(fields=title_fields([IndicesKey.INFO_MODEL]),
                         search_string="RA-05 string")
    assert json.dumps(result) == json.dumps(expected)
def test_title_query():
    expected = {
        "bool": {
            "must": {
                "dis_max": {
                    "queries": [
                        {
                            "multi_match": {
                                "query":
                                "åpne data",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.*.ngrams",
                                    "title.*.ngrams.2_gram",
                                    "title.*.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "multi_match": {
                                "query":
                                "åpne data",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "title.ngrams",
                                    "title.ngrams.2_gram",
                                    "title.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "multi_match": {
                                "query":
                                "åpne data",
                                "type":
                                "bool_prefix",
                                "fields": [
                                    "prefLabel.*.ngrams",
                                    "prefLabel.*.ngrams.2_gram",
                                    "prefLabel.*.ngrams.3_gram",
                                ],
                            }
                        },
                        {
                            "query_string": {
                                "query":
                                "*åpne* *data*",
                                "fields": [
                                    "title.*.raw",
                                    "title.raw",
                                    "prefLabel.*.raw",
                                ],
                            }
                        },
                    ],
                }
            },
            "should": [
                {
                    "match": {
                        "provenance.code": "NASJONAL"
                    }
                },
                {
                    "term": {
                        "nationalComponent": "true"
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "accessRights.code.keyword": "PUBLIC"
                                }
                            },
                            {
                                "term": {
                                    "distribution.openLicense": "true"
                                }
                            },
                        ]
                    }
                },
            ],
            "boost":
            10,
        }
    }

    result = title_query(fields=["title.*", "title", "prefLabel.*"],
                         search_string="åpne data")
    assert json.dumps(result) == json.dumps(expected)