Java Code Examples for org.hibernate.search.jpa.FullTextQuery#setSort()

The following examples show how to use org.hibernate.search.jpa.FullTextQuery#setSort() . 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
@SuppressWarnings("unchecked")
@Override
public List<Project> searchByName(String searchTerm, Integer limit, Integer offset) {
	FullTextQuery query = getSearchByNameQuery(searchTerm);
	
	// Sort
	List<SortField> sortFields = ImmutableList.<SortField>builder()
			.add(new SortField(Project.NAME_SORT_FIELD_NAME, SortField.Type.STRING))
			.build();
	query.setSort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
	
	if (offset != null) {
		query.setFirstResult(offset);
	}
	if (limit != null) {
		query.setMaxResults(limit);
	}
	
	return (List<Project>) query.getResultList();
}
 
Example 2
Source File: UserDaoImpl.java    From artifact-listener with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<User> search(String searchTerm, Integer limit, Integer offset) {
	FullTextQuery query = getSearchQuery(searchTerm);
	
	query.setSort(new Sort(new SortField(Binding.user().userName().getPath(), SortField.Type.STRING)));
	
	if (offset != null) {
		query.setFirstResult(offset);
	}
	if (limit != null) {
		query.setMaxResults(limit);
	}
	
	return (List<User>) query.getResultList();
}
 
Example 3
Source File: ArtifactDaoImpl.java    From artifact-listener with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Artifact> searchByName(String searchTerm, ArtifactDeprecationStatus deprecation, Integer limit, Integer offset) {
	FullTextQuery query = getSearchByNameQuery(searchTerm, deprecation);
	
	// Sort
	List<SortField> sortFields = ImmutableList.<SortField>builder()
			.add(new SortField(Binding.artifact().group().getPath() + '.' + ArtifactGroup.GROUP_ID_SORT_FIELD_NAME, SortField.Type.STRING))
			.add(new SortField(Artifact.ARTIFACT_ID_SORT_FIELD_NAME, SortField.Type.STRING))
			.build();
	query.setSort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
	
	if (offset != null) {
		query.setFirstResult(offset);
	}
	if (limit != null) {
		query.setMaxResults(limit);
	}
	
	return (List<Artifact>) query.getResultList();
}
 
Example 4
Source File: ArtifactDaoImpl.java    From artifact-listener with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Artifact> searchRecommended(String searchTerm, Integer limit, Integer offset) throws ServiceException {
	FullTextQuery query = getSearchRecommendedQuery(searchTerm);
	if (query == null) {
		return Lists.newArrayListWithExpectedSize(0);
	}
	
	// Sort
	List<SortField> sortFields = ImmutableList.<SortField>builder()
			.add(new SortField(Binding.artifact().followersCount().getPath(), SortField.Type.LONG, true))
			.add(new SortField(Binding.artifact().group().getPath() + '.' + ArtifactGroup.GROUP_ID_SORT_FIELD_NAME, SortField.Type.STRING))
			.add(new SortField(Artifact.ARTIFACT_ID_SORT_FIELD_NAME, SortField.Type.STRING))
			.build();
	
	query.setSort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
	
	if (offset != null) {
		query.setFirstResult(offset);
	}
	if (limit != null) {
		query.setMaxResults(limit);
	}
	
	return (List<Artifact>) query.getResultList();
}
 
Example 5
Source File: BookDaoImpl.java    From maven-framework-project with MIT License 4 votes vote down vote up
@Override
public QueryResult<Book> query(String keyword, int start, int pagesize,Analyzer analyzer,String...field) throws Exception{
	
	QueryResult<Book> queryResult=new QueryResult<Book>();
	
	List<Book> books=new ArrayList<Book>();
	
	FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
	
	//使用Hibernate Search api查询 从多个字段匹配 name、description、authors.name
	//QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Book.class ).get();
	//Query luceneQuery = qb.keyword().onFields(field).matching(keyword).createQuery();

	//使用lucene api查询 从多个字段匹配 name、description、authors.name
	
	MultiFieldQueryParser queryParser=new MultiFieldQueryParser(Version.LUCENE_36,new String[]{"name","description","authors.name"}, analyzer);
	Query luceneQuery=queryParser.parse(keyword);
	
	FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery);
	int searchresultsize = fullTextQuery.getResultSize();
	queryResult.setSearchresultsize(searchresultsize);
			
	fullTextQuery.setFirstResult(start);
	fullTextQuery.setMaxResults(pagesize);
	
	//设置按id排序
	fullTextQuery.setSort(new Sort(new SortField("id", SortField.INT ,true)));
	
	//高亮设置
	SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
	QueryScorer queryScorer=new QueryScorer(luceneQuery);
	Highlighter highlighter=new Highlighter(formatter, queryScorer);

	@SuppressWarnings("unchecked")
	List<Book> tempresult = fullTextQuery.getResultList();
	for (Book book : tempresult) {
		String highlighterString=null;
		try {
			//高亮name
			highlighterString=highlighter.getBestFragment(analyzer, "name", book.getName());
			if(highlighterString!=null){
				book.setName(highlighterString);
			}
			//高亮authors.name
			Set<Author> authors = book.getAuthors();
			for (Author author : authors) {
				highlighterString=highlighter.getBestFragment(analyzer, "authors.name", author.getName());
				if(highlighterString!=null){
					author.setName(highlighterString);
				}
			}
			//高亮description
			highlighterString=highlighter.getBestFragment(analyzer, "description", book.getDescription());
			if(highlighterString!=null){
				book.setDescription(highlighterString);
			}
		} catch (Exception e) {
		}
		
		books.add(book);	
		
	}
	
	queryResult.setSearchresult(books);
	
	return queryResult;
}