Skip to content

Commit

Permalink
search: remove elasticsearch deprecated imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kpsherva committed Jan 18, 2024
1 parent 33b1c79 commit b627389
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 44 deletions.
11 changes: 5 additions & 6 deletions cds_ils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import arrow
import click
import pkg_resources
from elasticsearch import VERSION as ES_VERSION
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from flask import current_app
from flask.cli import with_appcontext
from invenio_accounts.models import User
Expand Down Expand Up @@ -457,8 +456,8 @@ def clean_loans(user_email, given_date):
.filter(
"bool",
filter=[
Q("term", patron_pid=patron_pid),
Q("term", _created=given_date),
dsl.Q("term", patron_pid=patron_pid),
dsl.Q("term", _created=given_date),
],
)
.scan()
Expand All @@ -477,8 +476,8 @@ def clean_loans(user_email, given_date):
.filter(
"bool",
filter=[
Q("term", patron_pid=patron_pid),
Q("term", _created=given_date),
dsl.Q("term", patron_pid=patron_pid),
dsl.Q("term", _created=given_date),
],
)
.scan()
Expand Down
15 changes: 7 additions & 8 deletions cds_ils/importer/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-ILS Importer module."""
from elasticsearch_dsl import Q
from elasticsearch_dsl.query import Match
from invenio_search.engine import dsl
from invenio_app_ils.proxies import current_app_ils


Expand All @@ -17,8 +16,8 @@ def search_documents_by_isbn(isbn):
search = document_search.query(
"bool",
must=[
Q("term", identifiers__scheme="ISBN"),
Q("term", identifiers__value=isbn),
dsl.Q("term", identifiers__scheme="ISBN"),
dsl.Q("term", identifiers__value=isbn),
],
)
return search
Expand All @@ -30,8 +29,8 @@ def search_documents_by_doi(doi):
search = document_search.query(
"bool",
must=[
Q("term", identifiers__scheme="DOI"),
Q("term", identifiers__value=doi),
dsl.Q("term", identifiers__scheme="DOI"),
dsl.Q("term", identifiers__value=doi),
],
)
return search
Expand Down Expand Up @@ -61,15 +60,15 @@ def fuzzy_search_document(title, authors):
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
document_search = current_app_ils.document_search_cls()
search = document_search.query(
Match(
dsl.query.Match(
title__keyword={
"fuzziness": "AUTO",
"fuzzy_transpositions": "true",
"query": title,
}
)
).filter(
Match(
dsl.query.Match(
authors__full_name={
"query": " ".join(authors),
"fuzziness": "AUTO",
Expand Down
4 changes: 2 additions & 2 deletions cds_ils/importer/documents/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import click
from dateutil import parser
from elasticsearch import TransportError
from invenio_search.engine import search
from invenio_app_ils.documents.api import DocumentIdProvider
from invenio_app_ils.errors import IlsValidationError
from invenio_app_ils.proxies import current_app_ils
Expand Down Expand Up @@ -410,7 +410,7 @@ def fuzzy_match_documents(self):
authors = [author["full_name"] for author in self.json_data.get("authors", [])]
try:
fuzzy_results = fuzzy_search_document(title, authors).scan()
except TransportError:
except search.TransportError:
raise SimilarityMatchUnavailable
return fuzzy_results

Expand Down
4 changes: 2 additions & 2 deletions cds_ils/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time

import pkg_resources
from elasticsearch import TransportError
from invenio_search.engine import search
from flask import current_app
from invenio_app_ils.errors import RecordHasReferencesError
from invenio_app_ils.proxies import current_app_ils
Expand Down Expand Up @@ -162,7 +162,7 @@ def find_partial_matches(self, pids_list=None, exact_match=None):
for match in fuzzy_results
if match.pid != exact_match
]
except TransportError:
except search.TransportError:
raise SimilarityMatchUnavailable
return fuzzy_matches + amibiguous_matches

Expand Down
10 changes: 5 additions & 5 deletions cds_ils/importer/series/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

"""CDS-ILS Series Importer api."""

from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from invenio_app_ils.proxies import current_app_ils


Expand All @@ -17,8 +17,8 @@ def search_series_by_isbn(isbn):
search = series_search.query(
"bool",
must=[
Q("term", identifiers__scheme="ISBN"),
Q("term", identifiers__value=isbn),
dsl.Q("term", identifiers__scheme="ISBN"),
dsl.Q("term", identifiers__value=isbn),
],
)
return search
Expand All @@ -30,8 +30,8 @@ def search_series_by_issn(issn):
search = series_search.query(
"bool",
must=[
Q("term", identifiers__scheme="ISSN"),
Q("term", identifiers__value=issn),
dsl.Q("term", identifiers__scheme="ISSN"),
dsl.Q("term", identifiers__value=issn),
],
)
return search
Expand Down
14 changes: 7 additions & 7 deletions cds_ils/migrator/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""CDS-ILS document migrator API."""

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from invenio_app_ils.proxies import current_app_ils

