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

documents: search in full-text #292

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sonar/modules/documents/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

"""Query for documents."""

import re

from elasticsearch_dsl.query import Q
from flask import current_app, request

Expand All @@ -40,12 +42,21 @@ def documents_query_parser(qstr=None):
if not qstr:
return Q()

fields = FIELDS.copy()

# Special treatment for fulltext, we want to search in all fields and
# additionally in the fulltext field.
if 'fulltext:' in qstr:
result = re.match(r'^fulltext:(.*)$', qstr)
qstr = result.group(1)
fields.append('fulltext')

operator, query_type = get_operator_and_query_type(qstr)

return Q(query_type,
query=qstr,
default_operator=operator,
fields=FIELDS,
fields=fields,
lenient=True)
# lenient property is necessary to make it wildcards working, see
# https://github.com/elastic/elasticsearch/issues/39577#issuecomment-468751713
Expand Down
12 changes: 11 additions & 1 deletion tests/api/test_api_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from invenio_accounts.testutils import login_user_via_session


def test_api_query(client, document, document_json, make_document, superuser):
def test_api_query(client, document_with_file, document_json, make_document,
superuser):
"""Test simple flow using REST API."""
headers = [('Content-Type', 'application/json')]
login_user_via_session(client, email=superuser['email'])
Expand Down Expand Up @@ -92,6 +93,15 @@ def test_api_query(client, document, document_json, make_document, superuser):
assert response.status_code == 200
assert response.json['hits']['total'] == 1

# Test search in fulltext
response = client.get(url_for('invenio_records_rest.doc_list',
q='fulltext:the',
debug=1),
headers=headers)
assert response.status_code == 200
assert response.json['hits']['total'] == 1
assert len(response.json['hits']['hits'][0]['explanation']['details']) == 3

# Not allowed operator
with pytest.raises(Exception) as exception:
response = client.get(url_for('invenio_records_rest.doc_list',
Expand Down