SearchEngine implements Indexer, Searcher { public SearchResult search(String query, int page, Class<? extends BioPAXElement> filterByType, String[] datasources, String[] organisms) { SearchResult response = null; LOG.debug("search: " + query + ", page: " + page + ", filterBy: " + filterByType + "; extra filters: ds in (" + Arrays.toString(datasources) + "), org. in (" + Arrays.toString(organisms) + ")"); IndexSearcher searcher = null; try { QueryParser queryParser = new MultiFieldQueryParser(DEFAULT_FIELDS, analyzer); queryParser.setAllowLeadingWildcard(true); searcher = searcherManager.acquire(); if(!query.trim().equals("*")) { Query luceneQuery = queryParser.parse(query); LOG.debug("parsed lucene query is " + luceneQuery.getClass().getSimpleName()); Filter filter = createFilter(filterByType, datasources, organisms); TopDocs topDocs = searcher.search(luceneQuery, filter, maxHitsPerPage); if(page>0) { TopScoreDocCollector collector = TopScoreDocCollector.create(maxHitsPerPage*(page+1), true); searcher.search(luceneQuery, filter, collector); topDocs = collector.topDocs(page * maxHitsPerPage, maxHitsPerPage); } response = transform(luceneQuery, searcher, true, topDocs); } else { if(filterByType==null) filterByType = Level3Element.class; BooleanQuery luceneQuery = new BooleanQuery(); for(Class<? extends BioPAXElement> subType : SimpleEditorMap.L3.getKnownSubClassesOf(filterByType)) { luceneQuery.add(new TermQuery(new Term(FIELD_TYPE, subType.getSimpleName().toLowerCase())), Occur.SHOULD); } Filter filter = createFilter(null, datasources, organisms); TopDocs topDocs = searcher.search(luceneQuery, filter, maxHitsPerPage); if(page>0) { TopScoreDocCollector collector = TopScoreDocCollector.create(maxHitsPerPage*(page+1), true); searcher.search(luceneQuery, filter, collector); topDocs = collector.topDocs(page * maxHitsPerPage, maxHitsPerPage); } response = transform(luceneQuery, searcher, false, topDocs); } } catch (ParseException e) { throw new RuntimeException("getTopDocs: failed to parse the query string.", e); } catch (IOException e) { throw new RuntimeException("getTopDocs: failed.", e); } finally { try { if(searcher!=null) { searcherManager.release(searcher); searcher = null; } } catch (IOException e) {} } response.setPage(page); return response; } SearchEngine(Model model, String indexLocation); void setMaxHitsPerPage(int maxHitsPerPage); int getMaxHitsPerPage(); SearchResult search(String query, int page,
			Class<? extends BioPAXElement> filterByType, String[] datasources,
			String[] organisms); void index(); static final String FIELD_URI; static final String FIELD_KEYWORD; static final String FIELD_NAME; static final String FIELD_XREFID; static final String FIELD_PATHWAY; static final String FIELD_N_PARTICIPANTS; static final String FIELD_N_PROCESSES; static final String FIELD_N; static final String FIELD_ORGANISM; static final String FIELD_DATASOURCE; static final String FIELD_TYPE; final static String[] DEFAULT_FIELDS; final static int DEFAULT_MAX_HITS_PER_PAGE; }
@Test public final void testSearch() throws IOException { SimpleIOHandler reader = new SimpleIOHandler(); Model model = reader.convertFromOWL(getClass().getResourceAsStream("/pathwaydata1.owl")); SearchEngine searchEngine = new SearchEngine(model, indexLocation + "index1"); searchEngine.index(); assertTrue(new File(indexLocation + "index1").exists()); SearchResult response = searchEngine.search("ATP", 0, null, null, null); assertNotNull(response); assertFalse(response.getHits().isEmpty()); assertEquals(7, response.getHits().size()); assertEquals(7, response.getTotalHits()); response = searchEngine.search("ATP", 0, Interaction.class, null, null); assertNotNull(response); assertEquals(2, response.getHits().size()); response = searchEngine.search("ATP", 0, Pathway.class, null, null); assertNotNull(response); assertEquals(1, response.getHits().size()); BioPAXElement hit = response.getHits().get(0); assertEquals(4, hit.getAnnotations().get(HitAnnotation.HIT_PROCESSES.name())); assertEquals(7, hit.getAnnotations().get(HitAnnotation.HIT_PARTICIPANTS.name())); assertTrue(hit instanceof Pathway); assertEquals(6, hit.getAnnotations().keySet().size()); response = searchEngine.search("*", 0, Pathway.class, null, null); assertNotNull(response); assertEquals(1, response.getHits().size()); response = searchEngine.search("*", 0, null, null, null); assertEquals(50, response.getHits().size()); response = searchEngine.search("*", 0, PhysicalEntity.class, null, null); assertEquals(8, response.getHits().size()); response = searchEngine.search("*", 0, PhysicalEntity.class, null, new String[] {"562"}); assertEquals(2, response.getHits().size()); response = searchEngine.search("*", 0, PhysicalEntity.class, null, new String[] {"Escherichia"}); assertEquals(2, response.getHits().size()); response = searchEngine.search("*", 0, PhysicalEntity.class, null, new String[] {"Escherichia coliü"}); assertEquals(2, response.getHits().size()); response = searchEngine.search("*", 0, Provenance.class, null, null); assertEquals(2, response.getHits().size()); response = searchEngine.search("*", 0, Provenance.class, new String[] {"kegg"}, null); assertEquals(1, response.getHits().size()); response = searchEngine.search("*", 0, Provenance.class, new String[] {"Kyoto Encyclopedia of Genes and Genomes"}, null); assertEquals(1, response.getHits().size()); response = searchEngine.search("*", 0, Pathway.class, new String[] {"http: assertEquals(1, response.getHits().size()); response = searchEngine.search("glycolysis", 0, SmallMoleculeReference.class, null, null); assertTrue(response.getHits().isEmpty()); response = searchEngine.search("pathway:glycolysis", 0, SmallMoleculeReference.class, null, null); assertEquals(5, response.getHits().size()); searchEngine.setMaxHitsPerPage(10); response = searchEngine.search("*", 0, null, null, null); assertEquals(50, response.getTotalHits()); assertEquals(10, response.getHits().size()); response = searchEngine.search("*", 1, null, null, null); assertEquals(10, response.getHits().size()); }