public static void deleteEntities(DatasourceMapperTester datasourceMapper, AbstractEntity... entities) { println("Starting index removal..."); SearchIntegrator searchIntegrator = datasourceMapper.getSearchIntegrator(); Worker worker = searchIntegrator.getWorker(); TransactionContextTester tc = new TransactionContextTester(); boolean needsFlush = false; int i = 1; for (; i <= entities.length; i++) { AbstractEntity entity = entities[i - 1]; Work work = new Work(entity, entity.getId(), WorkType.DELETE, false); worker.performWork(work, tc); needsFlush = true; if (i % 1000 == 0) { //commit in batches of 1000: tc.end(); needsFlush = false; tc = new TransactionContextTester(); } } if (needsFlush) { //commit remaining work tc.end(); } println(" ... removed an index of " + (i - 1) + " entities."); }
@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); } }
public static SearchIntegrator createSearchIntegrator(Class<?>... classes) { SearchConfigurationTester configuration = SearchConfigurationTester.noTestDefaults(); configuration.addClasses(classes); SearchIntegratorBuilder builder = new SearchIntegratorBuilder(); builder.configuration(configuration); return builder.buildSearchIntegrator(); }
public static void preindexEntities(DatasourceMapperTester datasourceMapper, AbstractEntity... entities) { println("Starting index creation..."); SearchIntegrator searchIntegrator = datasourceMapper.getSearchIntegrator(); Worker worker = searchIntegrator.getWorker(); TransactionContextTester tc = new TransactionContextTester(); boolean needsFlush = false; int i = 1; for (; i <= entities.length; i++) { AbstractEntity entity = entities[i - 1]; Work work = new Work(entity, entity.getId(), WorkType.ADD, false); worker.performWork(work, tc); //noinspection unchecked datasourceMapper.put(entity); needsFlush = true; if (i % 1000 == 0) { //commit in batches of 1000: tc.end(); needsFlush = false; tc = new TransactionContextTester(); } } if (needsFlush) { //commit remaining work tc.end(); } println(" ... created an index of " + (i - 1) + " entities."); }
public static void purgeAll(DatasourceMapperTester datasourceMapper, Class<?> entityClass) { println("Purging index - " + entityClass.getSimpleName() + " ..."); SearchIntegrator searchIntegrator = datasourceMapper.getSearchIntegrator(); Worker worker = searchIntegrator.getWorker(); TransactionContextTester tc = new TransactionContextTester(); IndexedTypeIdentifier indexedTypeIdentifier = PojoIndexedTypeIdentifier.convertFromLegacy(entityClass); Work work = new Work(indexedTypeIdentifier, null, WorkType.PURGE_ALL); worker.performWork(work, tc); tc.end(); datasourceMapper.clear(); }
@Override public Analyzer getAnalyzer(String analyzerName) { if (jpaPropertiesProvider.isHibernateSearchElasticSearchEnabled()) { if ("default".equals(analyzerName)) { return PassThroughAnalyzer.INSTANCE; } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class) .getAnalyzer(CoreLuceneClientAnalyzersDefinitionProvider.ANALYZER_NAME_PREFIX + analyzerName); } } else { return Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().unwrap(SearchIntegrator.class).getAnalyzer(analyzerName); } }
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 DatasourceMapperTester(SearchIntegrator searchIntegrator) { this.searchIntegrator = searchIntegrator; }
public SearchIntegrator getSearchIntegrator() { return searchIntegrator; }
public static <T extends AbstractEntity> DatasourceMapperTester<T> createDatasourceMapper(Class<T> entityClass) { SearchIntegrator searchIntegrator = createSearchIntegrator(entityClass); return new DatasourceMapperTester<T>(searchIntegrator); }
protected SearchIntegrator getSearchIntegrator() { return searchManager.unwrap(SearchIntegrator.class); }
protected SearchIntegrator getSearchIntegrator() { if (searchIntegrator == null) { searchIntegrator = ContextHelper.getSearchIntegratorBySF(emf.unwrap(SessionFactoryImplementor.class)); } return searchIntegrator; }
@Override protected SearchIntegrator getSearchIntegrator() { // TODO Auto-generated method stub return null; }
public HSearchQueryImpl(HSQuery hsquery, DtoQueryExecutor queryExec, SearchIntegrator searchIntegrator) { this.hsquery = hsquery; this.queryExec = queryExec; this.searchIntegrator = searchIntegrator; }
@Test public void testWithoutNewClasses() { SearchConfiguration searchConfiguration = new StandaloneSearchConfiguration(); List<Class<?>> classes = Arrays.asList( TopLevel.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 ); } ); SearchIntegrator impl = builder.buildSearchIntegrator(); TopLevel tl = new TopLevel(); tl.setId( 123 ); Embedded eb = new Embedded( 1 ); List<Embedded2> embedded2 = new ArrayList<>(); { Embedded2 e1 = new Embedded2(); e1.setEmbedded( eb ); embedded2.add( e1 ); Embedded2 e2 = new Embedded2(); e2.setEmbedded( eb ); embedded2.add( e1 ); } eb.setEmbedded2( embedded2 ); tl.setEmbedded( eb ); Transaction tc = new Transaction(); impl.getWorker().performWork( new Work( tl, WorkType.ADD ), tc ); tc.commit(); assertEquals( 1, impl.createHSQuery().luceneQuery( new MatchAllDocsQuery() ).targetedEntities( Collections.singletonList( TopLevel.class ) ).queryResultSize() ); }
protected abstract SearchIntegrator getSearchIntegrator();