From ef90d3d48bc21f46a1a42f4df686199a0214467c Mon Sep 17 00:00:00 2001 From: Israel Date: Sun, 7 Apr 2024 20:13:42 -0600 Subject: [PATCH] * Salvando migracion --- pom.xml | 17 +++-- .../sepomex/model/AsentamientoTipo.java | 1 + .../perales/sepomex/model/CodigoPostal.java | 3 +- .../com/perales/sepomex/model/Colonia.java | 18 +----- .../com/perales/sepomex/model/Estado.java | 1 + .../sepomex/model/InegiClaveCiudad.java | 2 +- .../com/perales/sepomex/model/ZonaTipo.java | 8 +-- .../sepomex/service/ColoniaService.java | 62 ++++++++++--------- .../util/MyLuceneAnalysisConfigurer.java | 21 +++++++ .../resources/application-local.properties | 4 +- 10 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/perales/sepomex/util/MyLuceneAnalysisConfigurer.java diff --git a/pom.xml b/pom.xml index 572cd10..ba7e524 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ 3.0.1-b06 3.0.1-b12 2.4.0-b180830.0359 - 5.11.10.Final + 6.0.10.Final 1.3 42.4.2 2.0.2 @@ -113,11 +113,20 @@ - org.hibernate - hibernate-search-orm + org.hibernate.search + hibernate-search-mapper-orm + ${hibernate.search.orm.version} + + + org.hibernate.search + hibernate-search-backend-lucene + ${hibernate.search.orm.version} + + + org.hibernate.search + hibernate-search-v5migrationhelper-orm ${hibernate.search.orm.version} - org.hamcrest hamcrest-all diff --git a/src/main/java/com/perales/sepomex/model/AsentamientoTipo.java b/src/main/java/com/perales/sepomex/model/AsentamientoTipo.java index 498dc4b..b145669 100644 --- a/src/main/java/com/perales/sepomex/model/AsentamientoTipo.java +++ b/src/main/java/com/perales/sepomex/model/AsentamientoTipo.java @@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import org.hibernate.search.annotations.*; +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; import javax.persistence.*; import javax.validation.constraints.NotBlank; diff --git a/src/main/java/com/perales/sepomex/model/CodigoPostal.java b/src/main/java/com/perales/sepomex/model/CodigoPostal.java index ec4c3ae..4d00afe 100644 --- a/src/main/java/com/perales/sepomex/model/CodigoPostal.java +++ b/src/main/java/com/perales/sepomex/model/CodigoPostal.java @@ -7,7 +7,6 @@ import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.TermVector; - import javax.persistence.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -65,4 +64,4 @@ public class CodigoPostal implements Serializable { @OneToMany(mappedBy = "codigoPostalAdministracionAsentamientoOficina", fetch = FetchType.LAZY) private List coloniasCodigoPostalAdministracionAsentamientoOficina; -} \ No newline at end of file +} diff --git a/src/main/java/com/perales/sepomex/model/Colonia.java b/src/main/java/com/perales/sepomex/model/Colonia.java index 8454845..528753d 100644 --- a/src/main/java/com/perales/sepomex/model/Colonia.java +++ b/src/main/java/com/perales/sepomex/model/Colonia.java @@ -7,11 +7,8 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.ToString; -import org.apache.lucene.analysis.core.KeywordTokenizerFactory; -import org.apache.lucene.analysis.core.LowerCaseFilterFactory; -import org.apache.lucene.analysis.es.SpanishLightStemFilterFactory; -import org.apache.lucene.analysis.standard.StandardTokenizerFactory; -import org.hibernate.search.annotations.*; +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField; +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -19,12 +16,6 @@ import java.io.Serializable; @Indexed -@AnalyzerDef(name = "es", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters = { - @TokenFilterDef(factory = SpanishLightStemFilterFactory.class), - @TokenFilterDef(factory = LowerCaseFilterFactory.class)}) -@AnalyzerDef(name = "es_beginEnd",tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class), filters = { - @TokenFilterDef(factory = SpanishLightStemFilterFactory.class), - @TokenFilterDef(factory = LowerCaseFilterFactory.class)}) @Data @NoArgsConstructor @EqualsAndHashCode(exclude = { @@ -65,10 +56,7 @@ public class Colonia implements Serializable { @Column(name = "id") private Long id; - - - @Field(store = Store.YES) - @Field(name = "coloniaEs_beginEnd", store = Store.YES, analyzer = @Analyzer(definition = "es_beginEnd")) + @FullTextField(analyzer = "MyLuceneAnalysisConfigurer") @NotNull @NotBlank @Column(name = "nombre", nullable = false) diff --git a/src/main/java/com/perales/sepomex/model/Estado.java b/src/main/java/com/perales/sepomex/model/Estado.java index df503d6..9876b6d 100644 --- a/src/main/java/com/perales/sepomex/model/Estado.java +++ b/src/main/java/com/perales/sepomex/model/Estado.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import lombok.ToString; import org.hibernate.search.annotations.*; +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; import javax.persistence.*; import javax.validation.constraints.NotBlank; diff --git a/src/main/java/com/perales/sepomex/model/InegiClaveCiudad.java b/src/main/java/com/perales/sepomex/model/InegiClaveCiudad.java index 6b9aa67..8aec898 100644 --- a/src/main/java/com/perales/sepomex/model/InegiClaveCiudad.java +++ b/src/main/java/com/perales/sepomex/model/InegiClaveCiudad.java @@ -44,4 +44,4 @@ public class InegiClaveCiudad implements Serializable { @OneToMany(mappedBy = "inegiClaveCiudad", fetch = FetchType.LAZY) private List colonias; -} \ No newline at end of file +} diff --git a/src/main/java/com/perales/sepomex/model/ZonaTipo.java b/src/main/java/com/perales/sepomex/model/ZonaTipo.java index 83654df..5d03f42 100644 --- a/src/main/java/com/perales/sepomex/model/ZonaTipo.java +++ b/src/main/java/com/perales/sepomex/model/ZonaTipo.java @@ -6,10 +6,8 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import org.hibernate.search.annotations.Analyzer; -import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Indexed; -import org.hibernate.search.annotations.Store; +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField; import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -39,9 +37,7 @@ public class ZonaTipo implements Serializable { @Column(name = "id") private Integer id; - - @Field(store = Store.YES) - @Field(name = "zonaTipoEs_beginEnd", store = Store.YES, analyzer = @Analyzer(definition = "es_beginEnd")) + @FullTextField(analyzer = "name") @NotNull @NotBlank @Column(name = "nombre", nullable = false) diff --git a/src/main/java/com/perales/sepomex/service/ColoniaService.java b/src/main/java/com/perales/sepomex/service/ColoniaService.java index 86dc2f4..7e4678b 100644 --- a/src/main/java/com/perales/sepomex/service/ColoniaService.java +++ b/src/main/java/com/perales/sepomex/service/ColoniaService.java @@ -6,15 +6,15 @@ import com.perales.sepomex.repository.ColoniaRepository; import com.perales.sepomex.util.Parser; import lombok.extern.log4j.Log4j2; -import org.apache.lucene.search.Query; import org.hibernate.Criteria; import org.hibernate.FetchMode; import org.hibernate.Session; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; +import org.hibernate.search.engine.search.query.SearchQuery; import org.hibernate.search.jpa.FullTextEntityManager; -import org.hibernate.search.jpa.Search; -import org.hibernate.search.query.dsl.QueryBuilder; +import org.hibernate.search.mapper.orm.Search; +import org.hibernate.search.mapper.orm.session.SearchSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -26,6 +26,10 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceUnit; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; +import javax.persistence.criteria.Root; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -35,7 +39,6 @@ import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; -import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -270,31 +273,21 @@ public void indexDb() throws InterruptedException { fullTextEntityManager.createIndexer().startAndWait(); } + /** + * Crea una busqueda de texto de hibernate search y la fusiona + * con una busqueda de SQL por medio de criteria. + * @param colonia con todos los parametros para realizar la busqueda + * @return una lista de colonias que coinciden con la busqueda. + */ public List search(Colonia colonia){ - FullTextEntityManager fullTextEntityManager - = Search.getFullTextEntityManager( em ); - QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory() - .buildQueryBuilder() - .forEntity(Colonia.class) - .get(); - - Query fuzzyQuery = queryBuilder - .keyword() - .fuzzy() - .onField("nombre") - .matching( colonia.getNombre()) - .createQuery(); - - org.hibernate.search.jpa.FullTextQuery jpaQuery - = fullTextEntityManager.createFullTextQuery(fuzzyQuery, Colonia.class); - - jpaQuery.setCriteriaQuery( createCriteriaSearch(colonia) ); - jpaQuery.setMaxResults(100); - jpaQuery.limitExecutionTimeTo(1l, TimeUnit.SECONDS); - List lista = jpaQuery.getResultList(); - fullTextEntityManager.close(); - em.close(); - return lista; + SearchSession searchSession = Search.session( em ); + SearchQuery searchQuery = searchSession.search(Colonia.class) + .where( f -> f.match() + .fields( "nombre" ) + .matching( colonia.getNombre() ); + javax.persistence.TypedQuery jpaQuery = Search.toJpaQuery( searchQuery ); + org.hibernate.query.Query ormQuery = Search.toOrmQuery( searchQuery ); + return null; } @SuppressWarnings("deprecated") @@ -312,6 +305,17 @@ private Criteria createCriteriaSearch(Colonia colonia){ if(colonia.getMunicipio() != null ){ criteria.add( Restrictions.eq( "municipio.id", colonia.getMunicipio().getId() ) ); } + + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(Colonia.class); + + Root rootColonia = cq.from(Colonia.class); + rootColonia.join("estado", JoinType.LEFT); + rootColonia.join("municipio", JoinType.LEFT); + rootColonia.join("codigoPostal", JoinType.LEFT); + + cq.select(rootColonia).where(); + return criteria; } @@ -322,4 +326,4 @@ public int createCriteriaSearchCount(Colonia colonia){ return count.intValue(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/perales/sepomex/util/MyLuceneAnalysisConfigurer.java b/src/main/java/com/perales/sepomex/util/MyLuceneAnalysisConfigurer.java new file mode 100644 index 0000000..d927d46 --- /dev/null +++ b/src/main/java/com/perales/sepomex/util/MyLuceneAnalysisConfigurer.java @@ -0,0 +1,21 @@ +package com.perales.sepomex.util; + +import org.apache.lucene.analysis.core.LowerCaseFilterFactory; +import org.apache.lucene.analysis.es.SpanishLightStemFilterFactory; +import org.apache.lucene.analysis.snowball.SnowballPorterFilterFactory; +import org.apache.lucene.analysis.standard.StandardTokenizerFactory; +import org.hibernate.search.backend.lucene.analysis.LuceneAnalysisConfigurationContext; +import org.hibernate.search.backend.lucene.analysis.LuceneAnalysisConfigurer; + +public class MyLuceneAnalysisConfigurer implements LuceneAnalysisConfigurer { + @Override + public void configure(LuceneAnalysisConfigurationContext context) { + context.analyzer( "spanish" ).custom() + .tokenizer( StandardTokenizerFactory.class ) + .tokenFilter( SpanishLightStemFilterFactory.class ) + .tokenFilter( LowerCaseFilterFactory.class ) + .tokenFilter( SnowballPorterFilterFactory.class ) + .param( "language", "Spanish" ) + .tokenFilter( LowerCaseFilterFactory.class ); + } +} diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index d986ae8..1b35d9f 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -12,7 +12,7 @@ spring.jpa.properties.hibernate.hbm2ddl.auto=none spring.jpa.properties.hibernate.jdbc.batch_size=25 spring.jpa.properties.hibernate.order_inserts=true spring.jpa.properties.hibernate.order_updates= true -spring.jpa.properties.hibernate.show_sql=true +spring.jpa.properties.hibernate.show_sql=false spring.jpa.properties.hibernate.search.default.directory_provider=filesystem spring.jpa.properties.hibernate.search.default.indexBase=sepomex-indices/ spring.datasource.hikari.maximum-pool-size=40 @@ -21,4 +21,4 @@ spring.datasource.hikari.minimum-idle=20 spring.datasource.url=jdbc:postgresql://localhost:5432/sepomex spring.datasource.username=sepomex spring.datasource.password=sepomex -debug=false \ No newline at end of file +debug=false