9
9
import os
10
10
import io
11
11
import logging
12
- import base64
13
12
import threading
14
13
import contextlib
15
14
import datetime
16
15
from copy import deepcopy
17
16
18
17
from lxml import isoschematron , etree
19
- from elasticsearch import Elasticsearch
20
- from django .conf import settings
21
18
from django .db .models import Q
22
19
from django .db import IntegrityError
23
20
24
21
from scielomanager .celery import app
22
+ from scielomanager import connectors
25
23
from . import models
26
24
27
25
37
35
ARTICLE_META_SCHEMATRON = isoschematron .Schematron (file = BASIC_ARTICLE_META_PATH )
38
36
39
37
40
- def get_elasticsearch ():
41
- """Fábrica de clientes do Elasticsearch.
42
-
43
- Essa função é um singleton.
44
- """
45
- if not hasattr (get_elasticsearch , 'client' ):
46
- get_elasticsearch .client = Elasticsearch (settings .ELASTICSEARCH_NODES )
47
-
48
- return get_elasticsearch .client
49
-
50
-
51
- def index_article (id , struct , ** kwargs ):
52
- """Indexa `struct` no índice de artigos do catman no Elasticsearch.
53
- """
54
- client = get_elasticsearch ()
55
- result = client .index (
56
- index = settings .ES_ARTICLE_INDEX_NAME ,
57
- doc_type = settings .ES_ARTICLE_DOC_TYPE ,
58
- id = id , body = struct , ** kwargs )
59
-
60
- return result
38
+ elasticsearch_client = connectors .ArticleElasticsearch ()
61
39
62
40
63
41
def _gen_es_struct_from_article (article ):
@@ -78,13 +56,12 @@ def _gen_es_struct_from_article(article):
78
56
for attr , expr in values_to_struct_mapping }
79
57
80
58
article_as_octets = str (article .xml )
81
- base64_octets = base64 .b64encode (article_as_octets )
82
59
83
60
partial_struct = {
84
61
'version' : article .xml_version ,
85
62
'is_aop' : article .is_aop ,
86
- 'b64_source' : base64_octets ,
87
63
'source' : article_as_octets ,
64
+ 'aid' : article .aid ,
88
65
}
89
66
90
67
es_struct .update (partial_struct )
@@ -122,16 +99,14 @@ def submit_to_elasticsearch(article_pk):
122
99
return None
123
100
124
101
struct = _gen_es_struct_from_article (article )
125
- result = index_article (article .aid , struct )
102
+ elasticsearch_client . add (article .aid , struct )
126
103
127
- logger .info ('Elasticsearch indexing result for article "%s": %s.' ,
128
- article .domain_key , result )
104
+ logger .info ('Article "%s" was indexed successfully.' , article .domain_key )
129
105
130
- if result .get ('_version' ) > 0 :
131
- article .es_updated_at = datetime .datetime .now ()
132
- article .es_is_dirty = False
133
- with avoid_circular_signals (ARTICLE_SAVE_MUTEX ):
134
- article .save ()
106
+ article .es_updated_at = datetime .datetime .now ()
107
+ article .es_is_dirty = False
108
+ with avoid_circular_signals (ARTICLE_SAVE_MUTEX ):
109
+ article .save ()
135
110
136
111
137
112
@app .task (ignore_result = True )
0 commit comments