1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from tg import config |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
def compute_index_name(desired_name): |
6
|
|
|
es_project_name = config.get('elasticsearch.project_name') |
7
|
|
|
es_instance_name = config.get('elasticsearch.instance_name') |
8
|
|
|
|
9
|
|
|
index_prefix = '%s_%s' % (es_project_name, es_instance_name) |
10
|
|
|
|
11
|
|
|
# This test prevents infinite length indexes. Indeed, elasticsearch_dsl |
12
|
|
|
# makes heavy uses of meta programming, and when you try to modify the |
13
|
|
|
# index name in document_cls._doc_type.index to use a custom generated index |
14
|
|
|
# name (and not static one), through the use of a callback, or a regular |
15
|
|
|
# function, it won't work. Therefore, we are forced to change the index name |
16
|
|
|
# dynamically (during the object object instantiation, in its constructor), |
17
|
|
|
# thus calling this function in the constructor. BUT, the index name for |
18
|
|
|
# a document is stored in a meta class which is shared by EVERY documents of |
19
|
|
|
# the same type. And, when dynamically setting the index name to a new |
20
|
|
|
# custom computed value (by say adding a prefix to the original name, like |
21
|
|
|
# we do in this function), it would behave this way: |
22
|
|
|
# |
23
|
|
|
# - first instantiation: |
24
|
|
|
# - _doc_type.index = foo |
25
|
|
|
# - constructor: prepend bar_ to foo |
26
|
|
|
# - result: _doc_type.index = bar_foo |
27
|
|
|
# - second instantiation: |
28
|
|
|
# - _doc_type.index = bar_foo |
29
|
|
|
# - constructor: prepend bar_ to bar_foo |
30
|
|
|
# - result: _doc_type.index = bar_bar_foo |
31
|
|
|
# . |
32
|
|
|
# . |
33
|
|
|
# . |
34
|
|
|
# . |
35
|
|
|
# - nth instantiation: |
36
|
|
|
# - _doc_type.index = bar_...bar_foo (with nth - 1 bar) |
37
|
|
|
# - constructor: prepend bar_ to bar_...bar_foo (with nth - 1 bar) |
38
|
|
|
# - result: _doc_type.index = bar_...bar_foo (with nth bar) |
39
|
|
|
# |
40
|
|
|
# Which would cause a lot of issues as you might have guessed, (such as |
41
|
|
|
# creating n index for the same document, until the size of the index was |
42
|
|
|
# too big for Elasticsearch, but the damages would already be consequent). |
43
|
|
|
# I don't know of any other way to fix this properly, until we can use |
44
|
|
|
# functions or callbacks in the Meta class for an elasticsearch_dsl.DocType |
45
|
|
|
# class to dynamically compute indexes name at run time. At least this |
46
|
|
|
# is a working method. |
47
|
|
|
if index_prefix in desired_name: |
48
|
|
|
return desired_name |
49
|
|
|
|
50
|
|
|
return '%s_%s' % (index_prefix, desired_name) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class PaginatedSearch(object): |
54
|
|
|
def __init__(self, search): |
55
|
|
|
self._search = search |
56
|
|
|
|
57
|
|
|
def __len__(self): |
58
|
|
|
return self._search.count() |
59
|
|
|
|
60
|
|
|
def __getitem__(self, item): |
61
|
|
|
return list(self._search[item]) |
62
|
|
|
|