Search in sources :

Example 41 with Collections

use of java.util.Collections in project graylog2-server by Graylog2.

the class QuerySuggestionsES6 method suggest.

@Override
public SuggestionResponse suggest(SuggestionRequest req) {
    final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
    final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size())));
    final Search.Builder searchBuilder = new Search.Builder(search.toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(affectedIndices.isEmpty() ? Collections.singleton("") : affectedIndices).allowNoIndices(false).ignoreUnavailable(false);
    try {
        final SearchResult result = JestUtils.execute(jestClient, searchBuilder.build(), () -> "Unable to perform aggregation: ");
        final TermsAggregation aggregation = result.getAggregations().getTermsAggregation("fieldvalues");
        final List<SuggestionEntry> entries = aggregation.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getCount())).collect(Collectors.toList());
        if (!entries.isEmpty()) {
            return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, aggregation.getSumOtherDocCount());
        } else {
            final List<SuggestionEntry> corrections = Optional.of(result.getJsonObject()).map(o -> o.get("suggest")).map(o -> o.get("corrections")).map(o -> o.get(0)).map(o -> o.get("options")).map(options -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(options.elements(), Spliterator.ORDERED), false).map(option -> new SuggestionEntry(option.get("text").textValue(), option.get("freq").longValue())).collect(Collectors.toList())).orElseGet(Collections::emptyList);
            return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
        }
    } catch (Exception e) {
        final SuggestionError err = SuggestionError.create(e.getClass().getSimpleName(), e.getMessage());
        return SuggestionResponse.forError(req.field(), req.input(), err);
    }
}
Also used : TermsAggregation(io.searchbox.core.search.aggregation.TermsAggregation) SuggestBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry) Spliterators(java.util.Spliterators) JestClient(io.searchbox.client.JestClient) Inject(javax.inject.Inject) JestUtils(org.graylog.storage.elasticsearch6.jest.JestUtils) StreamSupport(java.util.stream.StreamSupport) SuggestionResponse(org.graylog.plugins.views.search.engine.suggestions.SuggestionResponse) IndexMapping(org.graylog2.indexer.IndexMapping) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) QueryBuilders(org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders) Search(io.searchbox.core.Search) Set(java.util.Set) SuggestBuilders(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.suggest.SuggestBuilders) SuggestionRequest(org.graylog.plugins.views.search.engine.suggestions.SuggestionRequest) Collectors(java.util.stream.Collectors) List(java.util.List) QuerySuggestionsService(org.graylog.plugins.views.search.engine.QuerySuggestionsService) AggregationBuilders(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilders) Optional(java.util.Optional) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) Collections(java.util.Collections) Spliterator(java.util.Spliterator) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResult(io.searchbox.core.SearchResult) TermsAggregation(io.searchbox.core.search.aggregation.TermsAggregation) SuggestBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.suggest.SuggestBuilder) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResult(io.searchbox.core.SearchResult) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) SuggestBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) Search(io.searchbox.core.Search) Collections(java.util.Collections) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry)

Example 42 with Collections

use of java.util.Collections in project mybatis-3 by mybatis.

the class DefaultObjectFactory method instantiateClass.

private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    try {
        Constructor<T> constructor;
        if (constructorArgTypes == null || constructorArgs == null) {
            constructor = type.getDeclaredConstructor();
            try {
                return constructor.newInstance();
            } catch (IllegalAccessException e) {
                if (Reflector.canControlMemberAccessible()) {
                    constructor.setAccessible(true);
                    return constructor.newInstance();
                } else {
                    throw e;
                }
            }
        }
        constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
        try {
            return constructor.newInstance(constructorArgs.toArray(new Object[0]));
        } catch (IllegalAccessException e) {
            if (Reflector.canControlMemberAccessible()) {
                constructor.setAccessible(true);
                return constructor.newInstance(constructorArgs.toArray(new Object[0]));
            } else {
                throw e;
            }
        }
    } catch (Exception e) {
        String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList).stream().map(Class::getSimpleName).collect(Collectors.joining(","));
        String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList).stream().map(String::valueOf).collect(Collectors.joining(","));
        throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
    }
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) Collections(java.util.Collections) ReflectionException(org.apache.ibatis.reflection.ReflectionException)

Example 43 with Collections

use of java.util.Collections in project mycore by MyCoRe-Org.

the class MCRSword method initConfig.

