Skip to content

Commit

Permalink
* Salvando migracion
Browse files Browse the repository at this point in the history
  • Loading branch information
ripper2hl committed Apr 8, 2024
1 parent 52048a5 commit ef90d3d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 59 deletions.
17 changes: 13 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<javax.el.api.version>3.0.1-b06</javax.el.api.version>
<javax.el.version>3.0.1-b12</javax.el.version>
<jaxb.api.version>2.4.0-b180830.0359</jaxb.api.version>
<hibernate.search.orm.version>5.11.10.Final</hibernate.search.orm.version>
<hibernate.search.orm.version>6.0.10.Final</hibernate.search.orm.version>
<hamcrest.all.version>1.3</hamcrest.all.version>
<postgresql.version>42.4.2</postgresql.version>
<jakarta.validation.api>2.0.2</jakarta.validation.api>
Expand Down Expand Up @@ -113,11 +113,20 @@
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-mapper-orm</artifactId>
<version>${hibernate.search.orm.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-backend-lucene</artifactId>
<version>${hibernate.search.orm.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-v5migrationhelper-orm</artifactId>
<version>${hibernate.search.orm.version}</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/perales/sepomex/model/CodigoPostal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +64,4 @@ public class CodigoPostal implements Serializable {

@OneToMany(mappedBy = "codigoPostalAdministracionAsentamientoOficina", fetch = FetchType.LAZY)
private List<Colonia> coloniasCodigoPostalAdministracionAsentamientoOficina;
}
}
18 changes: 3 additions & 15 deletions src/main/java/com/perales/sepomex/model/Colonia.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@
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;
import javax.validation.constraints.NotNull;
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 = {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/perales/sepomex/model/Estado.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ public class InegiClaveCiudad implements Serializable {

@OneToMany(mappedBy = "inegiClaveCiudad", fetch = FetchType.LAZY)
private List<Colonia> colonias;
}
}
8 changes: 2 additions & 6 deletions src/main/java/com/perales/sepomex/model/ZonaTipo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 33 additions & 29 deletions src/main/java/com/perales/sepomex/service/ColoniaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Colonia> 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<Colonia> searchQuery = searchSession.search(Colonia.class)
.where( f -> f.match()
.fields( "nombre" )
.matching( colonia.getNombre() );
javax.persistence.TypedQuery<Colonia> jpaQuery = Search.toJpaQuery( searchQuery );
org.hibernate.query.Query<Colonia> ormQuery = Search.toOrmQuery( searchQuery );
return null;
}

@SuppressWarnings("deprecated")
Expand All @@ -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<Colonia> cq = cb.createQuery(Colonia.class);

Root<Colonia> 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;
}

Expand All @@ -322,4 +326,4 @@ public int createCriteriaSearchCount(Colonia colonia){
return count.intValue();
}

}
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
debug=false

0 comments on commit ef90d3d

Please sign in to comment.