Search in sources :

Example 6 with SearchResults

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;
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) BioSafetyOption(org.jbei.ice.lib.shared.BioSafetyOption) SearchResults(org.jbei.ice.lib.dto.search.SearchResults) Entry(org.jbei.ice.storage.model.Entry) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) PartData(org.jbei.ice.lib.dto.entry.PartData) FullTextQuery(org.hibernate.search.FullTextQuery) QueryType(org.jbei.ice.lib.search.QueryType) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 7 with SearchResults

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());
}
Also used : SearchQuery(org.jbei.ice.lib.dto.search.SearchQuery) Account(org.jbei.ice.storage.model.Account) Entry(org.jbei.ice.storage.model.Entry) EntryCreator(org.jbei.ice.lib.entry.EntryCreator) PartData(org.jbei.ice.lib.dto.entry.PartData) PlasmidData(org.jbei.ice.lib.dto.entry.PlasmidData) SearchResults(org.jbei.ice.lib.dto.search.SearchResults) Test(org.junit.Test)

Aggregations

SearchResults (org.jbei.ice.lib.dto.search.SearchResults)7 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)4 Entry (org.jbei.ice.storage.model.Entry)4 Session (org.hibernate.Session)3 FullTextQuery (org.hibernate.search.FullTextQuery)3 FullTextSession (org.hibernate.search.FullTextSession)3 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)3 PartData (org.jbei.ice.lib.dto.entry.PartData)3 SearchQuery (org.jbei.ice.lib.dto.search.SearchQuery)3 LinkedList (java.util.LinkedList)2 TermContext (org.hibernate.search.query.dsl.TermContext)2 BioSafetyOption (org.jbei.ice.lib.shared.BioSafetyOption)2 EntryType (org.jbei.ice.lib.dto.entry.EntryType)1 PlasmidData (org.jbei.ice.lib.dto.entry.PlasmidData)1 FieldFilter (org.jbei.ice.lib.dto.search.FieldFilter)1 RegistryPartner (org.jbei.ice.lib.dto.web.RegistryPartner)1 EntryCreator (org.jbei.ice.lib.entry.EntryCreator)1 TaskStatus (org.jbei.ice.lib.executor.TaskStatus)1 QueryType (org.jbei.ice.lib.search.QueryType)1 SearchController (org.jbei.ice.lib.search.SearchController)1