/
SearchEngineServiceImpl.java
301 lines (271 loc) · 12.3 KB
/
SearchEngineServiceImpl.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.rest.api.service.impl.search;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.apim.core.api.crud_service.ApiCrudService;
import io.gravitee.apim.core.api.domain_service.ApiMetadataDecoderDomainService;
import io.gravitee.apim.core.api.domain_service.ApiMetadataDecoderDomainService.ApiMetadataDecodeContext;
import io.gravitee.apim.core.api.query_service.ApiQueryService;
import io.gravitee.apim.core.documentation.crud_service.PageCrudService;
import io.gravitee.apim.core.documentation.model.PrimaryOwnerApiTemplateData;
import io.gravitee.apim.core.membership.domain_service.ApiPrimaryOwnerDomainService;
import io.gravitee.apim.core.search.model.IndexableApi;
import io.gravitee.apim.core.search.model.IndexablePage;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.model.MessageRecipient;
import io.gravitee.rest.api.model.ApiPageEntity;
import io.gravitee.rest.api.model.PageEntity;
import io.gravitee.rest.api.model.UserEntity;
import io.gravitee.rest.api.model.api.ApiEntity;
import io.gravitee.rest.api.model.command.CommandSearchIndexerEntity;
import io.gravitee.rest.api.model.command.CommandTags;
import io.gravitee.rest.api.model.command.NewCommandEntity;
import io.gravitee.rest.api.model.search.Indexable;
import io.gravitee.rest.api.model.v4.api.GenericApiEntity;
import io.gravitee.rest.api.service.ApiMetadataService;
import io.gravitee.rest.api.service.CommandService;
import io.gravitee.rest.api.service.PageService;
import io.gravitee.rest.api.service.UserService;
import io.gravitee.rest.api.service.common.ExecutionContext;
import io.gravitee.rest.api.service.exceptions.AbstractNotFoundException;
import io.gravitee.rest.api.service.exceptions.TechnicalManagementException;
import io.gravitee.rest.api.service.impl.search.lucene.DocumentSearcher;
import io.gravitee.rest.api.service.impl.search.lucene.DocumentTransformer;
import io.gravitee.rest.api.service.impl.search.lucene.SearchEngineIndexer;
import io.gravitee.rest.api.service.impl.search.lucene.searcher.ApiDocumentSearcher;
import io.gravitee.rest.api.service.search.SearchEngineService;
import io.gravitee.rest.api.service.search.query.Query;
import io.gravitee.rest.api.service.v4.ApiSearchService;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
public class SearchEngineServiceImpl implements SearchEngineService {
private static final String ACTION_INDEX = "I";
private static final String ACTION_DELETE = "D";
/**
* Logger.
*/
private final Logger logger = LoggerFactory.getLogger(SearchEngineServiceImpl.class);
private final ObjectMapper mapper = new ObjectMapper();
@Autowired
private SearchEngineIndexer indexer;
@Autowired
private Collection<DocumentTransformer> transformers;
@Autowired
private Collection<DocumentSearcher> searchers;
@Autowired
@Lazy
private CommandService commandService;
@Autowired
@Lazy
private ApiMetadataService apiMetadataService;
@Autowired
@Lazy
private ApiSearchService apiSearchService;
@Autowired
@Lazy
private PageService pageService;
@Autowired
@Lazy
private UserService userService;
@Autowired
private PageCrudService pageCrudService;
@Autowired
private ApiCrudService apiCrudService;
@Autowired
private ApiPrimaryOwnerDomainService apiPrimaryOwnerDomainService;
@Autowired
private ApiMetadataDecoderDomainService apiMetadataDecoderDomainService;
@Async("indexerThreadPoolTaskExecutor")
@Override
public void index(ExecutionContext executionContext, Indexable source, boolean locally, boolean commit) {
indexLocally(source, commit);
if (!locally) {
CommandSearchIndexerEntity content = new CommandSearchIndexerEntity();
content.setAction(ACTION_INDEX);
content.setId(source.getId());
content.setClazz(source.getClass().getName());
sendCommands(executionContext, content);
}
}
@Async("indexerThreadPoolTaskExecutor")
@Override
public void delete(ExecutionContext executionContext, Indexable source) {
CommandSearchIndexerEntity content = new CommandSearchIndexerEntity();
content.setAction(ACTION_DELETE);
content.setId(source.getId());
content.setClazz(source.getClass().getName());
sendCommands(executionContext, content);
}
@Override
public void commit() {
try {
indexer.commit();
} catch (TechnicalException te) {
logger.error("Unexpected error while Lucene commit", te);
}
}
@Override
public void process(ExecutionContext executionContext, CommandSearchIndexerEntity content) {
if (ACTION_DELETE.equals(content.getAction())) {
try {
Indexable source = createInstance(content.getClazz());
source.setId(content.getId());
deleteLocally(source);
} catch (Exception ex) {
throw new TechnicalManagementException(
"Unable to delete document for content [ " + content.getId() + " - " + content.getClazz() + " ]",
ex
);
}
} else if (ACTION_INDEX.equals(content.getAction())) {
Indexable source = getSource(executionContext, content.getClazz(), content.getId());
if (source != null) {
indexLocally(source, true);
}
}
}
private void sendCommands(final ExecutionContext executionContext, CommandSearchIndexerEntity content) {
try {
NewCommandEntity msg = new NewCommandEntity();
msg.setTags(Collections.singletonList(CommandTags.DATA_TO_INDEX));
msg.setTo(MessageRecipient.MANAGEMENT_APIS.name());
msg.setTtlInSeconds(60);
msg.setContent(mapper.writeValueAsString(content));
commandService.send(executionContext, msg);
} catch (JsonProcessingException e) {
logger.error("Unexpected error while sending a message", e);
}
}
private Indexable getSource(final ExecutionContext executionContext, String clazz, String id) {
try {
if (ApiEntity.class.getName().equals(clazz) || io.gravitee.rest.api.model.v4.api.ApiEntity.class.getName().equals(clazz)) {
GenericApiEntity genericApi = apiSearchService.findGenericById(executionContext, id);
return apiMetadataService.fetchMetadataForApi(executionContext, genericApi);
} else if (PageEntity.class.getName().equals(clazz) || ApiPageEntity.class.getName().equals(clazz)) {
return pageService.findById(id);
} else if (UserEntity.class.getName().equals(clazz)) {
return userService.findById(executionContext, id);
} else if (IndexablePage.class.getName().equals(clazz)) {
return new IndexablePage(pageCrudService.get(id));
} else if (IndexableApi.class.getName().equals(clazz)) {
var api = apiCrudService.get(id);
var primaryOwner = apiPrimaryOwnerDomainService.getApiPrimaryOwner(executionContext.getOrganizationId(), id);
var metadata = apiMetadataDecoderDomainService.decodeMetadata(
id,
ApiMetadataDecodeContext
.builder()
.name(api.getName())
.description(api.getDescription())
.createdAt(Date.from(api.getCreatedAt().toInstant()))
.updatedAt(Date.from(api.getUpdatedAt().toInstant()))
.primaryOwner(
new PrimaryOwnerApiTemplateData(
primaryOwner.id(),
primaryOwner.displayName(),
primaryOwner.email(),
primaryOwner.type().name()
)
)
.build()
);
return new IndexableApi(api, primaryOwner, metadata);
}
} catch (final AbstractNotFoundException nfe) {
// ignore not found exception because may be due to synchronization not yet processed by DBs
}
return null;
}
private void indexLocally(Indexable source, boolean commit) {
transformers
.stream()
.filter(transformer -> transformer.handle(source.getClass()))
.findFirst()
.ifPresent(transformer -> {
try {
indexer.index(transformer.transform(source), commit);
} catch (TechnicalException te) {
logger.error("Unexpected error while indexing a document", te);
}
});
}
private void deleteLocally(Indexable source) {
transformers
.stream()
.filter(transformer -> transformer.handle(source.getClass()))
.findFirst()
.ifPresent(transformer -> {
try {
indexer.remove(transformer.transform(source));
} catch (TechnicalException te) {
logger.error("Unexpected error while deleting a document", te);
}
});
}
private Indexable createInstance(String className) throws Exception {
try {
final Class<?> clazz = Class.forName(className);
Assert.isAssignable(Indexable.class, clazz);
return (Indexable) clazz.newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
logger.error("Unable to instantiate class: {}", className, ex);
throw ex;
}
}
@Override
public SearchResult search(final ExecutionContext executionContext, Query<? extends Indexable> query) {
Optional<SearchResult> results = searchers
.stream()
.filter(searcher -> searcher.handle(query.getRoot()))
.findFirst()
.flatMap(searcher -> {
try {
if (searcher instanceof ApiDocumentSearcher) {
Optional<DocumentSearcher> pageDocumentSearcher = searchers
.stream()
.filter(s -> s.handle(PageEntity.class))
.findFirst();
if (pageDocumentSearcher.isPresent()) {
SearchResult apiReferences = pageDocumentSearcher.get().searchReference(executionContext, query);
if (!apiReferences.getDocuments().isEmpty()) {
query.setIds(apiReferences.getDocuments());
}
}
}
return Optional.of(searcher.search(executionContext, query));
} catch (TechnicalException te) {
logger.error("Unexpected error while searching a document", te);
return Optional.empty();
}
});
return results.orElse(null);
}
}