use of org.hibernate.search.FullTextQuery in project ice by JBEI.
the class HibernateSearch method filterBlastResults.
/**
* Intended to be called after running a blast search to filter the results. Blast search runs
* on a fasta file which contains all the sequences. Hibernate search has a security filter for permissions
* and is therefore used to filter out the blast results, as well as to filter based on the entry
* attributes not handled by blast; such as "has sequence" and "bio-safety level"
*
* @param userId identifier for account of user performing search
* @param start paging start
* @param count maximum number of results to return
* @param blastResults raw results of the blast search
* @return wrapper around list of filtered results
*/
public SearchResults filterBlastResults(String userId, int start, int count, SearchQuery searchQuery, final HashMap<String, SearchResult> blastResults) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Entry.class).get();
Query query = qb.keyword().onField("visibility").matching(Visibility.OK.getValue()).createQuery();
// todo : there is a limit of 1024 boolean clauses so return only return top blast results
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(query, BooleanClause.Occur.FILTER);
// wrap Lucene query in a org.hibernate.Query
Class<?>[] classes = SearchFieldFactory.classesForTypes(searchQuery.getEntryTypes());
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), classes);
// enable security filter if an admin
checkEnableSecurityFilter(userId, fullTextQuery);
// enable has attachment/sequence/sample (if needed)
checkEnableHasAttribute(fullTextQuery, searchQuery.getParameters());
// bio-safety level
if (searchQuery.getBioSafetyOption() != null) {
TermContext levelContext = qb.keyword();
Query biosafetyQuery = levelContext.onField("bioSafetyLevel").ignoreFieldBridge().matching(searchQuery.getBioSafetyOption().getValue()).createQuery();
builder.add(biosafetyQuery, BooleanClause.Occur.MUST);
}
// execute search
fullTextQuery.setProjection("id");
// list contains an object array with one Long object
List luceneResult = fullTextQuery.list();
HashSet<String> resultSet = new HashSet<>();
// page
for (Object object : luceneResult) {
Long result = (Long) ((Object[]) object)[0];
resultSet.add(result.toString());
}
Iterator<String> iterator = blastResults.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (!resultSet.contains(key))
iterator.remove();
}
SearchResult[] searchResults = new SearchResult[count];
int limit = (start + count) > blastResults.size() ? blastResults.size() : (start + count);
LinkedList<SearchResult> list = new LinkedList<>(Arrays.asList(blastResults.values().toArray(searchResults)).subList(start, limit));
SearchResults results = new SearchResults();
results.setResultCount(blastResults.size());
results.setResults(list);
return results;
}
use of org.hibernate.search.FullTextQuery in project querydsl by querydsl.
the class AbstractSearchQuery method createQuery.
private FullTextQuery createQuery(boolean forCount) {
QueryMetadata metadata = queryMixin.getMetadata();
org.apache.lucene.search.Query query;
if (metadata.getWhere() != null) {
query = serializer.toQuery(metadata.getWhere(), metadata);
} else {
query = new MatchAllDocsQuery();
}
FullTextQuery fullTextQuery = session.createFullTextQuery(query, path.getType());
// order
if (!metadata.getOrderBy().isEmpty() && !forCount) {
fullTextQuery.setSort(serializer.toSort(metadata.getOrderBy()));
}
// paging
QueryModifiers modifiers = metadata.getModifiers();
if (modifiers.isRestricting() && !forCount) {
Integer limit = modifiers.getLimitAsInteger();
Integer offset = modifiers.getOffsetAsInteger();
if (limit != null) {
fullTextQuery.setMaxResults(limit.intValue());
}
if (offset != null) {
fullTextQuery.setFirstResult(offset.intValue());
}
}
return fullTextQuery;
}
use of org.hibernate.search.FullTextQuery in project ice by JBEI.
the class HibernateSearch method executeSearchNoTerms.
public SearchResults executeSearchNoTerms(String userId, HashMap<String, SearchResult> blastResults, SearchQuery searchQuery) {
ArrayList<EntryType> entryTypes = searchQuery.getEntryTypes();
if (entryTypes == null || entryTypes.isEmpty()) {
entryTypes = new ArrayList<>();
entryTypes.addAll(Arrays.asList(EntryType.values()));
}
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
int resultCount;
FullTextSession fullTextSession = Search.getFullTextSession(session);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Entry.class).get();
ArrayList<Query> except = new ArrayList<>();
for (EntryType type : EntryType.values()) {
if (entryTypes.contains(type))
continue;
except.add(qb.keyword().onField("recordType").matching(type.getName()).createQuery());
}
// add terms for record types
Query[] queries = new Query[] {};
Query recordTypeQuery = qb.all().except(except.toArray(queries)).createQuery();
builder.add(recordTypeQuery, BooleanClause.Occur.FILTER);
// visibility
Query visibilityQuery = qb.keyword().onField("visibility").matching(Visibility.OK.getValue()).createQuery();
builder.add(visibilityQuery, BooleanClause.Occur.FILTER);
// bio safety level
BioSafetyOption option = searchQuery.getBioSafetyOption();
if (option != null) {
TermContext bslContext = qb.keyword();
Query biosafetyQuery = bslContext.onField("bioSafetyLevel").ignoreFieldBridge().matching(option.getIntValue()).createQuery();
builder.add(biosafetyQuery, BooleanClause.Occur.FILTER);
}
// check filter filters
if (searchQuery.getFieldFilters() != null && !searchQuery.getFieldFilters().isEmpty()) {
for (FieldFilter fieldFilter : searchQuery.getFieldFilters()) {
String searchField = SearchFieldFactory.searchFieldForEntryField(fieldFilter.getField());
if (StringUtils.isEmpty(searchField))
continue;
Query filterQuery = qb.keyword().onField(searchField).matching(fieldFilter.getFilter()).createQuery();
builder.add(filterQuery, BooleanClause.Occur.MUST);
}
}
// check if there is a blast results
createBlastFilterQuery(fullTextSession, blastResults, builder);
// wrap Lucene query in a org.hibernate.Query
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Entry.class);
// get sorting values
Sort sort = getSort(searchQuery.getParameters().isSortAscending(), searchQuery.getParameters().getSortField());
fullTextQuery.setSort(sort);
// enable security filter if needed
checkEnableSecurityFilter(userId, fullTextQuery);
// enable has attachment/sequence/sample (if needed)
checkEnableHasAttribute(fullTextQuery, searchQuery.getParameters());
// set paging params
fullTextQuery.setFirstResult(searchQuery.getParameters().getStart());
fullTextQuery.setMaxResults(searchQuery.getParameters().getRetrieveCount());
resultCount = fullTextQuery.getResultSize();
List result = fullTextQuery.list();
LinkedList<SearchResult> searchResults = new LinkedList<>();
for (Object object : result) {
Entry entry = (Entry) object;
SearchResult searchResult;
if (blastResults != null) {
searchResult = blastResults.get(Long.toString(entry.getId()));
if (// this should not really happen since we already filter
searchResult == null)
continue;
} else {
searchResult = new SearchResult();
searchResult.setScore(1f);
PartData info = ModelToInfoFactory.createTableViewData(userId, entry, true);
searchResult.setEntryInfo(info);
}
searchResult.setMaxScore(1f);
searchResults.add(searchResult);
}
SearchResults results = new SearchResults();
results.setResultCount(resultCount);
results.setResults(searchResults);
Logger.info(userId + ": obtained " + resultCount + " results for empty query");
return results;
}
use of org.hibernate.search.FullTextQuery in project ice by JBEI.
the class HibernateSearch method executeSearch.
@SuppressWarnings("unchecked")
public SearchResults executeSearch(String userId, HashMap<String, QueryType> terms, SearchQuery searchQuery, HashMap<String, SearchResult> blastResults) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
int resultCount;
FullTextSession fullTextSession = Search.getFullTextSession(session);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
// get classes for search
HashSet<String> fields = new HashSet<>();
fields.addAll(SearchFieldFactory.entryFields(searchQuery.getEntryTypes()));
Class<?>[] classes = SearchFieldFactory.classesForTypes(searchQuery.getEntryTypes());
// generate queries for terms filtering stop words
for (Map.Entry<String, QueryType> entry : terms.entrySet()) {
String term = cleanQuery(entry.getKey());
if (term.trim().isEmpty() || StandardAnalyzer.STOP_WORDS_SET.contains(term))
continue;
BioSafetyOption safetyOption = searchQuery.getBioSafetyOption();
generateQueriesForType(fullTextSession, fields, builder, term, entry.getValue(), safetyOption);
}
// check for blast search results filter
createBlastFilterQuery(fullTextSession, blastResults, builder);
// wrap Lucene query in a org.hibernate.Query
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), classes);
// get max score
fullTextQuery.setFirstResult(0);
fullTextQuery.setMaxResults(1);
fullTextQuery.setProjection(FullTextQuery.SCORE);
List result = fullTextQuery.list();
float maxScore = -1f;
if (result.size() == 1) {
maxScore = (Float) ((Object[]) (result.get(0)))[0];
}
// end get max score
// get sorting values
Sort sort = getSort(searchQuery.getParameters().isSortAscending(), searchQuery.getParameters().getSortField());
fullTextQuery.setSort(sort);
// projection (specified properties must be stored in the index @Field(store=Store.YES))
fullTextQuery.setProjection(FullTextQuery.SCORE, FullTextQuery.THIS);
// enable security filter if needed
fullTextQuery = checkEnableSecurityFilter(userId, fullTextQuery);
// check sample
checkEnableHasAttribute(fullTextQuery, searchQuery.getParameters());
// set paging params
fullTextQuery.setFirstResult(searchQuery.getParameters().getStart());
fullTextQuery.setMaxResults(searchQuery.getParameters().getRetrieveCount());
resultCount = fullTextQuery.getResultSize();
// execute search
result = fullTextQuery.list();
Logger.info(resultCount + " results for \"" + searchQuery.getQueryString() + "\"");
LinkedList<SearchResult> searchResults = new LinkedList<>();
for (Object[] objects : (Iterable<Object[]>) result) {
float score = (Float) objects[0];
Entry entry = (Entry) objects[1];
SearchResult searchResult;
if (blastResults != null) {
searchResult = blastResults.get(Long.toString(entry.getId()));
if (// this should not really happen since we already filter
searchResult == null)
continue;
} else {
searchResult = new SearchResult();
searchResult.setScore(score);
PartData info = ModelToInfoFactory.createTableViewData(userId, entry, true);
if (info == null)
continue;
searchResult.setEntryInfo(info);
}
searchResult.setMaxScore(maxScore);
searchResults.add(searchResult);
}
SearchResults results = new SearchResults();
results.setResultCount(resultCount);
results.setResults(searchResults);
return results;
}
Aggregations