Java Code Examples for org.hibernate.search.query.dsl.BooleanJunction#must()

The following examples show how to use org.hibernate.search.query.dsl.BooleanJunction#must() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectDaoImpl.java    From artifact-listener with Apache License 2.0 6 votes vote down vote up
private FullTextQuery getSearchByNameQuery(String searchTerm) {
	FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
	
	QueryBuilder projectQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
			.forEntity(Project.class).get();
	
	BooleanJunction<?> booleanJunction = projectQueryBuilder.bool();
	if (StringUtils.hasText(searchTerm)) {
		booleanJunction.must(projectQueryBuilder
				.keyword()
				.fuzzy().withPrefixLength(1).withThreshold(0.8F)
				.onField(Binding.project().name().getPath())
				.matching(searchTerm)
				.createQuery());
	} else {
		booleanJunction.must(projectQueryBuilder.all().createQuery());
	}
	
	return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), Project.class);
}
 
Example 2
Source File: UserDaoImpl.java    From artifact-listener with Apache License 2.0 6 votes vote down vote up
private FullTextQuery getSearchQuery(String searchTerm) {
	FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
	
	QueryBuilder userQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
			.forEntity(User.class).get();
	
	BooleanJunction<?> booleanJunction = userQueryBuilder.bool();
	
	if (StringUtils.hasText(searchTerm)) {
		booleanJunction.must(userQueryBuilder
				.keyword()
				.fuzzy().withPrefixLength(1).withThreshold(0.8F)
				.onField(Binding.user().userName().getPath())
				.andField(Binding.user().fullName().getPath())
				.matching(searchTerm)
				.createQuery());
	} else {
		booleanJunction.must(userQueryBuilder.all().createQuery());
	}
	
	return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), User.class);
}
 
Example 3
Source File: ArtifactDaoImpl.java    From artifact-listener with Apache License 2.0 5 votes vote down vote up
private FullTextQuery getSearchByNameQuery(String searchTerm, ArtifactDeprecationStatus deprecation) {
	FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
	
	QueryBuilder artifactQueryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
			.forEntity(Artifact.class).get();
	
	BooleanJunction<?> booleanJunction = artifactQueryBuilder.bool();

	if (deprecation != null) {
		booleanJunction.must(artifactQueryBuilder
				.keyword()
				.onField(Binding.artifact().deprecationStatus().getPath())
				.matching(deprecation)
				.createQuery());
	}
	
	if (StringUtils.hasText(searchTerm)) {
		booleanJunction.must(artifactQueryBuilder
				.keyword()
				.fuzzy().withPrefixLength(1).withThreshold(0.8F)
				.onField(Binding.artifact().artifactId().getPath())
				.andField(Binding.artifact().group().groupId().getPath())
				.matching(searchTerm)
				.createQuery());
	} else {
		booleanJunction.must(artifactQueryBuilder.all().createQuery());
	}
	
	return fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), Artifact.class);
}
 
Example 4
Source File: ArtifactDaoImpl.java    From artifact-listener with Apache License 2.0 5 votes vote down vote up
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, 2);
		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(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);
}
 
Example 5
Source File: CategoryRepositoryImpl.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Override
public Page<Category> search(CategorySearchRequest request, Pageable pageable) {
	FullTextEntityManager fullTextEntityManager =  Search.getFullTextEntityManager(entityManager);
	QueryBuilder qb = fullTextEntityManager.getSearchFactory()
			.buildQueryBuilder()
			.forEntity(Category.class)
			.get();
	
	@SuppressWarnings("rawtypes")
	BooleanJunction<BooleanJunction> junction = qb.bool();
	junction.must(qb.all().createQuery());

	if (StringUtils.hasText(request.getKeyword())) {
		Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms");
		String[] fields = new String[] {
				"name"
		};
		MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
		parser.setDefaultOperator(QueryParser.Operator.AND);
		Query query = null;
		try {
			query = parser.parse(request.getKeyword());
		}
		catch (ParseException e1) {
			try {
				query = parser.parse(QueryParser.escape(request.getKeyword()));
			}
			catch (ParseException e2) {
				throw new RuntimeException(e2);
			}
		}
		junction.must(query);
	}

	if (StringUtils.hasText(request.getLanguage())) {
		junction.must(qb.keyword().onField("language").matching(request.getLanguage()).createQuery());
	}

	Query searchQuery = junction.createQuery();
	
	Session session = (Session) entityManager.getDelegate();
	Criteria criteria = session.createCriteria(Category.class);

	Sort sort = new Sort(new SortField("sortName", SortField.Type.STRING));

	FullTextQuery persistenceQuery = fullTextEntityManager
			.createFullTextQuery(searchQuery, Category.class)
			.setCriteriaQuery(criteria)
			.setSort(sort);
	if (pageable.isPaged()) {
		persistenceQuery.setFirstResult((int) pageable.getOffset());
		persistenceQuery.setMaxResults(pageable.getPageSize());
	}

	int resultSize = persistenceQuery.getResultSize();

	@SuppressWarnings("unchecked")
	List<Category> results = persistenceQuery.getResultList();
	return new PageImpl<>(results, pageable, resultSize);
}
 
