List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context)
From source file:com.gitblit.LuceneExecutor.java
License:Apache License
/** * Searches the specified repositories for the given text or query * //from w w w . ja v a 2 s . c om * @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
/** * Gets an index searcher for the repository. * * @param repository/* w ww .java 2 s . co m*/ * @return * @throws IOException */ private IndexSearcher getIndexSearcher(String repository) throws IOException { IndexSearcher searcher = searchers.get(repository); if (searcher == null) { IndexWriter writer = getIndexWriter(repository); searcher = new IndexSearcher(DirectoryReader.open(writer, true)); searchers.put(repository, searcher); } return searcher; }
From source file:com.gitblit.service.LuceneService.java
License:Apache License
/** * Searches the specified repositories for the given text or query * * @param text/*from w ww .j a va2 s .com*/ * 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
private IndexSearcher getSearcher() throws IOException { if (searcher == null) { searcher = new IndexSearcher(DirectoryReader.open(getWriter(), true)); }//from w w w.j a v a 2 s . com return searcher; }
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 w w .j a va2 s . co 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; }
From source file:com.github.cmisbox.persistence.Storage.java
License:Open Source License
private IndexSearcher getSearcher() throws Exception { this.reader = this.reader.reopen(); return new IndexSearcher(this.reader); }
From source file:com.github.jiloc.USTweetsAnalyzer.Analyzer_Index.java
public Analyzer_Index(Directory dir) throws IOException { ir = DirectoryReader.open(dir);//from ww w . ja va2s .c o m searcher = new IndexSearcher(ir); // universe = new HashMap<String, HashSet<String>>(); }
From source file:com.github.le11.nls.lucene.UIMABaseAnalyzerTest.java
License:Apache License
@Test public void baseUIMAAnalyzerIntegrationTest() { try {/*from w w w . j a v a2 s .c o m*/ // add the first doc Document doc = new Document(); doc.add(new Field("title", "this is a dummy title ", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("contents", "there is some content written here", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc, analyzer); writer.commit(); // try the search over the first doc IndexSearcher indexSearcher = new IndexSearcher(writer.getReader()); TopDocs result = indexSearcher.search(new MatchAllDocsQuery("contents"), 10); assertTrue(result.totalHits > 0); Document d = indexSearcher.doc(result.scoreDocs[0].doc); assertNotNull(d); assertNotNull(d.getFieldable("title")); assertNotNull(d.getFieldable("contents")); // add a second doc doc = new Document(); doc.add(new Field("title", "il mio titolo", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("contents", "che cosa e' scritto qui", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc, analyzer); writer.commit(); // do a matchalldocs query to retrieve both docs indexSearcher = new IndexSearcher(writer.getReader()); result = indexSearcher.search(new MatchAllDocsQuery("contents"), 10); assertTrue(result.totalHits > 0); for (ScoreDoc di : result.scoreDocs) { d = indexSearcher.doc(di.doc); assertNotNull(d); assertNotNull(d.getFieldable("title")); assertNotNull(d.getFieldable("contents")); } } catch (Exception e) { fail(e.getLocalizedMessage()); } }
From source file:com.github.le11.nls.lucene.UIMATypeBasedSimilarityTest.java
License:Apache License
@Before public void setUp() throws Exception { dir = new RAMDirectory(); analyzer = new UIMAPayloadsAnalyzer("/HmmTaggerAggregate.xml"); writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_33, analyzer)); Document doc = new Document(); doc.add(new Field("title", "this is a dummy title containing an entity for London", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("contents", "there is some content written here about the british city", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc, analyzer);//w ww .ja v a 2 s . c om writer.commit(); // try the search over the first doc IndexSearcher indexSearcher = new IndexSearcher(writer.getReader()); TopDocs result = indexSearcher.search(new MatchAllDocsQuery("contents"), 10); assertTrue(result.totalHits > 0); Document d = indexSearcher.doc(result.scoreDocs[0].doc); assertNotNull(d); assertNotNull(d.getFieldable("title")); assertNotNull(d.getFieldable("contents")); // add a second doc doc = new Document(); doc.add(new Field("title", "some title regarding some article written in English", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("contents", "this is the content of the article about", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc, analyzer); writer.commit(); }