@Test @Ignore public void testSearch() throws Exception { LuceneSearcher searcher = new LuceneSearcher(new AppContext()); searcher.search("\"hindi sahit\"~5", 20); searcher.close(); }
public List<Publication> search(String queryString, int hitsPerPage) throws ParseException, IOException { IndexSearcher searcher = searcherManager.acquire(); try { List<Publication> publications = new ArrayList<>(hitsPerPage); long start = System.currentTimeMillis(); Query query; try { queryString = queryString.trim().toLowerCase(); if (queryString.isEmpty() || queryString.equals("*") || queryString.equals("**")) { queryString = "*"; } query = parser.parse(queryString); } catch (ParseException e) { logger.warn("Problem with query syntax :" + e.getMessage()); query = parser.parse(QueryParserUtil.escape(queryString)); } TopDocs results = searcher.search(query, hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; long end = System.currentTimeMillis(); System.out.println(query.toString(field) + ", results =" + numTotalHits + ", Total matching docs= " + hits.length + ",Total Docs = " + searcher.getIndexReader().numDocs() + ", Time: " + (end - start) + "ms"); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"), new QueryScorer(query)); highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); highlighter.setTextFragmenter(new SimpleFragmenter(200)); int maxNumFragmentsRequired = 2; String fragmentSeparator = "..."; for (int i = 0; i < hits.length; i++) { final Publication publication = getPublication(searcher.doc(hits[i].doc)); String result = highlighter.getBestFragments(analyzer.tokenStream("title1", new StringReader(publication.toString())), publication.toString(), maxNumFragmentsRequired, fragmentSeparator); if (result != null && !result.trim().isEmpty()) publication.setSearchText(result); else publication.setSearchText(publication.toString()); publications.add(publication); } return publications; } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } finally { searcherManager.release(searcher); } return Collections.emptyList(); }
LuceneSearcher { public List<Publication> search(String queryString, int hitsPerPage) throws ParseException, IOException { IndexSearcher searcher = searcherManager.acquire(); try { List<Publication> publications = new ArrayList<>(hitsPerPage); long start = System.currentTimeMillis(); Query query; try { queryString = queryString.trim().toLowerCase(); if (queryString.isEmpty() || queryString.equals("*") || queryString.equals("**")) { queryString = "*"; } query = parser.parse(queryString); } catch (ParseException e) { logger.warn("Problem with query syntax :" + e.getMessage()); query = parser.parse(QueryParserUtil.escape(queryString)); } TopDocs results = searcher.search(query, hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; long end = System.currentTimeMillis(); System.out.println(query.toString(field) + ", results =" + numTotalHits + ", Total matching docs= " + hits.length + ",Total Docs = " + searcher.getIndexReader().numDocs() + ", Time: " + (end - start) + "ms"); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"), new QueryScorer(query)); highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); highlighter.setTextFragmenter(new SimpleFragmenter(200)); int maxNumFragmentsRequired = 2; String fragmentSeparator = "..."; for (int i = 0; i < hits.length; i++) { final Publication publication = getPublication(searcher.doc(hits[i].doc)); String result = highlighter.getBestFragments(analyzer.tokenStream("title1", new StringReader(publication.toString())), publication.toString(), maxNumFragmentsRequired, fragmentSeparator); if (result != null && !result.trim().isEmpty()) publication.setSearchText(result); else publication.setSearchText(publication.toString()); publications.add(publication); } return publications; } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } finally { searcherManager.release(searcher); } return Collections.emptyList(); } }
LuceneSearcher { public List<Publication> search(String queryString, int hitsPerPage) throws ParseException, IOException { IndexSearcher searcher = searcherManager.acquire(); try { List<Publication> publications = new ArrayList<>(hitsPerPage); long start = System.currentTimeMillis(); Query query; try { queryString = queryString.trim().toLowerCase(); if (queryString.isEmpty() || queryString.equals("*") || queryString.equals("**")) { queryString = "*"; } query = parser.parse(queryString); } catch (ParseException e) { logger.warn("Problem with query syntax :" + e.getMessage()); query = parser.parse(QueryParserUtil.escape(queryString)); } TopDocs results = searcher.search(query, hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; long end = System.currentTimeMillis(); System.out.println(query.toString(field) + ", results =" + numTotalHits + ", Total matching docs= " + hits.length + ",Total Docs = " + searcher.getIndexReader().numDocs() + ", Time: " + (end - start) + "ms"); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"), new QueryScorer(query)); highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); highlighter.setTextFragmenter(new SimpleFragmenter(200)); int maxNumFragmentsRequired = 2; String fragmentSeparator = "..."; for (int i = 0; i < hits.length; i++) { final Publication publication = getPublication(searcher.doc(hits[i].doc)); String result = highlighter.getBestFragments(analyzer.tokenStream("title1", new StringReader(publication.toString())), publication.toString(), maxNumFragmentsRequired, fragmentSeparator); if (result != null && !result.trim().isEmpty()) publication.setSearchText(result); else publication.setSearchText(publication.toString()); publications.add(publication); } return publications; } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } finally { searcherManager.release(searcher); } return Collections.emptyList(); } LuceneSearcher(IndexWriter writer);  LuceneSearcher(AppContext appContext); }
LuceneSearcher { public List<Publication> search(String queryString, int hitsPerPage) throws ParseException, IOException { IndexSearcher searcher = searcherManager.acquire(); try { List<Publication> publications = new ArrayList<>(hitsPerPage); long start = System.currentTimeMillis(); Query query; try { queryString = queryString.trim().toLowerCase(); if (queryString.isEmpty() || queryString.equals("*") || queryString.equals("**")) { queryString = "*"; } query = parser.parse(queryString); } catch (ParseException e) { logger.warn("Problem with query syntax :" + e.getMessage()); query = parser.parse(QueryParserUtil.escape(queryString)); } TopDocs results = searcher.search(query, hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; long end = System.currentTimeMillis(); System.out.println(query.toString(field) + ", results =" + numTotalHits + ", Total matching docs= " + hits.length + ",Total Docs = " + searcher.getIndexReader().numDocs() + ", Time: " + (end - start) + "ms"); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"), new QueryScorer(query)); highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); highlighter.setTextFragmenter(new SimpleFragmenter(200)); int maxNumFragmentsRequired = 2; String fragmentSeparator = "..."; for (int i = 0; i < hits.length; i++) { final Publication publication = getPublication(searcher.doc(hits[i].doc)); String result = highlighter.getBestFragments(analyzer.tokenStream("title1", new StringReader(publication.toString())), publication.toString(), maxNumFragmentsRequired, fragmentSeparator); if (result != null && !result.trim().isEmpty()) publication.setSearchText(result); else publication.setSearchText(publication.toString()); publications.add(publication); } return publications; } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } finally { searcherManager.release(searcher); } return Collections.emptyList(); } LuceneSearcher(IndexWriter writer);  LuceneSearcher(AppContext appContext); void refresh(); List<Publication> search(String queryString, int hitsPerPage); List<Publication> searchExisting(int maxResults); Map<String, String> search(String barcode); List<Map<String, String>> search2(String line, int startPage, int hitsPerPage); void close(); Publication searchBook(String barcode); int countDocs(); }
LuceneSearcher { public List<Publication> search(String queryString, int hitsPerPage) throws ParseException, IOException { IndexSearcher searcher = searcherManager.acquire(); try { List<Publication> publications = new ArrayList<>(hitsPerPage); long start = System.currentTimeMillis(); Query query; try { queryString = queryString.trim().toLowerCase(); if (queryString.isEmpty() || queryString.equals("*") || queryString.equals("**")) { queryString = "*"; } query = parser.parse(queryString); } catch (ParseException e) { logger.warn("Problem with query syntax :" + e.getMessage()); query = parser.parse(QueryParserUtil.escape(queryString)); } TopDocs results = searcher.search(query, hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; long end = System.currentTimeMillis(); System.out.println(query.toString(field) + ", results =" + numTotalHits + ", Total matching docs= " + hits.length + ",Total Docs = " + searcher.getIndexReader().numDocs() + ", Time: " + (end - start) + "ms"); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"), new QueryScorer(query)); highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); highlighter.setTextFragmenter(new SimpleFragmenter(200)); int maxNumFragmentsRequired = 2; String fragmentSeparator = "..."; for (int i = 0; i < hits.length; i++) { final Publication publication = getPublication(searcher.doc(hits[i].doc)); String result = highlighter.getBestFragments(analyzer.tokenStream("title1", new StringReader(publication.toString())), publication.toString(), maxNumFragmentsRequired, fragmentSeparator); if (result != null && !result.trim().isEmpty()) publication.setSearchText(result); else publication.setSearchText(publication.toString()); publications.add(publication); } return publications; } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } finally { searcherManager.release(searcher); } return Collections.emptyList(); } LuceneSearcher(IndexWriter writer);  LuceneSearcher(AppContext appContext); void refresh(); List<Publication> search(String queryString, int hitsPerPage); List<Publication> searchExisting(int maxResults); Map<String, String> search(String barcode); List<Map<String, String>> search2(String line, int startPage, int hitsPerPage); void close(); Publication searchBook(String barcode); int countDocs();  }