from cds_ils.migrator.errors import DocumentMigrationError
Expand Down Expand Up @@ -49,7 +49,7 @@ def get_all_documents_with_files():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__has_files=True),
dsl.Q("term", _migration__has_files=True),
],
)
return search
Expand All @@ -61,7 +61,7 @@ def get_documents_with_proxy_eitems():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__eitems_has_proxy=True),
dsl.Q("term", _migration__eitems_has_proxy=True),
],
)
return search
Expand All @@ -73,7 +73,7 @@ def get_documents_with_ebl_eitems():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__eitems_has_ebl=True),
dsl.Q("term", _migration__eitems_has_ebl=True),
],
)
return search
Expand All @@ -85,7 +85,7 @@ def get_documents_with_safari_eitems():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__eitems_has_safari=True),
dsl.Q("term", _migration__eitems_has_safari=True),
],
)
return search
Expand All @@ -97,7 +97,7 @@ def get_documents_with_external_eitems():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__eitems_has_external=True),
dsl.Q("term", _migration__eitems_has_external=True),
],
)
return search
Expand All @@ -109,7 +109,7 @@ def search_documents_with_siblings_relations():
search = document_search.filter(
"bool",
filter=[
Q("term", _migration__has_related=True),
dsl.Q("term", _migration__has_related=True),
],
)
return search
4 changes: 2 additions & 2 deletions cds_ils/migrator/internal_locations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import json

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from invenio_app_ils.internal_locations.api import InternalLocation
from invenio_app_ils.internal_locations.search import InternalLocationSearch
from invenio_app_ils.proxies import current_app_ils
Expand Down Expand Up @@ -74,7 +74,7 @@ def import_internal_locations_from_json(
def get_internal_location_by_legacy_recid(legacy_recid):
"""Search for internal location by legacy id."""
search = InternalLocationSearch().query(
"bool", filter=[Q("term", legacy_ids=legacy_recid)]
"bool", filter=[dsl.Q("term", legacy_ids=legacy_recid)]
)
result = search.execute()
hits_total = result.hits.total.value
Expand Down
4 changes: 2 additions & 2 deletions cds_ils/migrator/items/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from invenio_app_ils.proxies import current_app_ils
from invenio_db import db

Expand Down Expand Up @@ -103,7 +103,7 @@ def get_item_by_barcode(barcode, raise_exception=True):
search = current_app_ils.item_search_cls().query(
"bool",
filter=[
Q("term", barcode=barcode),
dsl.Q("term", barcode=barcode),
],
)
result = search.execute()
Expand Down
6 changes: 3 additions & 3 deletions cds_ils/migrator/patrons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from flask import current_app
from invenio_app_ils.patrons.indexer import PatronIndexer
from invenio_app_ils.patrons.search import PatronsSearch
Expand Down Expand Up @@ -74,7 +74,7 @@ def get_user_by_person_id(person_id):
search = PatronsSearch().query(
"bool",
filter=[
Q("term", person_id=person_id),
dsl.Q("term", person_id=person_id),
],
)
results = search.execute()
Expand All @@ -95,7 +95,7 @@ def get_user_by_legacy_id(legacy_id):
search = PatronsSearch().query(
"bool",
filter=[
Q("term", legacy_id=legacy_id),
dsl.Q("term", legacy_id=legacy_id),
],
)
results = search.execute()
Expand Down
6 changes: 3 additions & 3 deletions cds_ils/migrator/relations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from flask import current_app
from invenio_app_ils.proxies import current_app_ils
from invenio_app_ils.records_relations.api import (
Expand Down Expand Up @@ -223,8 +223,8 @@ def link_record_and_journal(record_cls, search):
series_search.filter(
"bool",
filter=[
Q("term", mode_of_issuance="MULTIPART_MONOGRAPH"),
Q("term", _migration__has_serial=True),
dsl.Q("term", mode_of_issuance="MULTIPART_MONOGRAPH"),
dsl.Q("term", _migration__has_serial=True),
],
),
)
Expand Down
8 changes: 4 additions & 4 deletions cds_ils/migrator/series/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from copy import deepcopy

import click
from elasticsearch_dsl import Q
from invenio_search.engine import dsl
from flask import current_app
from invenio_app_ils.proxies import current_app_ils
from invenio_app_ils.series.api import Series
Expand Down Expand Up @@ -195,8 +195,8 @@ def get_serials_by_child_recid(recid):
search = series_search.query(
"bool",
filter=[
Q("term", mode_of_issuance="SERIAL"),
Q("term", _migration__children=recid),
dsl.Q("term", mode_of_issuance="SERIAL"),
dsl.Q("term", _migration__children=recid),
],
)
for hit in search.params(scroll="1h").scan():
Expand Down Expand Up @@ -274,7 +274,7 @@ def search_series_with_relations():
search = series_search.filter(
"bool",
filter=[
Q("term", _migration__has_related=True),
dsl.Q("term", _migration__has_related=True),
],
)
return search
Expand Down

0 comments on commit b627389

Please sign in to comment.