@Override public Analyzer getAnalyzer(Class<?> entityType) { if (jpaPropertiesProvider.isHibernateSearchElasticSearchEnabled()) { ExtendedSearchIntegrator searchIntegrator = Search.getFullTextEntityManager(getEntityManager()) .getSearchFactory().unwrap(ExtendedSearchIntegrator.class); ScopedElasticsearchAnalyzer scopedAnalyzer = (ScopedElasticsearchAnalyzer) searchIntegrator.getAnalyzerReference(entityType).getAnalyzer(); try { // these properties are package protected ! ElasticsearchAnalyzer globalAnalyzer = (ElasticsearchAnalyzer) FieldUtils.getField(ScopedElasticsearchAnalyzer.class, "globalAnalyzer", true).get(scopedAnalyzer); @SuppressWarnings("unchecked") Map<String, ElasticsearchAnalyzer> analyzers = (Map<String, ElasticsearchAnalyzer>) FieldUtils.getField(ScopedElasticsearchAnalyzer.class, "scopedAnalyzers", true).get(scopedAnalyzer); Map<String, Analyzer> luceneAnalyzers = Maps.newHashMap(); for (Entry<String, ElasticsearchAnalyzer> analyzer : analyzers.entrySet()) { luceneAnalyzers.put(analyzer.getKey(), getAnalyzer(analyzer.getValue().getName(null))); } return new ScopedLuceneAnalyzer(getAnalyzer(globalAnalyzer.getName(null)) /* parameter is not used */, luceneAnalyzers); } catch (IllegalAccessException e) { throw new RuntimeException("illegal access on scoped analyzer", e); } } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class).getAnalyzer(entityType); } }
private void tryOutDelete( IndexUpdater updater, ExtendedSearchIntegrator impl, int expectedCount, Object id, Class<?> clazz) { updater.updateEvent( Arrays.asList( new UpdateEventInfo( clazz, id, EventType.DELETE ) ) ); assertEquals( expectedCount, impl.createHSQuery() .targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder().forEntity( Place.class ).get().all().createQuery() ) .queryResultSize() ); this.reset( updater, impl ); }
private void tryOutDeleteNonRoot( IndexUpdater updater, ExtendedSearchIntegrator impl, int expectedCount, Object id, Class<?> clazz, String fieldToCheckCount, String originalMatch) { this.deletedSorcerer = true; updater.updateEvent( Arrays.asList( new UpdateEventInfo( clazz, id, EventType.DELETE ) ) ); assertEquals( expectedCount, impl.createHSQuery() .targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder().forEntity( Place.class ).get().keyword().onField( fieldToCheckCount ).matching( originalMatch ) .createQuery() ).queryResultSize() ); this.deletedSorcerer = false; this.reset( updater, impl ); }
/** * @see MassIndexerImpl#toRootEntities */ protected Set<Class<?>> getIndexedRootEntities(SearchFactory searchFactory, Class<?>... selection) { ExtendedSearchIntegrator searchIntegrator = searchFactory.unwrap(ExtendedSearchIntegrator.class); Set<Class<?>> entities = new HashSet<Class<?>>(); // first build the "entities" set containing all indexed subtypes of "selection". for (Class<?> entityType : selection) { Set<Class<?>> targetedClasses = searchIntegrator.getIndexedTypesPolymorphic(new Class[] { entityType }); if (targetedClasses.isEmpty()) { String msg = entityType.getName() + " is not an indexed entity or a subclass of an indexed entity"; throw new IllegalArgumentException(msg); } entities.addAll(targetedClasses); } Set<Class<?>> cleaned = new HashSet<Class<?>>(); Set<Class<?>> toRemove = new HashSet<Class<?>>(); //now remove all repeated types to avoid duplicate loading by polymorphic query loading for (Class<?> type : entities) { boolean typeIsOk = true; for (Class<?> existing : cleaned) { if (existing.isAssignableFrom(type)) { typeIsOk = false; break; } if (type.isAssignableFrom(existing)) { toRemove.add(existing); } } if (typeIsOk) { cleaned.add(type); } } cleaned.removeAll(toRemove); return cleaned; }
public MassIndexerImpl( EntityManagerFactory emf, ExtendedSearchIntegrator searchIntegrator, List<Class<?>> rootTypes, TransactionManager transactionManager) { this.emf = emf; this.searchIntegrator = searchIntegrator; this.rootTypes = rootTypes; this.transactionManager = transactionManager; }
@Override public SynchronizedUpdateSource getUpdateSource( ExtendedSearchIntegrator searchIntegrator, Map<Class<?>, RehashedTypeMetadata> rehashedTypeMetadataPerIndexRoot, Map<Class<?>, List<Class<?>>> containedInIndexOf, Properties properties, EntityManagerFactory emf, TransactionManager transactionManager, Set<Class<?>> indexRelevantEntities) { HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) emf; SessionFactoryImpl sessionFactory = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory(); ServiceRegistry serviceRegistry = sessionFactory.getServiceRegistry(); EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); HibernateUpdateSource updateSource = new HibernateUpdateSource(); updateSource.initialize( searchIntegrator ); listenerRegistry.addDuplicationStrategy( new DuplicationStrategyImpl( HibernateUpdateSource.class ) ); listenerRegistry.appendListeners( EventType.POST_INSERT, updateSource ); listenerRegistry.appendListeners( EventType.POST_UPDATE, updateSource ); listenerRegistry.appendListeners( EventType.POST_DELETE, updateSource ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_RECREATE, updateSource ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_REMOVE, updateSource ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_UPDATE, updateSource ); return updateSource; }
/** * Initialize method called by Hibernate Core when the SessionFactory starts */ public void initialize(ExtendedSearchIntegrator extendedIntegrator) { this.extendedIntegrator = extendedIntegrator; if ( !disabled ) { skipDirtyChecks = !extendedIntegrator.isDirtyChecksEnabled(); LOGGER.fine( "Hibernate Search dirty checks " + (skipDirtyChecks ? "disabled" : "enabled") ); } }
SynchronizedUpdateSource getUpdateSource( ExtendedSearchIntegrator searchIntegrator, Map<Class<?>, RehashedTypeMetadata> rehashedTypeMetadataPerIndexRoot, Map<Class<?>, List<Class<?>>> containedInIndexOf, Properties properties, EntityManagerFactory emf, TransactionManager transactionManager, Set<Class<?>> indexRelevantEntities);
private void writeAllIntoIndex(EntityManager em, ExtendedSearchIntegrator impl) throws InterruptedException { // and write data in the index again this.setupData( em ); // wait a bit until the AsyncUpdateSource sent the appropriate events Sleep.sleep( 100_000, () -> this.assertCount( impl, 2 ), 100, "" ); }
private List<Integer> queryPlaceIds(ExtendedSearchIntegrator impl, String field, String value) { return impl.createHSQuery().targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder() .forEntity( Place.class ) .get() .keyword() .onField( field ) .matching( value ) .createQuery() ) .queryEntityInfos().stream().map( (entInfo) -> (Integer) entInfo.getId() ).collect( Collectors.toList() ); }
public static StandaloneSearchFactory createSearchFactory( SearchConfiguration searchConfiguration, Collection<Class<?>> classes) { SearchIntegratorBuilder builder = new SearchIntegratorBuilder(); // we have to build an integrator here (but we don't need it afterwards) builder.configuration( searchConfiguration ).buildSearchIntegrator(); classes.forEach( builder::addClass ); SearchIntegrator impl = builder.buildSearchIntegrator(); return new StandaloneSearchFactoryImpl( impl.unwrap( ExtendedSearchIntegrator.class ) ); }
public static MetadataProvider getDummyMetadataProvider(SearchConfiguration searchConfiguration) { ConfigContext configContext = new ConfigContext( searchConfiguration, new BuildContext() { @Override public ExtendedSearchIntegrator getUninitializedSearchIntegrator() { return null; } @Override public String getIndexingStrategy() { return IndexingMode.EVENT.toExternalRepresentation(); } @Override public IndexingMode getIndexingMode() { return IndexingMode.EVENT; } @Override public ServiceManager getServiceManager() { return new StandardServiceManager( searchConfiguration, this, Environment.DEFAULT_SERVICES_MAP ); } @Override public IndexManagerHolder getAllIndexesManager() { return new IndexManagerHolder(); } @Override public ErrorHandler getErrorHandler() { return new LogErrorHandler(); } } ); return new AnnotationMetadataProvider( new JavaReflectionManager(), configContext ); }
public IndexUpdater( Map<Class<?>, RehashedTypeMetadata> metadataPerForIndexRoot, Map<Class<?>, List<Class<?>>> containedInIndexOf, ReusableEntityProvider entityProvider, ExtendedSearchIntegrator searchIntegrator) { this( metadataPerForIndexRoot, containedInIndexOf, entityProvider, (IndexWrapper) null ); this.indexWrapper = new DefaultIndexWrapper( searchIntegrator ); }
@Test public void testWithIndex() { SearchConfiguration searchConfiguration = new StandaloneSearchConfiguration(); List<Class<?>> classes = Arrays.asList( Place.class, Sorcerer.class ); SearchIntegratorBuilder builder = new SearchIntegratorBuilder(); // we have to build an integrator here (but we don't need it afterwards) builder.configuration( searchConfiguration ).buildSearchIntegrator(); classes.forEach( (clazz) -> { builder.addClass( clazz ); } ); ExtendedSearchIntegrator impl = (ExtendedSearchIntegrator) builder.buildSearchIntegrator(); IndexUpdater updater = new IndexUpdater( this.rehashedTypeMetadataPerIndexRoot, this.containedInIndexOf, this.entityProvider, impl ); try { this.reset( updater, impl ); this.tryOutDelete( updater, impl, 0, 1, Place.class ); // this shouldn't delete the root though this.tryOutDeleteNonRoot( updater, impl, 0, 2, Sorcerer.class, "sorcerers.name", "Saruman" ); this.tryOutDeleteNonRoot( updater, impl, 1, 2, Sorcerer.class, "name", "Valinor" ); this.tryOutUpdate( updater, impl, 0, 1, Place.class, "name", "Valinor" ); this.tryOutUpdate( updater, impl, 0, 2, Sorcerer.class, "sorcerers.name", "Saruman" ); } finally { updater.close(); } }
private void assertCount(ExtendedSearchIntegrator impl, int count) { assertEquals( count, impl.createHSQuery() .targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder().forEntity( Place.class ).get().all().createQuery() ) .queryResultSize() ); }
private void tryOutUpdate( IndexUpdater updater, ExtendedSearchIntegrator impl, int expectedCount, Object id, Class<?> clazz, String field, String originalMatch) { this.changed = true; updater.updateEvent( Arrays.asList( new UpdateEventInfo( clazz, id, EventType.UPDATE ) ) ); assertEquals( expectedCount, impl.createHSQuery() .targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder() .forEntity( Place.class ) .get() .keyword() .onField( field ) .matching( originalMatch ) .createQuery() ) .queryResultSize() ); this.changed = false; this.reset( updater, impl ); }
@Override public ExtendedSearchIntegrator getSearchIntegrator() { return this.searchIntegrator; }
@Override public SynchronizedUpdateSource getUpdateSource( ExtendedSearchIntegrator searchIntegrator, Map<Class<?>, RehashedTypeMetadata> rehashedTypeMetadataPerIndexRoot, Map<Class<?>, List<Class<?>>> containedInIndexOf, Properties properties, EntityManagerFactory emf, TransactionManager transactionManager, Set<Class<?>> indexRelevantEntities) { JpaEntityManager entityManager = (JpaEntityManager) emf.createEntityManager(); try { Session session = entityManager.getServerSession(); IndexUpdater indexUpdater = new IndexUpdater( rehashedTypeMetadataPerIndexRoot, containedInIndexOf, new DummyReusableEntityProvider(), searchIntegrator ); EclipseLinkUpdateSource eclipseLinkUpdateSource = new EclipseLinkUpdateSource( indexUpdater, indexRelevantEntities, rehashedTypeMetadataPerIndexRoot, containedInIndexOf, transactionManager ); for ( Class<?> entity : indexRelevantEntities ) { if ( session.getDescriptor( entity ) == null ) { //no JPA entity continue; } session.getDescriptor( entity ) .getDescriptorEventManager() .addListener( eclipseLinkUpdateSource.descriptorEventAspect ); } if(transactionManager == null) { //equivalent to resource local tx session.getEventManager().addListener( eclipseLinkUpdateSource.sessionEventAspect ); } return eclipseLinkUpdateSource; } finally { entityManager.close(); } }
public ExtendedSearchIntegrator getExtendedSearchFactoryIntegrator() { return extendedIntegrator; }
public ExtendedSearchIntegrator getSearchIntegrator() { return this.searchIntegrator; }
private boolean assertCount(ExtendedSearchIntegrator impl, int count) { return count == impl.createHSQuery() .targetedEntities( Arrays.asList( Place.class ) ) .luceneQuery( impl.buildQueryBuilder().forEntity( Place.class ).get().all().createQuery() ) .queryResultSize(); }
public StandaloneSearchFactoryImpl(ExtendedSearchIntegrator searchIntegrator) { super(); this.searchIntegrator = searchIntegrator; this.queryExec = new DtoQueryExecutor(); }
@Override public ExtendedSearchIntegrator getUninitializedSearchIntegrator() { return null; }
public DefaultIndexWrapper(ExtendedSearchIntegrator searchIntegrator) { this.searchIntegrator = searchIntegrator; }
public ExtendedSearchIntegrator getSearchIntegrator();