Example 6
Source File: UserRepositoryImpl.java    From wallride with Apache License 2.0 4 votes vote down vote up
private FullTextQuery buildFullTextQuery(UserSearchRequest request, Pageable pageable, Criteria criteria) {
		FullTextEntityManager fullTextEntityManager =  Search.getFullTextEntityManager(entityManager);
		QueryBuilder qb = fullTextEntityManager.getSearchFactory()
				.buildQueryBuilder()
				.forEntity(User.class)
				.get();

		@SuppressWarnings("rawtypes")
		BooleanJunction<BooleanJunction> junction = qb.bool();
		junction.must(qb.all().createQuery());

		if (StringUtils.hasText(request.getKeyword())) {
			Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms");
			String[] fields = new String[] {
					"loginId",
					"name.firstName", "name.lastName",
			};
			MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
			parser.setDefaultOperator(QueryParser.Operator.AND);
			Query query = null;
			try {
				query = parser.parse(request.getKeyword());
			}
			catch (ParseException e1) {
				try {
					query = parser.parse(QueryParser.escape(request.getKeyword()));
				}
				catch (ParseException e2) {
					throw new RuntimeException(e2);
				}
			}
			junction.must(query);
		}

		if (!CollectionUtils.isEmpty(request.getRoles())) {
			for (User.Role role : request.getRoles()) {
				junction.must(qb.keyword().onField("roles").matching(role).createQuery());
			}
		}

		Query searchQuery = junction.createQuery();

		Sort sort = new Sort(new SortField("sortId", SortField.Type.LONG, false));

		FullTextQuery persistenceQuery = fullTextEntityManager
				.createFullTextQuery(searchQuery, User.class)
				.setCriteriaQuery(criteria)
//				.setProjection("id")
				.setSort(sort);
		if (pageable.isPaged()) {
			persistenceQuery.setFirstResult((int) pageable.getOffset());
			persistenceQuery.setMaxResults(pageable.getPageSize());
		}
		return persistenceQuery;
	}
 
Example 7
Source File: TagRepositoryImpl.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Override
public Page<Tag> search(TagSearchRequest request, Pageable pageable) {
	FullTextEntityManager fullTextEntityManager =  Search.getFullTextEntityManager(entityManager);
	QueryBuilder qb = fullTextEntityManager.getSearchFactory()
			.buildQueryBuilder()
			.forEntity(Tag.class)
			.get();
	
	@SuppressWarnings("rawtypes")
	BooleanJunction<BooleanJunction> junction = qb.bool();
	junction.must(qb.all().createQuery());

	if (StringUtils.hasText(request.getKeyword())) {
		Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms");
		String[] fields = new String[] {
				"name"
		};
		MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
		parser.setDefaultOperator(QueryParser.Operator.AND);
		Query query = null;
		try {
			query = parser.parse(request.getKeyword());
		}
		catch (ParseException e1) {
			try {
				query = parser.parse(QueryParser.escape(request.getKeyword()));
			}
			catch (ParseException e2) {
				throw new RuntimeException(e2);
			}
		}
		junction.must(query);
	}

	if (StringUtils.hasText(request.getLanguage())) {
		junction.must(qb.keyword().onField("language").matching(request.getLanguage()).createQuery());
	}

	Query searchQuery = junction.createQuery();
	
	Session session = (Session) entityManager.getDelegate();
	Criteria criteria = session.createCriteria(Tag.class);

	Sort sort = new Sort(new SortField("sortName", SortField.Type.STRING));

	FullTextQuery persistenceQuery = fullTextEntityManager
			.createFullTextQuery(searchQuery, Tag.class)
			.setCriteriaQuery(criteria)
			.setSort(sort);
	persistenceQuery.setFirstResult((int) pageable.getOffset());
	persistenceQuery.setMaxResults(pageable.getPageSize());

	int resultSize = persistenceQuery.getResultSize();

	@SuppressWarnings("unchecked")
	List<Tag> results = persistenceQuery.getResultList();
	return new PageImpl<>(results, pageable, resultSize);
}
 
Example 8
Source File: CustomFieldRepositoryImpl.java    From wallride with Apache License 2.0 4 votes vote down vote up
public FullTextQuery buildFullTextQuery(CustomFieldSearchRequest request, Pageable pageable, Criteria criteria) {
	FullTextEntityManager fullTextEntityManager =  Search.getFullTextEntityManager(entityManager);
	QueryBuilder qb = fullTextEntityManager.getSearchFactory()
			.buildQueryBuilder()
			.forEntity(CustomField.class)
			.get();
	
	@SuppressWarnings("rawtypes")
	BooleanJunction<BooleanJunction> junction = qb.bool();
	junction.must(qb.all().createQuery());

	if (StringUtils.hasText(request.getKeyword())) {
		Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms");
		String[] fields = new String[] {
				"name", "code", "description"
		};
		MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
		parser.setDefaultOperator(QueryParser.Operator.AND);
		Query query = null;
		try {
			query = parser.parse(request.getKeyword());
		}
		catch (ParseException e1) {
			try {
				query = parser.parse(QueryParser.escape(request.getKeyword()));
			}
			catch (ParseException e2) {
				throw new RuntimeException(e2);
			}
		}
		junction.must(query);
	}

	if (StringUtils.hasText(request.getLanguage())) {
		junction.must(qb.keyword().onField("language").matching(request.getLanguage()).createQuery());
	}

	Query searchQuery = junction.createQuery();
	
	Sort sort = new Sort(new SortField("idx", SortField.Type.INT));

	FullTextQuery persistenceQuery = fullTextEntityManager
			.createFullTextQuery(searchQuery, CustomField.class)
			.setCriteriaQuery(criteria)
			.setSort(sort);
	if (pageable.isPaged()) {
		persistenceQuery.setFirstResult((int) pageable.getOffset());
		persistenceQuery.setMaxResults(pageable.getPageSize());
	}
	return persistenceQuery;
}