-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Inject Hibernate5Module to support JPA lazy objects in JSON * Json include transient field use @JsonProperty * Add tests for Products API * Update version to 0.2.3 * Move Jackson config location * Add ProductSearchDto * Remove @NotNull * Add valid flag * Rename * Draft * change indexBase * update hibernate search config * Use moreLikeThis() to implement searchByExample() * Update tests * Remove @EnableWebMvc * Fix CORS * Add addCorsMappings() in WebMvcConfig * Update docs
- Loading branch information
Showing
23 changed files
with
328 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
api-gate/src/main/java/com/llzw/apigate/persistence/dao/ProductSearchableRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.llzw.apigate.persistence.dao; | ||
|
||
import com.llzw.apigate.persistence.entity.Product; | ||
import java.util.List; | ||
|
||
public interface ProductSearchableRepository { | ||
|
||
List<Product> searchByNameWithCustomQuery(String text); | ||
|
||
List<Product> searchByIntroductionWithCustomQuery(String text); | ||
|
||
List<Product> searchByNameOrIntroductionWithCustomQuery(String text); | ||
|
||
List<Product> searchByExample(Object example); | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
api-gate/src/main/java/com/llzw/apigate/persistence/dao/ProductSearchableRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.llzw.apigate.persistence.dao; | ||
|
||
import com.llzw.apigate.persistence.entity.Product; | ||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import org.apache.lucene.search.Query; | ||
import org.hibernate.search.engine.ProjectionConstants; | ||
import org.hibernate.search.jpa.FullTextEntityManager; | ||
import org.hibernate.search.jpa.FullTextQuery; | ||
import org.hibernate.search.jpa.Search; | ||
import org.hibernate.search.query.dsl.QueryBuilder; | ||
|
||
public class ProductSearchableRepositoryImpl implements ProductSearchableRepository { | ||
|
||
private static boolean indexed = false; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
public List<Product> searchByNameWithCustomQuery(String text) { | ||
init(); | ||
Query query = getQueryBuilder() | ||
.simpleQueryString() | ||
.onFields("name") | ||
.matching(text) | ||
.createQuery(); | ||
return getJpaQuery(query).getResultList(); | ||
} | ||
|
||
public List<Product> searchByIntroductionWithCustomQuery(String text) { | ||
init(); | ||
Query query = getQueryBuilder() | ||
.simpleQueryString() | ||
.onFields("introduction") | ||
.matching(text) | ||
.createQuery(); | ||
return getJpaQuery(query).getResultList(); | ||
} | ||
|
||
public List<Product> searchByNameOrIntroductionWithCustomQuery(String text) { | ||
init(); | ||
Query combinedQuery = getQueryBuilder() | ||
.simpleQueryString() | ||
.onFields("name", "introduction") | ||
.matching(text) | ||
.createQuery(); | ||
return getJpaQuery(combinedQuery).getResultList(); | ||
} | ||
|
||
@Override | ||
public List<Product> searchByExample(Object example) { | ||
init(); | ||
Query query = getQueryBuilder() | ||
.moreLikeThis() | ||
.excludeEntityUsedForComparison() | ||
.comparingAllFields() | ||
.toEntity(example) | ||
.createQuery(); | ||
return getJpaQuery(query) | ||
.setProjection(ProjectionConstants.THIS) | ||
.getResultList(); | ||
} | ||
|
||
private FullTextQuery getJpaQuery(Query luceneQuery) { | ||
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); | ||
return fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class); | ||
} | ||
|
||
private QueryBuilder getQueryBuilder() { | ||
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); | ||
return fullTextEntityManager.getSearchFactory() | ||
.buildQueryBuilder() | ||
.forEntity(Product.class) | ||
.get(); | ||
} | ||
|
||
private void init() { | ||
if (indexed) { | ||
return; | ||
} | ||
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); | ||
try { | ||
fullTextEntityManager.createIndexer().startAndWait(); | ||
indexed = true; | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.