List of usage examples for org.apache.lucene.search IndexSearcher doc
public Document doc(int docID) throws IOException
.getIndexReader().document(docID)
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for a matching eclass * //from w ww . ja va2 s . c o m * @param eclass * - the EClass to match when searching * @return a list of search results * @throws IllegalArgumentException * if the eclass is null or is not annotated for indexing * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { throw new IllegalArgumentException("EClass is not annotated for indexing"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); ScoreDoc[] hits = searcher.search(classQuery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Deletes a document matching the EObject * /*from w ww .java2s .com*/ * @param obj * @throws IllegalArgumentException * if EObject is null or the EObject is not contained in a * resource * @throws IOException * if there are issues saving the index */ public void deleteDocument(EObject obj) throws IllegalArgumentException, IOException { if (obj == null) { throw new IllegalArgumentException("EObject cannot be null"); } if (obj.eResource() == null) { throw new IllegalArgumentException("EObject must be contained in a Resource"); } Query query = findDocument(obj); if (query != null) { logger.debug("Deleting existing index for {}:{}", obj.eResource().getURI(), obj.eResource().getURIFragment(obj)); writer.deleteDocuments(query); if (!holdCommits) { writer.commit(); } } DirectoryReader reader = DirectoryReader.open(fsDir); ArrayList<String> names = new ArrayList<String>(); for (AtomicReaderContext context : reader.leaves()) { for (FieldInfo fi : context.reader().getFieldInfos()) { if (!names.contains(fi.name)) { names.add(fi.name); } } } if (names.size() > 0) { MultiFieldQueryParser parser = new MultiFieldQueryParser(version, names.toArray(new String[] {}), analyzer); try { query = parser.parse(obj.eResource().getURIFragment(obj)); IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs; for (ScoreDoc hit : hits) { Document hitDoc = searcher.doc(hit.doc); logger.debug("Hanging reference in: {}", hitDoc.getField(EMFIndexUtil.DOCUMENT_URI_KEY).stringValue()); } } catch (ParseException e) { logger.error(e.getMessage()); } } }
From source file:com.common.search.IKAnalyzerDemo.java
License:Apache License
public static void main(String[] args) { // Lucene Document String fieldName = "text"; // /* w w w .j av a 2 s . c o m*/ String text = "IK Analyzer "; // IKAnalyzer Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); // IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED)); iwriter.addDocument(doc); iwriter.close(); // ********************************** // ireader = IndexReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = ""; // QueryParserQuery QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); // 5 TopDocs topDocs = isearcher.search(query, 5); System.out.println(" " + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println(" " + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.ctl.utils.lucene.SearchFiles.java
License:Apache License
/** * This demonstrates a typical paging search scenario, where the search engine presents * pages of size n to the user. The user can then go to the next page if interested in * the next hits.// ww w. java 2 s . c o m * * When the query is executed for the first time, then only enough results are collected * to fill 5 result pages. If the user wants to page beyond this limit, then the query * is executed another time and all hits are collected. * */ public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { // Collect enough docs to show 5 pages TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = Math.toIntExact(results.totalHits); System.out.println(numTotalHits + " total matching documents"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'n') { break; } hits = searcher.search(query, numTotalHits).scoreDocs; } end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { if (raw) { // output raw format System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score); continue; } Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { System.out.println(" Title: " + doc.get("title")); } } else { System.out.println((i + 1) + ". " + "No path for this document"); } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); } System.out.println("(q)uit or enter number to jump to a page."); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'q') { quit = true; break; } if (line.charAt(0) == 'p') { start = Math.max(0, start - hitsPerPage); break; } else if (line.charAt(0) == 'n') { if (start + hitsPerPage < numTotalHits) { start += hitsPerPage; } break; } else { int page = Integer.parseInt(line); if ((page - 1) * hitsPerPage < numTotalHits) { start = (page - 1) * hitsPerPage; break; } else { System.out.println("No such page"); } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }
From source file:com.czw.search.lucene.example.facet.SimpleFacetsExample.java
License:Apache License
/** * User drills down on 'Publish Date/2012', and we * return facets for both 'Publish Date' and 'Author', * using DrillSideways.//from ww w.ja va 2 s. c om */ private List<FacetResult> drillSideways() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Passing no baseQuery means we drill down on all // documents ("browse only"): DrillDownQuery q = new DrillDownQuery(config); // Now user drills down on Publish Date/2010: q.add("Publish Date", "2010"); DrillSideways ds = new DrillSideways(searcher, config, taxoReader); DrillSidewaysResult result = ds.search(q, 10); // Retrieve results List<FacetResult> facets = result.facets.getAllDims(10); System.out.println("totalHits:" + result.hits.totalHits); ScoreDoc[] docs = result.hits.scoreDocs; System.out.println(searcher.doc(docs[0].doc)); indexReader.close(); taxoReader.close(); return facets; }
From source file:com.devb.search.IndicSearcher.java
License:Apache License
private void callSearch(boolean j) { System.out.println("Servlet Ctx " + servletContext.getRealPath("/")); String indexPath = servletContext.getRealPath("/") + "/hindex/"; String docsPath = servletContext.getRealPath("/") + "/hdocs/"; final File docDir = new File(docsPath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, " + "please check the path\n"); return;/*from w ww. j a va2 s . c om*/ } IndexReader reader = null; IndexSearcher searcher = null; Analyzer analyzer = null; String field = "contents"; try { reader = DirectoryReader.open(FSDirectory.open(new File(indexPath))); searcher = new IndexSearcher(reader); analyzer = new HindiAnalyzer(); } catch (IOException ioe) { ioe.printStackTrace(); } QueryParser parser = new QueryParser(field, analyzer); String /*ByteBuffer*/ line = null; Query query = null; try { // line = Charset.forName("UTF-8").encode(this.id); line = this.id; // line = line.trim(); if (line == null) { return; } System.out.println("Hindi StandardSearcher / callSearch Line " + line); query = parser.parse(line); System.out.println("Hindi StandardSearcher / callSearch Hindi Query " + query); final int maxHits = 10; ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs; try { // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); if (j) { JSONObject jo = new JSONObject(); jo.put("query", query.toString(field)); jo.put("path", hitDoc.get("path")); jo.put("line", hitDoc.get("linenumber")); jo.put("contents", hitDoc.get("contents")); ja.put(jo); } else { SearchResult ns = new SearchResult(); ns.setQuery(query.toString(field)); ns.setDocPath(hitDoc.get("path")); ns.setLineNum(hitDoc.get("linenumber")); ns.setContents(hitDoc.get("contents")); contentProvider.put(String.valueOf(i), ns); } } } catch (Exception ito) { ito.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } try { reader.close(); } catch (IOException ioe) { } }
From source file:com.devb.search.StandardSearcher.java
License:Apache License
private void callSearch(boolean j) { System.out.println("Servlet Ctx " + servletContext.getRealPath("/")); String indexPath = servletContext.getRealPath("/") + "/index/"; String docsPath = servletContext.getRealPath("/") + "/docs/"; final File docDir = new File(docsPath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, " + "please check the path\n"); return;//from www. ja v a2s . c o m } IndexReader reader = null; IndexSearcher searcher = null; Analyzer analyzer = null; String field = "contents"; try { reader = DirectoryReader.open(FSDirectory.open(new File(indexPath))); searcher = new IndexSearcher(reader); analyzer = new StandardAnalyzer(); } catch (IOException ioe) { ioe.printStackTrace(); } QueryParser parser = new QueryParser(field, analyzer); String line = null; Query query = null; try { line = this.id; line = line.trim(); if (line.length() == 0) { return; } query = parser.parse(line); final int maxHits = 10; ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs; try { // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); if (j) { JSONObject jo = new JSONObject(); jo.put("query", query.toString(field)); jo.put("path", hitDoc.get("path")); jo.put("line", hitDoc.get("linenumber")); jo.put("contents", hitDoc.get("contents")); ja.put(jo); } else { SearchResult ns = new SearchResult(); ns.setQuery(query.toString(field)); ns.setDocPath(hitDoc.get("path")); ns.setLineNum(hitDoc.get("linenumber")); ns.setContents(hitDoc.get("contents")); contents.put(String.valueOf(i), ns); } } } catch (Exception ito) { ito.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } try { reader.close(); } catch (IOException ioe) { } }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * //from w w w .j a v a 2 s . c o m * @param groupField * @param searchField * @param searchStr * @param pageNo * @param pageSize * @param orderField ? * @param orderFieldType ? * @param desc ? ?? * @return */ @SuppressWarnings("deprecation") public static Map<String, List<Document>> group(String module, String groupField, String searchField, String searchStr, int pageNo, int pageSize, String orderField, Type orderFieldType, boolean desc) { Map<String, List<Document>> result = new LinkedHashMap<String, List<Document>>(10); IndexReader reader = null; try { reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher indexSearcher = new IndexSearcher(reader); GroupingSearch groupingSearch = new GroupingSearch(groupField); Sort sort = new Sort(new SortField(orderField, orderFieldType, desc)); groupingSearch.setGroupSort(sort); groupingSearch.setSortWithinGroup(sort); groupingSearch.setFillSortFields(true); groupingSearch.setCachingInMB(4.0, true); groupingSearch.setAllGroups(true); //groupingSearch.setAllGroupHeads(true); groupingSearch.setGroupDocsLimit(pageSize); QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, searchField, new StandardAnalyzer(Version.LUCENE_4_10_0)); Query query = parser.parse(searchStr); TopGroups<BytesRef> groupResult = groupingSearch.search(indexSearcher, query, (pageNo - 1) * pageSize, pageSize); System.out.println("?" + groupResult.totalHitCount + ", ?" + groupResult.groups.length); List<Document> groupData = null; for (GroupDocs<BytesRef> groupDocs : groupResult.groups) { groupData = new ArrayList<Document>(pageSize); String groupName = groupDocs.groupValue.utf8ToString(); for (ScoreDoc scoreDoc : groupDocs.scoreDocs) { groupData.add(indexSearcher.doc(scoreDoc.doc)); } result.put(groupName, groupData); groupData = null; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * ??/*from ww w . j a v a 2 s .co m*/ * @param in * @param searcher * @param query * @return * @throws IOException */ public static Document searchOne(BufferedReader in, IndexSearcher searcher, Query query) throws IOException { TopDocs results = searcher.search(query, 1); ScoreDoc[] hits = results.scoreDocs; int numTotal = results.totalHits; if (numTotal > 0) { return searcher.doc(hits[0].doc); } return null; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * /*ww w.ja va 2 s .c o m*/ * @param searcher * @param query * @param sort * @param pageNo * @param pageSize * @return * @throws IOException */ public static PageBean<Document> doPagingSearch(IndexSearcher searcher, Query query, Sort sort, int pageNo, int pageSize) throws IOException { if (sort == null) sort = new Sort(new SortField("id", Type.STRING)); int start = (pageNo - 1) * pageSize; int numHits = pageNo * pageSize; TopFieldCollector collector = TopFieldCollector.create(sort, numHits, false, false, false, false); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs(start, pageSize).scoreDocs; if (hits == null || hits.length < 1) return null; List<Document> docs = new ArrayList<Document>(pageSize); for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); docs.add(doc); } return new PageBean<Document>(pageSize, pageNo, collector.getTotalHits(), docs); }