forked from inveniosoftware/invenio-oaiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
percolator.py
169 lines (144 loc) · 5.93 KB
/
percolator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Percolator."""
from __future__ import absolute_import, print_function
import json
from elasticsearch import VERSION as ES_VERSION
from flask import current_app
from invenio_indexer.api import RecordIndexer
from invenio_indexer.utils import schema_to_index
from invenio_search import current_search, current_search_client
from invenio_search.utils import build_index_name
from .models import OAISet
from .proxies import current_oaiserver
from .query import query_string_parser
def _build_percolator_index_name(index):
"""Build percolator index name."""
suffix = "-percolators"
if ES_VERSION[0] < 7:
suffix = ""
return build_index_name(index, suffix=suffix, app=current_app)
def _create_percolator_mapping(index, doc_type, mapping_path=None):
"""Update mappings with the percolator field.
.. note::
This is only needed from ElasticSearch v5 onwards, because percolators
are now just a special type of field inside mappings.
"""
percolator_index = _build_percolator_index_name(index)
if ES_VERSION[0] in (5, 6):
current_search_client.indices.put_mapping(
index=index, doc_type=doc_type,
body=PERCOLATOR_MAPPING)
elif ES_VERSION[0] == 7:
if not mapping_path:
mapping_path = current_search.mappings[index]
if not current_search_client.indices.exists(percolator_index):
with open(mapping_path, 'r') as body:
mapping = json.load(body)
mapping["mappings"]["properties"].update(
PERCOLATOR_MAPPING["properties"])
current_search_client.indices.create(
index=percolator_index,
body=mapping
)
def _percolate_query(index, doc_type, percolator_doc_type, document):
"""Get results for a percolate query."""
index = _build_percolator_index_name(index)
if ES_VERSION[0] in (2, 5):
results = current_search_client.percolate(
index=index, doc_type=doc_type, allow_no_indices=True,
ignore_unavailable=True, body={'doc': document}
)
return results['matches']
elif ES_VERSION[0] in (6, 7):
es_client_params = dict(
index=index, doc_type=percolator_doc_type, allow_no_indices=True,
ignore_unavailable=True, body={
'query': {
'percolate': {
'field': 'query',
'document_type': percolator_doc_type,
'document': document,
}
}
})
if ES_VERSION[0] == 7:
es_client_params.pop('doc_type')
results = current_search_client.search(**es_client_params)
return results['hits']['hits']
def _get_percolator_doc_type(index):
es_ver = ES_VERSION[0]
if es_ver == 2:
return '.percolator'
elif es_ver == 5:
return 'percolators'
elif es_ver in (6, 7):
mapping_path = current_search.mappings[index]
_, doc_type = schema_to_index(mapping_path)
return doc_type
PERCOLATOR_MAPPING = {
'properties': {'query': {'type': 'percolator'}}
}
def _new_percolator(spec, search_pattern):
"""Create new percolator associated with the new set."""
if spec and search_pattern:
query = query_string_parser(search_pattern=search_pattern).to_dict()
for index, mapping_path in current_search.mappings.items():
# Create the percolator doc_type in the existing index for >= ES5
# TODO: Consider doing this only once in app initialization
percolator_doc_type = _get_percolator_doc_type(index)
_create_percolator_mapping(
index, percolator_doc_type, mapping_path)
current_search_client.index(
index=_build_percolator_index_name(index),
doc_type=percolator_doc_type,
id='oaiset-{}'.format(spec),
body={'query': query}
)
def _delete_percolator(spec, search_pattern):
"""Delete percolator associated with the new oaiset."""
if spec:
for index in current_search.mappings.keys():
# Create the percolator doc_type in the existing index for >= ES5
percolator_doc_type = _get_percolator_doc_type(index)
_create_percolator_mapping(index, percolator_doc_type)
current_search_client.delete(
index=_build_percolator_index_name(index),
doc_type=percolator_doc_type,
id='oaiset-{}'.format(spec), ignore=[404]
)
def _build_cache():
"""Build sets cache."""
sets = current_oaiserver.sets
if sets is None:
# build sets cache
sets = current_oaiserver.sets = [
oaiset.spec for oaiset in OAISet.query.filter(
OAISet.search_pattern.is_(None)).all()]
return sets
def get_record_sets(record):
"""Find matching sets."""
# get lists of sets with search_pattern equals to None but already in the
# set list inside the record
record_sets = set(record.get('_oai', {}).get('sets', []))
for spec in _build_cache():
if spec in record_sets:
yield spec
# get list of sets that match using percolator
index, doc_type = RecordIndexer().record_to_index(record)
document = record.dumps()
percolator_doc_type = _get_percolator_doc_type(index)
_create_percolator_mapping(index, percolator_doc_type)
results = _percolate_query(index, doc_type, percolator_doc_type, document)
prefix = 'oaiset-'
prefix_len = len(prefix)
for match in results:
set_name = match['_id']
if set_name.startswith(prefix):
name = set_name[prefix_len:]
yield name