Search in sources :

Example 1 with SearchQuery

use of org.jbei.ice.lib.dto.search.SearchQuery 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;
}
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) 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) FullTextQuery(org.hibernate.search.FullTextQuery) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 2 with SearchQuery

use of org.jbei.ice.lib.dto.search.SearchQuery 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;
}
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)

Example 3 with SearchQuery

use of org.jbei.ice.lib.dto.search.SearchQuery in project ice by JBEI.

the class SearchResource method search.

/**
     * Keyword search
     *
     * @param queryString keywords to search on
     * @param searchWeb   whether to perform a web of registry search or not
     * @param offset      result start
     * @param limit       result count upper limit
     * @param sort        result sort
     * @param asc         true if return results in ascending order, false otherwise
     * @return wrapper around list of search results conforming to query params
     */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response search(@QueryParam("q") final String queryString, @DefaultValue("false") @QueryParam("webSearch") final boolean searchWeb, @DefaultValue("0") @QueryParam("offset") final int offset, @DefaultValue("15") @QueryParam("limit") final int limit, @DefaultValue("relevance") @QueryParam("sort") final String sort, @DefaultValue("false") @QueryParam("asc") final boolean asc) {
    final String userId = getUserId();
    if (StringUtils.isEmpty(userId) && !searchWeb) {
        return super.respond(Response.Status.FORBIDDEN);
    }
    log(userId, "query \'" + queryString + '\'');
    final SearchQuery query = new SearchQuery();
    query.setQueryString(queryString);
    final SearchQuery.Parameters parameters = query.getParameters();
    parameters.setRetrieveCount(limit);
    parameters.setStart(offset);
    parameters.setSortAscending(asc);
    parameters.setSortField(ColumnField.valueOf(sort.toUpperCase()));
    final List<EntryType> types = Arrays.asList(EntryType.values());
    query.setEntryTypes(types);
    return super.respond(controller.runSearch(userId, query));
}
Also used : SearchQuery(org.jbei.ice.lib.dto.search.SearchQuery) EntryType(org.jbei.ice.lib.dto.entry.EntryType)

Example 4 with SearchQuery

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

SearchQuery (org.jbei.ice.lib.dto.search.SearchQuery)4 SearchResults (org.jbei.ice.lib.dto.search.SearchResults)3 Entry (org.jbei.ice.storage.model.Entry)3 Session (org.hibernate.Session)2 FullTextQuery (org.hibernate.search.FullTextQuery)2 FullTextSession (org.hibernate.search.FullTextSession)2 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)2 TermContext (org.hibernate.search.query.dsl.TermContext)2 EntryType (org.jbei.ice.lib.dto.entry.EntryType)2 PartData (org.jbei.ice.lib.dto.entry.PartData)2 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)2 PlasmidData (org.jbei.ice.lib.dto.entry.PlasmidData)1 FieldFilter (org.jbei.ice.lib.dto.search.FieldFilter)1 EntryCreator (org.jbei.ice.lib.entry.EntryCreator)1 BioSafetyOption (org.jbei.ice.lib.shared.BioSafetyOption)1 Account (org.jbei.ice.storage.model.Account)1 Test (org.junit.Test)1