Search in sources :

Example 1 with QueryType

use of org.jbei.ice.lib.search.QueryType 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)

Aggregations

Session (org.hibernate.Session)1 FullTextQuery (org.hibernate.search.FullTextQuery)1 FullTextSession (org.hibernate.search.FullTextSession)1 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)1 PartData (org.jbei.ice.lib.dto.entry.PartData)1 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)1 SearchResults (org.jbei.ice.lib.dto.search.SearchResults)1 QueryType (org.jbei.ice.lib.search.QueryType)1 BioSafetyOption (org.jbei.ice.lib.shared.BioSafetyOption)1 Entry (org.jbei.ice.storage.model.Entry)1