private static void initConfig() {
    if (collections == null) {
        collections = new Hashtable<>();
        workspaceCollectionTable = new Hashtable<>();
        final MCRConfiguration mcrConfiguration = MCRConfiguration.instance();
        Map<String, String> propertiesMap = mcrConfiguration.getPropertiesMap();
        final int lenghtOfPropertyPrefix = MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX.length();
        LOGGER.info("--- INITIALIZE SWORD SERVER ---");
        propertiesMap.keySet().stream().filter(// remove all which are not collections
        prop -> prop.startsWith(MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX)).filter(// remove all which have no suffix
        prop -> !prop.trim().equals(MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX)).map(// remove MCR_SWORD_COLLECTION_PREFIX
        prop -> prop.substring(lenghtOfPropertyPrefix)).map(// split to workspace name and collection name
        prop -> prop.split(Pattern.quote("."), 2)).filter(// remove all whith no workspace or collection name
        prop -> prop.length == 2).forEach(workspaceCollectionEntry -> {
            final String collection = workspaceCollectionEntry[1];
            final String workspace = workspaceCollectionEntry[0];
            LOGGER.info("Found collection: {} in workspace {}", collection, workspace);
            String name = MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX + workspace + "." + collection;
            LOGGER.info("Try to init : {}", name);
            MCRSwordCollectionProvider collectionProvider = mcrConfiguration.getInstanceOf(name);
            collections.put(collection, collectionProvider);
            final MCRSwordLifecycleConfiguration lifecycleConfiguration = new MCRSwordLifecycleConfiguration(collection);
            collectionProvider.init(lifecycleConfiguration);
            // This Map is needed to speed up the build of the {@link org.mycore.mir.sword2.manager.MCRServiceDocumentManager}
            List<String> collectionsOfWorkspace;
            if (workspaceCollectionTable.containsKey(workspace)) {
                collectionsOfWorkspace = workspaceCollectionTable.get(workspace);
            } else {
                collectionsOfWorkspace = new ArrayList<>();
                workspaceCollectionTable.put(workspace, collectionsOfWorkspace);
            }
            collectionsOfWorkspace.add(collection);
        });
        addCollectionShutdownHook();
    }
}
Also used : List(java.util.List) MCRSwordLifecycleConfiguration(org.mycore.sword.application.MCRSwordLifecycleConfiguration) Logger(org.apache.logging.log4j.Logger) Map(java.util.Map) MCRConfiguration(org.mycore.common.config.MCRConfiguration) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) LogManager(org.apache.logging.log4j.LogManager) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) MCRSwordCollectionProvider(org.mycore.sword.application.MCRSwordCollectionProvider) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRSwordCollectionProvider(org.mycore.sword.application.MCRSwordCollectionProvider) MCRSwordLifecycleConfiguration(org.mycore.sword.application.MCRSwordLifecycleConfiguration)

Example 44 with Collections

use of java.util.Collections in project alluxio by Alluxio.

the class UfsJournalIntegrationTest method deleteFsMasterJournalLogs.

private void deleteFsMasterJournalLogs() throws Exception {
    String journalFolder = mLocalAlluxioCluster.getLocalAlluxioMaster().getJournalFolder();
    UfsJournal journal = new UfsJournal(new URI(PathUtils.concatPath(journalFolder, Constants.FILE_SYSTEM_MASTER_NAME)), new NoopMaster(), 0, Collections::emptySet);
    if (UfsJournalSnapshot.getCurrentLog(journal) != null) {
        UnderFileSystem.Factory.create(journalFolder, ServerConfiguration.global()).deleteFile(UfsJournalSnapshot.getCurrentLog(journal).getLocation().toString());
    }
}
Also used : Collections(java.util.Collections) AlluxioURI(alluxio.AlluxioURI) URI(java.net.URI) UfsJournal(alluxio.master.journal.ufs.UfsJournal) NoopMaster(alluxio.master.NoopMaster)

Example 45 with Collections

use of java.util.Collections in project alluxio by Alluxio.

the class TriggeredCheckpointTest method ufsJournal.

@Test
public void ufsJournal() throws Exception {
    int numFiles = 100;
    MultiProcessCluster cluster = MultiProcessCluster.newBuilder(PortCoordination.TRIGGERED_UFS_CHECKPOINT).setClusterName("TriggeredUfsCheckpointTest").addProperty(PropertyKey.MASTER_JOURNAL_TYPE, JournalType.UFS.toString()).addProperty(PropertyKey.MASTER_JOURNAL_CHECKPOINT_PERIOD_ENTRIES, String.valueOf(numFiles)).setNumMasters(1).setNumWorkers(1).build();
    try {
        cluster.start();
        cluster.waitForAllNodesRegistered(20 * Constants.SECOND_MS);
        // Get enough journal entries
        createFiles(cluster, numFiles);
        // Trigger checkpoint
        Assert.assertEquals(cluster.getMasterAddresses().get(0).getHostname(), cluster.getMetaMasterClient().checkpoint());
        String journalLocation = cluster.getJournalDir();
        UfsJournal ufsJournal = new UfsJournal(URIUtils.appendPathOrDie(new URI(journalLocation), Constants.FILE_SYSTEM_MASTER_NAME), new NoopMaster(""), 0, Collections::emptySet);
        Assert.assertEquals(1, UfsJournalSnapshot.getSnapshot(ufsJournal).getCheckpoints().size());
        validateCheckpointInClusterRestart(cluster);
        cluster.notifySuccess();
    } finally {
        cluster.destroy();
    }
}
Also used : MultiProcessCluster(alluxio.multi.process.MultiProcessCluster) Collections(java.util.Collections) AlluxioURI(alluxio.AlluxioURI) URI(java.net.URI) UfsJournal(alluxio.master.journal.ufs.UfsJournal) NoopMaster(alluxio.master.NoopMaster) Test(org.junit.Test)

Aggregations

Collections (java.util.Collections)116 List (java.util.List)60 ArrayList (java.util.ArrayList)41 Test (org.junit.Test)39 Map (java.util.Map)38 Collectors (java.util.stream.Collectors)35 Arrays (java.util.Arrays)28 HashMap (java.util.HashMap)27 Set (java.util.Set)25 HashSet (java.util.HashSet)23 IOException (java.io.IOException)19 Collection (java.util.Collection)19 Optional (java.util.Optional)19 TimeUnit (java.util.concurrent.TimeUnit)16 URI (java.net.URI)13 Assert (org.junit.Assert)13 Function (java.util.function.Function)12 Stream (java.util.stream.Stream)12 Before (org.junit.Before)12 Logger (org.slf4j.Logger)12