<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2018 CERN.
# Copyright (C) 2018 RERO.
#
# Invenio-Circulation is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Circulation search API."""

from invenio_search.api import RecordsSearch

from ..proxies import current_circulation


class LoansSearch(RecordsSearch):
    """RecordsSearch for borrowed documents."""

    class Meta:
        """Search only on loans index."""

        index = "loans"
        doc_types = None


def search_by_pid(item_pid=None, document_pid=None, filter_states=None,
                  exclude_states=None):
    """Retrieve loans attached to the given item or document."""
    search = current_circulation.loan_search

    if document_pid:
        search = search.filter("term", document_pid=document_pid)
    elif item_pid:
        search = search.filter("term", item_pid=item_pid)
    else:
        raise ValueError("Must specify item_pid or document_pid param")

    if filter_states:
        search = search.filter("terms", state=filter_states)
    elif exclude_states:
        search = search.exclude("terms", state=exclude_states)

    return search


def search_by_patron_item(patron_pid, item_pid, filter_states=None):
    """Retrieve loans for patron given an item."""
    search = current_circulation.loan_search
    search = search \
        .filter("term", patron_pid=patron_pid) \
        .filter("term", item_pid=item_pid)

    if filter_states:
        search = search.filter("terms", state=filter_states)

    return search


def search_by_patron_pid(patron_pid):
    """Retrieve loans of a patron."""
    search = current_circulation.loan_search
    search = search.filter("term", patron_pid=patron_pid)
    return search
<commit_msg>search: add support search exclude to ES 2<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2018 CERN.
# Copyright (C) 2018 RERO.
#
# Invenio-Circulation is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Circulation search API."""

from elasticsearch_dsl import VERSION as ES_VERSION
from invenio_search.api import RecordsSearch

from ..proxies import current_circulation


class LoansSearch(RecordsSearch):
    """RecordsSearch for borrowed documents."""

    class Meta:
        """Search only on loans index."""

        index = "loans"
        doc_types = None

    def exclude(self, *args, **kwargs):
        """Add method `exclude` to old elastic search versions."""
        if ES_VERSION[0] == 2:
            from elasticsearch_dsl.query import Bool, Q
            return self.query(Bool(filter=[~Q(*args, **kwargs)]))
        else:
            return super(LoansSearch, self).exclude(*args, **kwargs)


def search_by_pid(item_pid=None, document_pid=None, filter_states=None,
                  exclude_states=None):
    """Retrieve loans attached to the given item or document."""
    search = current_circulation.loan_search

    if document_pid:
        search = search.filter("term", document_pid=document_pid)
    elif item_pid:
        search = search.filter("term", item_pid=item_pid)
    else:
        raise ValueError("Must specify item_pid or document_pid param")

    if filter_states:
        search = search.filter("terms", state=filter_states)
    elif exclude_states:
        search = search.exclude("terms", state=exclude_states)

    return search


def search_by_patron_item(patron_pid, item_pid, filter_states=None):
    """Retrieve loans for patron given an item."""
    search = current_circulation.loan_search
    search = search \
        .filter("term", patron_pid=patron_pid) \
        .filter("term", item_pid=item_pid)

    if filter_states:
        search = search.filter("terms", state=filter_states)

    return search


def search_by_patron_pid(patron_pid):
    """Retrieve loans of a patron."""
    search = current_circulation.loan_search
    search = search.filter("term", patron_pid=patron_pid)
    return search

{'fraction_non_alphanumeric': 0.10204081632653061, 'fraction_numerical': 0.0051635111876075735, 'mean_word_length': 3.4313725490196076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4990663', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1158, 'n_words': 339}