Search in sources :

Example 1 with FieldFilter

use of org.jbei.ice.lib.dto.search.FieldFilter 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<>(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);
    fullTextQuery.setProjection(ProjectionConstants.ID, "owner");
    // 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<>();
    // since we are using projection, result is an arraylist of objects
    for (Object object : result) {
        Object[] objects = (Object[]) object;
        long entryId = (Long) objects[0];
        SearchResult searchResult;
        if (blastResults != null) {
            searchResult = blastResults.get(Long.toString(entryId));
            if (// this should not really happen since we already filter
            searchResult == null)
                continue;
        } else {
            searchResult = new SearchResult();
            searchResult.setScore(1f);
            PartData info = ModelToInfoFactory.createTableView(entryId, null);
            info.setOwner((String) objects[1]);
            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;
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) SearchQuery(org.jbei.ice.lib.dto.search.SearchQuery) FullTextQuery(org.hibernate.search.FullTextQuery) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) BioSafetyOption(org.jbei.ice.lib.shared.BioSafetyOption) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) SearchResults(org.jbei.ice.lib.dto.search.SearchResults) TermContext(org.hibernate.search.query.dsl.TermContext) Entry(org.jbei.ice.storage.model.Entry) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) FieldFilter(org.jbei.ice.lib.dto.search.FieldFilter) EntryType(org.jbei.ice.lib.dto.entry.EntryType) PartData(org.jbei.ice.lib.dto.entry.PartData) FullTextQuery(org.hibernate.search.FullTextQuery) 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 TermContext (org.hibernate.search.query.dsl.TermContext)1 EntryType (org.jbei.ice.lib.dto.entry.EntryType)1 PartData (org.jbei.ice.lib.dto.entry.PartData)1 FieldFilter (org.jbei.ice.lib.dto.search.FieldFilter)1 SearchQuery (org.jbei.ice.lib.dto.search.SearchQuery)1 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)1 SearchResults (org.jbei.ice.lib.dto.search.SearchResults)1 BioSafetyOption (org.jbei.ice.lib.shared.BioSafetyOption)1 Entry (org.jbei.ice.storage.model.Entry)1