List of usage examples for org.apache.lucene.search IndexSearcher search
public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager) throws IOException
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public ArrayList getIndexNames() { IndexSearcher mainIndexSearcher = null; IndexReader ir = null;//from ww w . j av a 2 s . c o m try { ir = IndexReader.open(FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX), new SimpleFSLockFactory(indexDir + File.separator + MAIN_INDEX))); mainIndexSearcher = new IndexSearcher(ir); ArrayList<String[]> indexNames = new ArrayList<String[]>(); mainIndexSearcher = new IndexSearcher(ir); Query q = new WildcardQuery(new Term("indexName", "*")); TopScoreDocCollector collector = TopScoreDocCollector.create(10000, false); mainIndexSearcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; for (ScoreDoc hit : hits) { Document doc = mainIndexSearcher.doc(hit.doc); String indexName = doc.get("indexName"); String indexDisplayName = doc.get("displayName"); indexNames.add(new String[] { indexName, indexDisplayName }); } return indexNames; } catch (Exception ex) { ex.printStackTrace(); return null; } finally { try { ir.close(); mainIndexSearcher.close(); ir = null; mainIndexSearcher = null; } catch (IOException e) { logger.info("Error: Unable to close index."); System.exit(0); e.printStackTrace(); } } }
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public ArrayList<CardSnapshot> getBookmarks() { ArrayList<CardSnapshot> cardSnaps = new ArrayList(); try {//from w w w . ja v a 2s .com IndexReader ir = IndexReader.open(FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX), new SimpleFSLockFactory(indexDir + File.separator + MAIN_INDEX))); IndexSearcher mainIndexSearcher = new IndexSearcher(ir); Query q = new WildcardQuery(new Term("qcId", "*")); TopScoreDocCollector collector = TopScoreDocCollector.create(10000, false); mainIndexSearcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; for (ScoreDoc hit : hits) { Document doc = mainIndexSearcher.doc(hit.doc); IndexReader reader = IndexReader .open(FSDirectory.open(new File(indexDir + File.separator + doc.get("qcIndexName")), new SimpleFSLockFactory(indexDir + File.separator + doc.get("qcIndexName")))); IndexSearcher searcher = new IndexSearcher(reader); q = new TermQuery(new Term("id", doc.get("qcId"))); collector = TopScoreDocCollector.create(10000, false); searcher.search(q, collector); ScoreDoc[] hits2 = collector.topDocs().scoreDocs; doc = searcher.doc(hits2[0].doc); cardSnaps.add(new CardSnapshot("", doc)); reader.close(); searcher.close(); reader = null; searcher = null; } ir.close(); mainIndexSearcher.close(); ir = null; mainIndexSearcher = null; } catch (Exception ex) { ex.printStackTrace(); } return cardSnaps; }
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public ArrayList<CardSnapshot> search(String searchText, ArrayList<String> indexNames, boolean searchInTitle, boolean searchInTags, boolean searchInText, boolean superFuzzy) { ArrayList<CardSnapshot> cardSnaps = new ArrayList(); try {/*w w w . ja v a 2 s . c o m*/ ArrayList<IndexSearcher> searchers = new ArrayList<IndexSearcher>(); for (String indexName : indexNames) { IndexReader reader = IndexReader .open(FSDirectory.open(new File(indexDir + File.separator + indexName), new SimpleFSLockFactory(indexDir + File.separator + indexName))); IndexSearcher searcher = new IndexSearcher(reader); searchers.add(searcher); } BooleanQuery query = new BooleanQuery(); if (searchInTitle) { IndexerUtil.getTokenizedQuery(query, "title", searchText, superFuzzy); } if (searchInTags) { IndexerUtil.getTokenizedQuery(query, "tags", searchText, superFuzzy); } if (searchInText) { IndexerUtil.getTokenizedQuery(query, "text", searchText, superFuzzy); IndexerUtil.getTokenizedQuery(query, "analyzedText", searchText, superFuzzy); } for (IndexSearcher searcher : searchers) { TopScoreDocCollector collector = TopScoreDocCollector.create(10000, false); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; for (ScoreDoc hit : hits) { Document doc = searcher.doc(hit.doc); TokenStream stream = TokenSources.getTokenStream("text", doc.get("analyzedText"), new StandardAnalyzer(Version.LUCENE_20.LUCENE_35)); QueryScorer scorer = new QueryScorer(query, "analyzedText"); Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 20); Highlighter highlighter = new Highlighter(scorer); highlighter.setTextFragmenter(fragmenter); String[] fragments = highlighter.getBestFragments(stream, doc.get("text"), 5); String highlights = ""; for (String fragment : fragments) { highlights += fragment + "..."; } if (highlights.equals("")) { String text = doc.get("text"); if (text.length() > 100) { highlights += doc.get("text").substring(0, 100); } else { highlights += doc.get("text"); } } cardSnaps.add(new CardSnapshot(highlights, doc)); } searcher.getIndexReader().close(); searcher.close(); searcher = null; } } catch (Exception ex) { ex.printStackTrace(); } return cardSnaps; }
From source file:com.gemstone.gemfire.cache.lucene.internal.directory.DumpDirectoryFilesIntegrationTest.java
License:Apache License
@Test public void shouldDumpReadableLuceneIndexFile() throws Exception { luceneService.createIndex(INDEX_NAME, REGION_NAME, "title", "description"); Region region = createRegion(REGION_NAME, RegionShortcut.PARTITION); region.put(0, new TestObject("title 1", "hello world")); region.put(1 * 113, new TestObject("title 2", "this will not match")); region.put(2 * 113, new TestObject("title 3", "hello world")); region.put(3 * 113, new TestObject("hello world", "hello world")); InternalLuceneIndex index = (InternalLuceneIndex) luceneService.getIndex(INDEX_NAME, REGION_NAME); index.waitUntilFlushed(60000);/* w ww . j a v a2 s. c o m*/ index.dumpFiles(diskDirRule.get().getAbsolutePath()); //Find the directory for the first bucket File bucket0 = diskDirRule.get().listFiles(file -> file.getName().endsWith("_0"))[0]; //Test that we can read the lucene index from the dump final FSDirectory directory = FSDirectory.open(bucket0.toPath()); IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); final TopDocs results = searcher.search(new MatchAllDocsQuery(), 1000); assertEquals(4, results.totalHits); }
From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImpl.java
License:Apache License
@Override public void query(Query query, int limit, IndexResultCollector collector) throws IOException { IndexSearcher searcher = searcherManager.acquire(); try {//from w ww . ja v a2 s . c o m TopDocs docs = searcher.search(query, limit); for (ScoreDoc scoreDoc : docs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); Object key = SerializerUtil.getKey(doc); collector.collect(key, scoreDoc.score); } } finally { searcherManager.release(searcher); } }
From source file:com.gitblit.LuceneExecutor.java
License:Apache License
/** * Searches the specified repositories for the given text or query * // w ww .java2s . c o m * @param text * if the text is null or empty, null is returned * @param page * the page number to retrieve. page is 1-indexed. * @param pageSize * the number of elements to return for this page * @param repositories * a list of repositories to search. if no repositories are * specified null is returned. * @return a list of SearchResults in order from highest to the lowest score * */ public List<SearchResult> search(String text, int page, int pageSize, String... repositories) { if (StringUtils.isEmpty(text)) { return null; } if (ArrayUtils.isEmpty(repositories)) { return null; } Set<SearchResult> results = new LinkedHashSet<SearchResult>(); StandardAnalyzer analyzer = new StandardAnalyzer(LUCENE_VERSION); try { // default search checks summary and content BooleanQuery query = new BooleanQuery(); QueryParser qp; qp = new QueryParser(LUCENE_VERSION, FIELD_SUMMARY, analyzer); qp.setAllowLeadingWildcard(true); query.add(qp.parse(text), Occur.SHOULD); qp = new QueryParser(LUCENE_VERSION, FIELD_CONTENT, analyzer); qp.setAllowLeadingWildcard(true); query.add(qp.parse(text), Occur.SHOULD); IndexSearcher searcher; if (repositories.length == 1) { // single repository search searcher = getIndexSearcher(repositories[0]); } else { // multiple repository search List<IndexReader> readers = new ArrayList<IndexReader>(); for (String repository : repositories) { IndexSearcher repositoryIndex = getIndexSearcher(repository); readers.add(repositoryIndex.getIndexReader()); } IndexReader[] rdrs = readers.toArray(new IndexReader[readers.size()]); MultiSourceReader reader = new MultiSourceReader(rdrs); searcher = new IndexSearcher(reader); } Query rewrittenQuery = searcher.rewrite(query); logger.debug(rewrittenQuery.toString()); TopScoreDocCollector collector = TopScoreDocCollector.create(5000, true); searcher.search(rewrittenQuery, collector); int offset = Math.max(0, (page - 1) * pageSize); ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs; int totalHits = collector.getTotalHits(); for (int i = 0; i < hits.length; i++) { int docId = hits[i].doc; Document doc = searcher.doc(docId); SearchResult result = createSearchResult(doc, hits[i].score, offset + i + 1, totalHits); if (repositories.length == 1) { // single repository search result.repository = repositories[0]; } else { // multi-repository search MultiSourceReader reader = (MultiSourceReader) searcher.getIndexReader(); int index = reader.getSourceIndex(docId); result.repository = repositories[index]; } String content = doc.get(FIELD_CONTENT); result.fragment = getHighlightedFragment(analyzer, query, content, result); results.add(result); } } catch (Exception e) { logger.error(MessageFormat.format("Exception while searching for {0}", text), e); } return new ArrayList<SearchResult>(results); }
From source file:com.gitblit.service.LuceneService.java
License:Apache License
/** * Searches the specified repositories for the given text or query * * @param text/* w w w . java 2 s . c o m*/ * if the text is null or empty, null is returned * @param page * the page number to retrieve. page is 1-indexed. * @param pageSize * the number of elements to return for this page * @param repositories * a list of repositories to search. if no repositories are * specified null is returned. * @return a list of SearchResults in order from highest to the lowest score * */ public List<SearchResult> search(String text, int page, int pageSize, String... repositories) { if (StringUtils.isEmpty(text)) { return null; } if (ArrayUtils.isEmpty(repositories)) { return null; } Set<SearchResult> results = new LinkedHashSet<SearchResult>(); StandardAnalyzer analyzer = new StandardAnalyzer(); try { // default search checks summary and content BooleanQuery.Builder bldr = new BooleanQuery.Builder(); QueryParser qp; qp = new QueryParser(FIELD_SUMMARY, analyzer); qp.setAllowLeadingWildcard(true); bldr.add(qp.parse(text), Occur.SHOULD); qp = new QueryParser(FIELD_CONTENT, analyzer); qp.setAllowLeadingWildcard(true); bldr.add(qp.parse(text), Occur.SHOULD); IndexSearcher searcher; if (repositories.length == 1) { // single repository search searcher = getIndexSearcher(repositories[0]); } else { // multiple repository search List<IndexReader> readers = new ArrayList<IndexReader>(); for (String repository : repositories) { IndexSearcher repositoryIndex = getIndexSearcher(repository); readers.add(repositoryIndex.getIndexReader()); } IndexReader[] rdrs = readers.toArray(new IndexReader[readers.size()]); MultiSourceReader reader = new MultiSourceReader(rdrs); searcher = new IndexSearcher(reader); } BooleanQuery query = bldr.build(); Query rewrittenQuery = searcher.rewrite(query); logger.debug(rewrittenQuery.toString()); TopScoreDocCollector collector = TopScoreDocCollector.create(5000); searcher.search(rewrittenQuery, collector); int offset = Math.max(0, (page - 1) * pageSize); ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs; int totalHits = collector.getTotalHits(); for (int i = 0; i < hits.length; i++) { int docId = hits[i].doc; Document doc = searcher.doc(docId); SearchResult result = createSearchResult(doc, hits[i].score, offset + i + 1, totalHits); if (repositories.length == 1) { // single repository search result.repository = repositories[0]; } else { // multi-repository search MultiSourceReader reader = (MultiSourceReader) searcher.getIndexReader(); int index = reader.getSourceIndex(docId); result.repository = repositories[index]; } String content = doc.get(FIELD_CONTENT); result.fragment = getHighlightedFragment(analyzer, query, content, result); results.add(result); } } catch (Exception e) { logger.error(MessageFormat.format("Exception while searching for {0}", text), e); } return new ArrayList<SearchResult>(results); }
From source file:com.gitblit.tickets.TicketIndexer.java
License:Apache License
/** * Search for tickets matching the query. The returned tickets are * shadows of the real ticket, but suitable for a results list. * * @param repository/*ww w.j av a2 s .com*/ * @param text * @param page * @param pageSize * @return search results */ public List<QueryResult> searchFor(RepositoryModel repository, String text, int page, int pageSize) { if (StringUtils.isEmpty(text)) { return Collections.emptyList(); } Set<QueryResult> results = new LinkedHashSet<QueryResult>(); StandardAnalyzer analyzer = new StandardAnalyzer(); try { // search the title, description and content BooleanQuery.Builder bldr = new BooleanQuery.Builder(); QueryParser qp; qp = new QueryParser(Lucene.title.name(), analyzer); qp.setAllowLeadingWildcard(true); bldr.add(qp.parse(text), Occur.SHOULD); qp = new QueryParser(Lucene.body.name(), analyzer); qp.setAllowLeadingWildcard(true); bldr.add(qp.parse(text), Occur.SHOULD); qp = new QueryParser(Lucene.content.name(), analyzer); qp.setAllowLeadingWildcard(true); bldr.add(qp.parse(text), Occur.SHOULD); IndexSearcher searcher = getSearcher(); Query rewrittenQuery = searcher.rewrite(bldr.build()); log.debug(rewrittenQuery.toString()); TopScoreDocCollector collector = TopScoreDocCollector.create(5000); searcher.search(rewrittenQuery, collector); int offset = Math.max(0, (page - 1) * pageSize); ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs; for (int i = 0; i < hits.length; i++) { int docId = hits[i].doc; Document doc = searcher.doc(docId); QueryResult result = docToQueryResult(doc); if (repository != null) { if (!result.repository.equalsIgnoreCase(repository.name)) { continue; } } results.add(result); } } catch (Exception e) { log.error(MessageFormat.format("Exception while searching for {0}", text), e); } return new ArrayList<QueryResult>(results); }
From source file:com.github.alvanson.xltsearch.SearchTask.java
License:Apache License
@Override protected List<SearchResult> call() { DirectoryReader ireader = null;/*w ww . j av a 2 s .c o m*/ List<SearchResult> results = null; updateMessage("Searching..."); try { ireader = DirectoryReader.open(config.getDirectory()); IndexSearcher isearcher = new IndexSearcher(ireader); isearcher.setSimilarity(config.getSimilarity()); QueryParser parser = new QueryParser(config.getVersion(), config.contentField, config.getAnalyzer()); Query query = parser.parse(qstr); logger.debug("Query: {}", query); ScoreDoc[] hits = isearcher.search(query, limit).scoreDocs; // collect results results = new ArrayList<>(hits.length); for (ScoreDoc hit : hits) { Document document = isearcher.doc(hit.doc); File file = new File(root.getPath() + File.separator + document.get(config.pathField)); String title = document.get(config.titleField); if (title == null) { title = ""; } // report metadata in `details` StringBuilder sb = new StringBuilder(); for (IndexableField field : document.getFields()) { if (field.stringValue() != null) { sb.append(field.name() + ": " + field.stringValue() + '\n'); } } results.add(new SearchResult(file, title, hit.score, sb.toString())); } updateMessage(hits.length + " results"); } catch (IOException ex) { updateMessage("I/O exception"); logger.error("I/O exception while reading index", ex); } catch (ParseException ex) { updateMessage("Parse error"); logger.warn("Parse exception while parsing '{}'", qstr, ex); } // close ireader if (ireader != null) { try { ireader.close(); } catch (IOException ex) { logger.warn("I/O exception while closing index reader", ex); } } return results; }
From source file:com.github.alvanson.xltsearch.SelectTask.java
License:Apache License
private Map<String, String> getHashSums() { Map<String, String> hashSums = new HashMap<>(); DirectoryReader ireader = null;/*from w ww . j av a2s . c o m*/ try { if (DirectoryReader.indexExists(config.getDirectory())) { // read hashsums from `directory` ireader = DirectoryReader.open(config.getDirectory()); IndexSearcher isearcher = new IndexSearcher(ireader); Query query = new MatchAllDocsQuery(); ScoreDoc[] hits = isearcher.search(query, ireader.numDocs() + 1).scoreDocs; // collect results for (ScoreDoc hit : hits) { Document document = isearcher.doc(hit.doc); String relPath = document.get(config.pathField); String hashSum = document.get(config.hashSumField); if (relPath != null && hashSum != null) { hashSums.put(relPath, hashSum); } } } // else: return empty map } catch (IOException ex) { logger.error("I/O exception while reading index", ex); } if (ireader != null) { try { ireader.close(); } catch (IOException ex) { logger.warn("I/O exception while closing index reader", ex); } } return hashSums; }