From 638bc9a30047ded783d65d4cfe3aef9c64b0686e Mon Sep 17 00:00:00 2001 From: Libi Gelber Date: Wed, 7 Jun 2017 18:58:07 +0300 Subject: [PATCH] Add place to advansed persons search (#182) * Add place to advansed persons search * 180/181 Add place params to general and persons search * Remove usless file --- bhs_api/persons.py | 4 +- bhs_api/v1_endpoints.py | 37 +++++++++++++------ docs/_build/index.html | 10 +++-- .../search/persons_advanced_search_params.md | 2 +- ...sons_advanced_search_params_description.md | 4 +- tests/common.py | 8 ++-- tests/mocks.py | 24 ++++++++++++ tests/test_search.py | 14 +++++++ 8 files changed, 81 insertions(+), 22 deletions(-) diff --git a/bhs_api/persons.py b/bhs_api/persons.py index 35c997a..2017ff7 100644 --- a/bhs_api/persons.py +++ b/bhs_api/persons.py @@ -27,6 +27,7 @@ "pob": None, "pob_t": "exact", "pom": None, "pom_t": "exact", "pod": None, "pod_t": "exact", + "place": None, "place_t": "exact", "yob": None, "yob_t": "exact", "yob_v": None, "yom": None, "yom_t": "exact", "yom_v": None, "yod": None, "yod_t": "exact", "yod_v": None, @@ -41,7 +42,8 @@ ("last", "last_name_lc"), ("pob", "BIRT_PLAC_lc"), ("pom", "MARR_PLAC_lc"), - ("pod", "DEAT_PLAC_lc"),) + ("pod", "DEAT_PLAC_lc"), + ("place", ["BIRT_PLAC_lc", "MARR_PLAC_lc", "DEAT_PLAC_lc"]),) PERSONS_SEARCH_EXACT_PARAMS = (("sex", "gender"), ("treenum", "tree_num")) diff --git a/bhs_api/v1_endpoints.py b/bhs_api/v1_endpoints.py index 1737097..be46eba 100755 --- a/bhs_api/v1_endpoints.py +++ b/bhs_api/v1_endpoints.py @@ -54,6 +54,22 @@ def custom_error(error): app.error_handler_spec[None][i] = custom_error ''' +def get_person_elastic_search_text_param_query(text_type, text_attr, text_value): + if isinstance(text_attr, list): + return {"bool": {"should": [ + get_person_elastic_search_text_param_query(text_type, sub_text_attr, text_value) + for sub_text_attr in text_attr + ]}} + elif text_type == "exact": + return {"term": {text_attr: text_value}} + elif text_type == "like": + return {"match": {text_attr: {"query": text_value, + "fuzziness": "AUTO"}}} + elif text_type == "starts": + return {"prefix": {text_attr: text_value}} + else: + raise Exception("invalid value for {} ({}): {}".format(text_type, text_attr, text_type)) + def es_search(q, size, collection=None, from_=0, sort=None, with_persons=False, **kwargs): if collection: @@ -63,14 +79,19 @@ def es_search(q, size, collection=None, from_=0, sort=None, with_persons=False, # we consider the with_persons to decide whether to include persons collection or not collections = [collection for collection in SEARCHABLE_COLLECTIONS if with_persons or collection != "persons"] + + + fields = ["Header.En^2", "Header.He^2", "UnitText1.En", "UnitText1.He"] default_query = { "query_string": { - "fields": ["Header.En^2", "Header.He^2", "UnitText1.En", "UnitText1.He"], + "fields": fields, "query": q, "default_operator": "and" } } - + for k, v in PERSONS_SEARCH_TEXT_PARAMS_LOWERCASE: + if not isinstance(v, list): + fields.append(v) if collection == "persons": must_queries = [] if q: @@ -99,15 +120,8 @@ def es_search(q, size, collection=None, from_=0, sort=None, with_persons=False, text_value = kwargs[text_param].lower() text_type_param = "{}_t".format(text_param) text_type = kwargs[text_type_param] - if text_type == "exact": - must_queries.append({"term": {text_attr: text_value}}) - elif text_type == "like": - must_queries.append({"match": {text_attr: {"query": text_value, - "fuzziness": "AUTO"}}}) - elif text_type == "starts": - must_queries.append({"prefix": {text_attr: text_value}}) - else: - raise Exception("invalid value for {} ({}): {}".format(text_type, text_attr, text_type)) + must_queries.append(get_person_elastic_search_text_param_query(text_type, text_attr, text_value)) + for exact_param, exact_attr in PERSONS_SEARCH_EXACT_PARAMS: if kwargs[exact_param]: exact_value = kwargs[exact_param] @@ -452,6 +466,7 @@ def general_search(): try: rv = es_search(**parameters) except Exception as e: + logging.exception(e) return humanify({"error": e.message}, 500) for item in rv['hits']['hits']: enrich_item(item['_source'], collection_name=item['_type']) diff --git a/docs/_build/index.html b/docs/_build/index.html index 0091ff7..0108be7 100644 --- a/docs/_build/index.html +++ b/docs/_build/index.html @@ -14,9 +14,9 @@

Example URI

GET& ], "total": 1 // total number of results } - }

Generated by aglio on 16 May 2017