@Override public <T> List<T> findByIdsWithContext(Class<T> clazz, String fetchContext, String... ids) { // get the fetch mpContext for the given type and apply it to the search List<String> includes = new ArrayList<String>(); List<String> excludes = new ArrayList<String>(); SourceFetchContext sourceFetchContext = getMappingBuilder().getFetchSource(clazz.getName(), fetchContext); if (sourceFetchContext != null) { includes.addAll(sourceFetchContext.getIncludes()); excludes.addAll(sourceFetchContext.getExcludes()); } else { getLog().warn("Unable to find fetch mpContext <" + fetchContext + "> for class <" + clazz.getName() + ">. It will be ignored."); } String[] inc = includes.isEmpty() ? null : includes.toArray(new String[includes.size()]); String[] exc = excludes.isEmpty() ? null : excludes.toArray(new String[excludes.size()]); // TODO: correctly manage "from" and "size" SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(getIndexForType(clazz)) .setQuery(QueryBuilders.idsQuery(MappingBuilder.indexTypeFromClass(clazz)).ids(ids)).setFetchSource(inc, exc).setSize(20); SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); return toGetListOfData(searchResponse, clazz); }
private QueryBuilder getVertexFilter(EnumSet<ElasticsearchDocumentType> elementTypes) { List<QueryBuilder> filters = new ArrayList<>(); List<String> edgeLabels = getParameters().getEdgeLabels(); String[] edgeLabelsArray = edgeLabels == null || edgeLabels.size() == 0 ? null : edgeLabels.toArray(new String[edgeLabels.size()]); Stream<EdgeInfo> edgeInfos = stream(sourceVertex.getEdgeInfos( direction, edgeLabelsArray, getParameters().getAuthorizations() )); if (otherVertexId != null) { edgeInfos = edgeInfos.filter(ei -> ei.getVertexId().equals(otherVertexId)); } String[] ids = edgeInfos.map(EdgeInfo::getVertexId).toArray(String[]::new); if (elementTypes.contains(ElasticsearchDocumentType.VERTEX)) { filters.add(QueryBuilders.idsQuery().ids(ids)); } if (elementTypes.contains(ElasticsearchDocumentType.VERTEX_EXTENDED_DATA)) { for (String vertexId : ids) { filters.add( QueryBuilders.boolQuery() .must(QueryBuilders.termQuery(Elasticsearch2SearchIndex.ELEMENT_TYPE_FIELD_NAME, ElasticsearchDocumentType.VERTEX_EXTENDED_DATA.getKey())) .must(QueryBuilders.termQuery(Elasticsearch2SearchIndex.EXTENDED_DATA_ELEMENT_ID_FIELD_NAME, vertexId))); } } return orFilters(filters); }
/** * Query for csars that are defined by locations. * * @return A maps of <csar_id, list of locations that uses the csar>. */ public Map<String, List<Location>> getAllExposedArchivesIdsExluding(Location excludedLocation) { // exclude a location from the search QueryBuilder query = QueryBuilders.boolQuery() .mustNot(QueryBuilders.idsQuery(Location.class.getSimpleName().toLowerCase()).ids(excludedLocation.getId())); List<Location> locations = alienDAO.customFindAll(Location.class, query); Map<String, List<Location>> archiveIds = Maps.newHashMap(); if (locations != null) { for (Location location : locations) { ILocationConfiguratorPlugin configuratorPlugin = getConfiguratorPlugin(location); List<PluginArchive> pluginArchives = configuratorPlugin.pluginArchives(); for (PluginArchive pluginArchive : safe(pluginArchives)) { String archiveId = pluginArchive.getArchive().getArchive().getId(); List<Location> locationsPerArchive = archiveIds.get(archiveId); if (locationsPerArchive == null) { locationsPerArchive = Lists.newArrayList(); archiveIds.put(archiveId, locationsPerArchive); } locationsPerArchive.add(location); } } } return archiveIds; }
@Override public T findOne(ID id) { SearchQuery query = new NativeSearchQueryBuilder() // .withQuery(and(idsQuery().ids(stringIdRepresentation(id)), aclFilter())) // .build(); List<T> list = elasticsearchOperations.queryForList(query, getEntityClass()); return list.isEmpty() ? null : list.get(0); }
.prepareSearch("_all") .setFetchSource(false) .setQuery(idsQuery(TYPE).ids(chunk)) .setSize(chunk.size()) .execute()
@Override public Iterable<T> findAll(Iterable<ID> ids) { Assert.notNull(ids, "ids can't be null."); SearchQuery query = new NativeSearchQueryBuilder() // .withQuery(and(idsQuery().ids(idsArray(ids)), aclFilter())) // .build(); return elasticsearchOperations.queryForList(query, getEntityClass()); }
@Override public Event execute(final Client client) { try { final SearchResponse r = client.prepareSearch(indexNamingStrategy.getRetrievalNames(snifferId)) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setQuery(QueryBuilders.idsQuery().ids(eventId)).execute().get(); if (r != null && r.getHits().hits().length > 0) { final SearchHit hit = r.getHits().hits()[0]; final Event event = jsonMapper.readValue(hit.getSourceAsString(), Event.class); event.setId(hit.getId()); return event; } else { return null; } } catch (final Exception e) { throw new DataAccessException("Failed to load for sniffer=" + snifferId + " the event: " + eventId, e); } } });
@SuppressWarnings({ "rawtypes", "unchecked" }) private void tryReusePreviousVersionConf(Plugin plugin) { // search for a previous version // TODO manage the case there are many previous versions QueryBuilder macthNameQuerybuilder = QueryBuilders.matchQuery("descriptor.id", plugin.getDescriptor().getId()); QueryBuilder idQueryBuilder = QueryBuilders.idsQuery(MappingBuilder.indexTypeFromClass(Plugin.class)).ids(plugin.getId()); QueryBuilder boolQuery = QueryBuilders.boolQuery().must(macthNameQuerybuilder).mustNot(idQueryBuilder); Plugin oldVersionPlugin = alienDAO.customFind(Plugin.class, boolQuery); if (oldVersionPlugin != null && oldVersionPlugin.isConfigurable()) { // get the configuration type Class<?> configType = pluginManager.getConfigurationType(plugin.getId()); PluginConfiguration oldPluginConf = alienDAO.findById(PluginConfiguration.class, oldVersionPlugin.getId()); // try to de-serialize it into the config of the new version if (oldPluginConf != null && oldPluginConf.getConfiguration() != null) { try { Object configObject = JsonUtil.readObject(JsonUtil.toString(oldPluginConf.getConfiguration()), configType); IPluginConfigurator configurator = pluginManager.getConfiguratorFor(plugin.getId()); configurator.setConfiguration(configObject); PluginConfiguration pluginConf = new PluginConfiguration(plugin.getId(), configObject); alienDAO.save(pluginConf); } catch (IOException e) { log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". The configuration beans are not comptible", e); } catch (PluginConfigurationException e) { log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". Error while applying the configuration", e); } } } }