Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext: change/allow passing a "mapping" dictionary to "current_search.create_index" #179

Open
slint opened this issue Jul 29, 2019 · 1 comment

Comments

@slint
Copy link
Member

slint commented Jul 29, 2019

Currently only a mapping_path can be passed to the current_search.create_index method. This is not very flexible since it requires having an actual JSON file with the mapping for creating indices with the correct prefixes/suffixes/write-aliases, etc.

Adding (or changing the current parameter) to accept a dictionary with the full mapping, is more flexible and allows other packages to create mappings.

@mesemus
Copy link

mesemus commented Aug 16, 2020

I second to that. It would also help to have a couple of signals in different phases of processing of mappings.
In my case, the mapping contains a lot of repeated sequences - multilingual strings. I've ended with mapping containing

"title": {
  "type": "#/definitions/multilingual"
}

To replace the multilingual type, I'm using app_loaded and replace mapping paths with temporary files:

def process(base_dir, filename):
    dest_file = os.path.join(base_dir, os.path.basename(filename))
    with open(filename) as f:
        mapping = json.load(f)

    defs = mapping.pop('definitions', {})
    substitute_types(defs, mapping['mappings']['properties'])
    with open(dest_file, 'w') as f:
        json.dump(mapping, f, ensure_ascii=False, indent=4)

    return dest_file

def process_mappings(source, app=None, **kwargs):
    mappings = app.extensions['invenio-search'].mappings

    transformed_mappings_dir = os.path.join(app.instance_path, 'mappings')
    if not os.path.exists(transformed_mappings_dir):
        os.makedirs(transformed_mappings_dir)

    for k, v in list(mappings.items()):
        mappings[k] = process(transformed_mappings_dir, v)

app_loaded.connect(process_mappings)

With @slint suggestion and "mapping loaded" signal this usecase could be implemented without temporary files.

If you want I can prepare a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants