List of usage examples for org.apache.lucene.search IndexSearcher search
protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Finds a document matching the object and returns the query used * /*from w w w .j av a 2s .c o m*/ * @param object * to search for in the index * @return the query that returns the document * @throws IllegalArgumentException * if the object is null * @throws IOException * if there are problems searching the index */ protected Query findDocument(EObject object) throws IllegalArgumentException, IOException { if (object == null) { throw new IllegalArgumentException("EObject cannot be null"); } Query rvalue = null; DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, object.eClass().getName())); bquery.add(classQuery, Occur.MUST); TermQuery uriQuery = new TermQuery( new Term(EMFIndexUtil.DOCUMENT_URI_KEY, object.eResource().getURI().toString())); bquery.add(uriQuery, Occur.MUST); TermQuery fragmentQuery = new TermQuery( new Term(EMFIndexUtil.FRAGMENT_URI_KEY, object.eResource().getURIFragment(object))); bquery.add(fragmentQuery, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, 1).scoreDocs; // Iterate through the results: if (hits.length > 0) { rvalue = bquery; } } 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 .j a v a2 s . co m * @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.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Deletes a document matching the URI//from w w w . ja v a2s. c om * * @param uri - the URI of the object reference to delete * @throws IllegalArgumentException * if the uri is null * @throws IOException * if there are issues saving the index */ public void deleteDocument(URI uri) throws IllegalArgumentException, IOException { if (uri == null) { throw new IllegalArgumentException("URI cannot be null"); } DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { TermQuery uriQuery = new TermQuery(new Term(EMFIndexUtil.DOCUMENT_URI_KEY, uri.toString())); ScoreDoc[] hits = searcher.search(uriQuery, null, 1).scoreDocs; // Iterate through the results: if (hits.length > 0) { writer.deleteDocuments(uriQuery); if (!holdCommits) { writer.commit(); } } } finally { reader.close(); } }
From source file:com.daniel.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.che.org/java/4_0/demo.html for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);//from w w w .j a va2 s. c om } String index = "index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } } IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
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. ja va 2 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 va 2 s .com } 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.eden.lucene.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);/*from w w w. j a v a2s . c o m*/ } String basePath = "D:/test/lucene"; String index = basePath + "/index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; /* for(int i = 0;i < args.length;i++) { if ("-index".equals(args[i])) { index = args[i+1]; i++; } else if ("-field".equals(args[i])) { field = args[i+1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i+1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i+1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i+1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i+1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } }*/ IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_46, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:com.evoapps.lucene.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public ArrayList<Publication> search(String queryTerm) throws Exception { list.clear();// w w w .j av a 2 s .c o m String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; /* if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0); }*/ String index = "index"; String field = "contents"; // String field = "Abstract"; String queries = null; int repeat = 0; boolean raw = false; String queryString = queryTerm; int hitsPerPage = 20; /*for(int i = 0;i < args.length;i++) { if ("-index".equals(args[i])) { index = args[i+1]; i++; } else if ("-field".equals(args[i])) { field = args[i+1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i+1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i+1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i+1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i+1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } } */ // index = "/home/subhash/LuceneFolder/Indexer"; index = "/home/subhash/Dropbox/LuceneFolder/IndexNewData"; IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } ArrayList<Publication> list = doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); return list; }
From source file:com.example.search.SearchFiles.java
License:Apache License
public static ArrayList<SearchResult> find(String input, int startPage) throws Exception { //String index = ".\\WebContent\\index";//D:\Users\admin\workspace\TestJsp\WebContent\index String index = "./index"; // System.out.println("index:"+index); String field = "content"; String queries = null;/* w w w.j av a2s . c o m*/ int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = HitsPerPage; IndexReader reader = IndexReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); // Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); Analyzer analyzer = new ICTCLASAnalyzer(); BufferedReader in = null; // in = new BufferedReader(input); int pos; ArrayList<SearchResult> result = new ArrayList<SearchResult>(); if (((pos = input.indexOf('\r')) != -1) || (pos = input.indexOf('\n')) != -1) input = input.substring(0, pos); QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer); // while (true) { // if (queries == null && queryString == null) { // prompt the user // System.out.println("Enter query: "); // } String line = queryString != null ? queryString : input; if (line == null || line.length() == -1) { searcher.close(); reader.close(); return result; } line = line.trim(); if (line.length() == 0) { searcher.close(); reader.close(); return result; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null, result, startPage); searcher.close(); reader.close(); return result; }
From source file:com.flaptor.hounder.indexer.LuceneUnicodeTest.java
License:Apache License
@TestInfo(testType = TestInfo.TestType.UNIT) public void testIndexedContent() { try {//from ww w .j a v a 2 s . c om String testString = "otorrinolaring\u00f3logo"; logger.debug("Using test string: " + testString); Document doc = new Document(); doc.add(new Field("field1", testString, Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc); writer.optimize(); writer.close(); IndexReader reader = IndexReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); int docId = searcher.search(new TermQuery(new Term("field1", testString)), null, 10).scoreDocs[0].doc; Document doc2 = searcher.doc(docId); String recoveredString = doc2.get("field1"); logger.debug("Recovered String: " + recoveredString); assertTrue("Strings do not match", testString.equals(recoveredString)); } catch (Exception e) { logger.error("Exception caught:" + e); assertTrue("exception", false); } }