|
| 1 | +package com.baeldung.hibernatesearch; |
| 2 | + |
| 3 | +import com.baeldung.hibernatesearch.model.Product; |
| 4 | +import org.apache.lucene.search.Query; |
| 5 | +import org.hibernate.search.engine.ProjectionConstants; |
| 6 | +import org.hibernate.search.jpa.FullTextEntityManager; |
| 7 | +import org.hibernate.search.jpa.FullTextQuery; |
| 8 | +import org.hibernate.search.jpa.Search; |
| 9 | +import org.hibernate.search.query.dsl.QueryBuilder; |
| 10 | +import org.springframework.stereotype.Repository; |
| 11 | + |
| 12 | +import javax.persistence.EntityManager; |
| 13 | +import javax.persistence.PersistenceContext; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Repository |
| 17 | +public class ProductSearchDao { |
| 18 | + |
| 19 | + @PersistenceContext |
| 20 | + private EntityManager entityManager; |
| 21 | + |
| 22 | + public List<Product> searchProductNameByKeywordQuery(String text) { |
| 23 | + |
| 24 | + Query keywordQuery = getQueryBuilder() |
| 25 | + .keyword() |
| 26 | + .onField("productName") |
| 27 | + .matching(text) |
| 28 | + .createQuery(); |
| 29 | + |
| 30 | + List<Product> results = getJpaQuery(keywordQuery).getResultList(); |
| 31 | + |
| 32 | + return results; |
| 33 | + } |
| 34 | + |
| 35 | + public List<Product> searchProductNameByFuzzyQuery(String text) { |
| 36 | + |
| 37 | + Query fuzzyQuery = getQueryBuilder() |
| 38 | + .keyword() |
| 39 | + .fuzzy() |
| 40 | + .withEditDistanceUpTo(2) |
| 41 | + .withPrefixLength(0) |
| 42 | + .onField("productName") |
| 43 | + .matching(text) |
| 44 | + .createQuery(); |
| 45 | + |
| 46 | + List<Product> results = getJpaQuery(fuzzyQuery).getResultList(); |
| 47 | + |
| 48 | + return results; |
| 49 | + } |
| 50 | + |
| 51 | + public List<Product> searchProductNameByWildcardQuery(String text) { |
| 52 | + |
| 53 | + Query wildcardQuery = getQueryBuilder() |
| 54 | + .keyword() |
| 55 | + .wildcard() |
| 56 | + .onField("productName") |
| 57 | + .matching(text) |
| 58 | + .createQuery(); |
| 59 | + |
| 60 | + List<Product> results = getJpaQuery(wildcardQuery).getResultList(); |
| 61 | + |
| 62 | + return results; |
| 63 | + } |
| 64 | + |
| 65 | + public List<Product> searchProductDescriptionByPhraseQuery(String text) { |
| 66 | + |
| 67 | + Query phraseQuery = getQueryBuilder() |
| 68 | + .phrase() |
| 69 | + .withSlop(1) |
| 70 | + .onField("description") |
| 71 | + .sentence(text) |
| 72 | + .createQuery(); |
| 73 | + |
| 74 | + List<Product> results = getJpaQuery(phraseQuery).getResultList(); |
| 75 | + |
| 76 | + return results; |
| 77 | + } |
| 78 | + |
| 79 | + public List<Product> searchProductNameAndDescriptionBySimpleQueryStringQuery(String text) { |
| 80 | + |
| 81 | + Query simpleQueryStringQuery = getQueryBuilder() |
| 82 | + .simpleQueryString() |
| 83 | + .onFields("productName", "description") |
| 84 | + .matching(text) |
| 85 | + .createQuery(); |
| 86 | + |
| 87 | + List<Product> results = getJpaQuery(simpleQueryStringQuery).getResultList(); |
| 88 | + |
| 89 | + return results; |
| 90 | + } |
| 91 | + |
| 92 | + public List<Product> searchProductNameByRangeQuery(int low, int high) { |
| 93 | + |
| 94 | + Query rangeQuery = getQueryBuilder() |
| 95 | + .range() |
| 96 | + .onField("memory") |
| 97 | + .from(low) |
| 98 | + .to(high) |
| 99 | + .createQuery(); |
| 100 | + |
| 101 | + List<Product> results = getJpaQuery(rangeQuery).getResultList(); |
| 102 | + |
| 103 | + return results; |
| 104 | + } |
| 105 | + |
| 106 | + public List<Object[]> searchProductNameByMoreLikeThisQuery(Product entity) { |
| 107 | + |
| 108 | + Query moreLikeThisQuery = getQueryBuilder() |
| 109 | + .moreLikeThis() |
| 110 | + .comparingField("productName") |
| 111 | + .toEntity(entity) |
| 112 | + .createQuery(); |
| 113 | + |
| 114 | + List<Object[]> results = getJpaQuery(moreLikeThisQuery).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE) |
| 115 | + .getResultList(); |
| 116 | + |
| 117 | + return results; |
| 118 | + } |
| 119 | + |
| 120 | + public List<Product> searchProductNameAndDescriptionByKeywordQuery(String text) { |
| 121 | + |
| 122 | + Query keywordQuery = getQueryBuilder() |
| 123 | + .keyword() |
| 124 | + .onFields("productName", "description") |
| 125 | + .matching(text) |
| 126 | + .createQuery(); |
| 127 | + |
| 128 | + List<Product> results = getJpaQuery(keywordQuery).getResultList(); |
| 129 | + |
| 130 | + return results; |
| 131 | + } |
| 132 | + |
| 133 | + public List<Object[]> searchProductNameAndDescriptionByMoreLikeThisQuery(Product entity) { |
| 134 | + |
| 135 | + Query moreLikeThisQuery = getQueryBuilder() |
| 136 | + .moreLikeThis() |
| 137 | + .comparingField("productName") |
| 138 | + .toEntity(entity) |
| 139 | + .createQuery(); |
| 140 | + |
| 141 | + List<Object[]> results = getJpaQuery(moreLikeThisQuery).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE) |
| 142 | + .getResultList(); |
| 143 | + |
| 144 | + return results; |
| 145 | + } |
| 146 | + |
| 147 | + public List<Product> searchProductNameAndDescriptionByCombinedQuery(String manufactorer, int memoryLow, int memoryTop, String extraFeature, String exclude) { |
| 148 | + |
| 149 | + Query combinedQuery = getQueryBuilder() |
| 150 | + .bool() |
| 151 | + .must(getQueryBuilder().keyword() |
| 152 | + .onField("productName") |
| 153 | + .matching(manufactorer) |
| 154 | + .createQuery()) |
| 155 | + .must(getQueryBuilder() |
| 156 | + .range() |
| 157 | + .onField("memory") |
| 158 | + .from(memoryLow) |
| 159 | + .to(memoryTop) |
| 160 | + .createQuery()) |
| 161 | + .should(getQueryBuilder() |
| 162 | + .phrase() |
| 163 | + .onField("description") |
| 164 | + .sentence(extraFeature) |
| 165 | + .createQuery()) |
| 166 | + .must(getQueryBuilder() |
| 167 | + .keyword() |
| 168 | + .onField("productName") |
| 169 | + .matching(exclude) |
| 170 | + .createQuery()) |
| 171 | + .not() |
| 172 | + .createQuery(); |
| 173 | + |
| 174 | + List<Product> results = getJpaQuery(combinedQuery).getResultList(); |
| 175 | + |
| 176 | + return results; |
| 177 | + } |
| 178 | + |
| 179 | + private FullTextQuery getJpaQuery(org.apache.lucene.search.Query luceneQuery) { |
| 180 | + |
| 181 | + FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); |
| 182 | + |
| 183 | + return fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class); |
| 184 | + } |
| 185 | + |
| 186 | + private QueryBuilder getQueryBuilder() { |
| 187 | + |
| 188 | + FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); |
| 189 | + |
| 190 | + return fullTextEntityManager.getSearchFactory() |
| 191 | + .buildQueryBuilder() |
| 192 | + .forEntity(Product.class) |
| 193 | + .get(); |
| 194 | + } |
| 195 | +} |
0 commit comments