Package org.grails.plugins.elasticsearch.mapping

Examples of org.grails.plugins.elasticsearch.mapping.SearchableClassMapping


        BulkRequestBuilder bulkRequestBuilder = elasticSearchClient.prepareBulk();
        //bulkRequestBuilder.setRefresh(true);

        // Execute index requests
        for (Map.Entry<IndexEntityKey, Object> entry : toIndex.entrySet()) {
            SearchableClassMapping scm = elasticSearchContextHolder.getMappingContextByType(entry.getKey().getClazz());
            if (isNewSession) {
                persistenceInterceptor = createInterceptor();
                session = SessionFactoryUtils.getSession(sessionFactory, true);
            }
            try {
                Object entity = entry.getValue();

                // If this not a transient instance, reattach it to the session
                if (session.contains(entity)) {
                    session.lock(entity, LockMode.NONE);
                    LOG.debug("Reattached entity to session");
                }

                XContentBuilder json = toJSON(entity);

                bulkRequestBuilder.add(
                        elasticSearchClient.prepareIndex()
                                .setIndex(scm.getIndexName())
                                .setType(scm.getElasticTypeName())
                                .setId(entry.getKey().getId()) // TODO : Composite key ?
                                .setSource(json)
                );
                if (LOG.isDebugEnabled()) {
                    try {
                        LOG.debug("Indexing " + entry.getKey().getClazz() + "(index:" + scm.getIndexName() + ",type:" + scm.getElasticTypeName() +
                                ") of id " + entry.getKey().getId() + " and source " + json.string());
                    } catch (IOException e) {
                    }
                }
            } finally {
                if (null != persistenceInterceptor) {
                    persistenceInterceptor.destroy();
                }
            }
        }

        // Execute delete requests
        for (IndexEntityKey key : toDelete) {
            SearchableClassMapping scm = elasticSearchContextHolder.getMappingContextByType(key.getClazz());
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting object from index " + scm.getIndexName() + " and type " + scm.getElasticTypeName() + " and ID " + key.getId());
            }
            bulkRequestBuilder.add(
                    elasticSearchClient.prepareDelete()
                            .setIndex(scm.getIndexName())
                            .setType(scm.getElasticTypeName())
                            .setId(key.getId())
            );
        }

        // Perform bulk request
View Full Code Here


            this.clazz = clazz;
        }

        IndexEntityKey(Object instance) {
            this.clazz = GrailsHibernateUtil.unwrapIfProxy(instance).getClass();
            SearchableClassMapping scm = elasticSearchContextHolder.getMappingContextByType(this.clazz);
            if (scm == null) {
                throw new IllegalArgumentException("Class " + clazz + " is not a searchable domain class.");
            }
            this.id = (InvokerHelper.invokeMethod(instance, "ident", null)).toString();
        }
View Full Code Here

    public Collection buildResults(SearchHits hits) {
        DefaultUnmarshallingContext unmarshallingContext = new DefaultUnmarshallingContext();
        List results = new ArrayList();
        for(SearchHit hit : hits) {
            String type = hit.type();
            SearchableClassMapping scm = elasticSearchContextHolder.findMappingContextByElasticType(type);
            if (scm == null) {
                LOG.warn("Unknown SearchHit: " + hit.id() + "#" + hit.type() + ", domain class name: ");
                continue;
            }
            String domainClassName = scm.getDomainClass().getFullName();

            GrailsDomainClassProperty identifier = scm.getDomainClass().getIdentifier();
            Object id = typeConverter.convertIfNecessary(hit.id(), identifier.getType());
            GroovyObject instance = (GroovyObject) scm.getDomainClass().newInstance();
            instance.setProperty(identifier.getName(), id);

            /*def mapContext = elasticSearchContextHolder.getMappingContext(domainClass.propertyName)?.propertiesMapping*/
            Map rebuiltProperties = new HashMap();
            for(Map.Entry<String, Object> entry : hit.getSource().entrySet()) {
                unmarshallingContext.getUnmarshallingStack().push(entry.getKey());
                rebuiltProperties.put(entry.getKey(),
                        unmarshallProperty(scm.getDomainClass(), entry.getKey(), entry.getValue(), unmarshallingContext));
                populateCyclicReference(instance, rebuiltProperties, unmarshallingContext);
                unmarshallingContext.resetContext();
            }
            // todo manage read-only transient properties...
            bind.invoke(instance, "bind", new Object[] { instance, rebuiltProperties });
View Full Code Here

TOP

Related Classes of org.grails.plugins.elasticsearch.mapping.SearchableClassMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.