use of org.jbei.ice.lib.dto.search.SearchResults 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;
}
use of org.jbei.ice.lib.dto.search.SearchResults in project ice by JBEI.
the class SearchControllerTest method testRunSearch.
@Test
public void testRunSearch() throws Exception {
Account account = AccountCreator.createTestAccount("testRunSearch", false);
Assert.assertNotNull(account);
// create entry
PlasmidData plasmidData = new PlasmidData();
plasmidData.setCircular(true);
plasmidData.setPromoters("pTet");
plasmidData.setOriginOfReplication("oRep");
PartData partData = new PartData(EntryType.PLASMID);
partData.setBioSafetyLevel(BioSafetyOption.LEVEL_ONE.ordinal());
partData.setStatus("Complete");
partData.setName("testPlasmid");
partData.setFundingSource("DOE");
partData.setPrincipalInvestigator("Nathan");
partData.setPlasmidData(plasmidData);
Entry entry = InfoToModelFactory.infoToEntry(partData);
entry = new EntryCreator().createEntry(account, entry, null);
Assert.assertNotNull(entry);
Assert.assertTrue(entry.getId() > 0);
// commit triggers indexing
HibernateUtil.commitTransaction();
HibernateUtil.beginTransaction();
SearchQuery query = new SearchQuery();
query.setQueryString("testPlasmid");
SearchResults results = controller.runSearch(account.getEmail(), query);
Assert.assertNotNull(results);
Assert.assertEquals(1, results.getResultCount());
// search for promoters
query.setQueryString("pTet");
results = controller.runSearch(account.getEmail(), query);
Assert.assertNotNull(results);
Assert.assertEquals(1, results.getResultCount());
// search email
query.setQueryString(account.getEmail());
results = controller.runSearch(account.getEmail(), query);
Assert.assertNotNull(results);
Assert.assertEquals(1, results.getResultCount());
// fake search
query.setQueryString("FAKE_SEARCH");
results = controller.runSearch(account.getEmail(), query);
Assert.assertNotNull(results);
Assert.assertEquals(0, results.getResultCount());
}
Aggregations