Hi everyone.
I have problem implementing hibernate search 5 %like% sql.
I have a “title” field on the “Email” class.
With some case search below I want to return the result as “PI_030120220659025592.xml” but the result returned is not as expected.
Search -> Result
PI_03012022065 -> PI_030120220659025592.xml
PI_030120220650 -> not found
592.xml -> not found
92.xml -> another result
Code implement:
@Entity
@Table(name = "emails")
@Getter
@Setter
@NoArgsConstructor(force = true)
@AllArgsConstructor
@Builder
@Indexed
public class Email implements Serializable {
private static final long serialVersionUID = 5522175857108981474L;
@Field(analyzer = @Analyzer(definition = "searchTextAnalyzer"))
private String title;
}
-------------
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Email.class).get();
BooleanJunction<BooleanJunction> bool = qb.bool();
Map<String, String> map = new HashMap<>();
map.put("minGramSize", "1");
map.put("maxGramSize", "100");
Analyzer analyzer = CustomAnalyzer.builder()
.withTokenizer( WhitespaceTokenizerFactory.class )
.addTokenFilter( LowerCaseFilterFactory.class )
.addTokenFilter( ASCIIFoldingFilterFactory.class )
.addTokenFilter(EdgeNGramFilterFactory.class, map)
.build();
List<String> keywords = this.tokenizeString(analyzer, title);
for (String keyword : keywords) {
bool.must(qb.keyword().boostedTo(5f).wildcard().onFields("title").matching("" + keyword + "").createQuery());
}
---------------------------
public List<String> tokenizeString(Analyzer analyzer, String string) {
List<String> result = new ArrayList<>();
try {
TokenStream stream = analyzer.tokenStream(null, new StringReader(string));
stream.reset();
while (stream.incrementToken()) {
result.add(stream.getAttribute(CharTermAttribute.class).toString().toLowerCase());
}
stream.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
Can someone help me figure out where the problem is?
Thank you.