private FullTextQuery getSearchRecommendedQuery(String searchTerm) throws ServiceException {
if (!StringUtils.hasText(searchTerm)) {
return null;
}
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
QueryBuilder artifactQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Artifact.class).get();
BooleanJunction<?> booleanJunction = artifactQueryBuilder.bool();
booleanJunction.must(artifactQueryBuilder
.keyword()
.onField(Binding.artifact().deprecationStatus().getPath())
.matching(ArtifactDeprecationStatus.NORMAL)
.createQuery());
try {
searchTerm = LuceneUtils.getSimilarityQuery(searchTerm, 0.8F);
String[] fields = new String[] {
Binding.artifact().artifactId().getPath(),
Binding.artifact().group().groupId().getPath()
};
Analyzer analyzer = Search.getFullTextEntityManager(getEntityManager()).getSearchFactory().getAnalyzer(Artifact.class);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, fields, analyzer);
parser.setDefaultOperator(MultiFieldQueryParser.AND_OPERATOR);
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(parser.parse(searchTerm), BooleanClause.Occur.MUST);
booleanJunction.must(booleanQuery);
} catch (ParseException e) {
throw new ServiceException(String.format("Error parsing request: %1$s", searchTerm), e);
}
return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), Artifact.class);
}