Java Code Examples for org.apache.lucene.queryparser.classic.MultiFieldQueryParser#setDefaultOperator()

The following examples show how to use org.apache.lucene.queryparser.classic.MultiFieldQueryParser#setDefaultOperator() . 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: 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 2
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 3
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 4
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 5
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;
}