inveniosoftware / invenio-records-rest

Invenio records REST API module.
https://invenio-records-rest.readthedocs.io
MIT License
4 stars 63 forks source link

docs: document customisation of search factory and query parser #237

Closed PSvishnu93 closed 5 years ago

PSvishnu93 commented 5 years ago

Is it possible to modify the querystring from the flask api? For example if the incoming request is http://localhost:5001/records/?q=abc%20cde I want to update the querystring value from "abc cde" to "*abc* *cde*"

lnielsen commented 5 years ago

That's best done by customizing the search factory which you configure here

# my-site/config.py
from invenio_records_rest.query import default_search_factory
from elasticsearch_dsl.query import Q

def my_query_parser(qstr=None):
        if qstr:
            return Q('query_string', query=qstr)
        return Q()

def my_search_factory(*args, **kwargs):
    return default_search_factory(*args, **kwargs, query_parser=my_query_parser)

RECORDS_REST_ENDPOINTS = {
        'recid': {
            # ...
            'search_factory_imp': my_search_factory,
    }
}

my_query_parser is the function you want to modify in order to implement the parsing. Right now what it does, is to simply parse the query to Elasticsearch and let Elasticsearch do the parsing of the query string.

In your case, probably the best is to use something like luqum to parse the query, modify the abstract query and then send it to elasticsearch.

Last, can I ask you to send your questions to https://github.com/inveniosoftware/troubleshooting/issues instead? We prefer keeping issues on each repository to only bugs etc. Questions we prefer having a central place. See also https://invenio.readthedocs.io/en/latest/community/getting-help.html

PSvishnu93 commented 5 years ago

The solution you provided by overriding the search_factory_imp works for me. Thank you for the solution. I will post my queries in the places you mentioned above. Again thanks a lot.