List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context)
From source file:bajavista.Buscador.java
public ArrayList<Informacion> buscarContenido(String busqueda) throws IOException, ParseException { StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_43); File indexDirES = new File(dirIndexES); Directory indexES = FSDirectory.open(indexDirES); //File indexDirNONES = new File(dirIndexNONES); //Directory indexNONES = FSDirectory.open(indexDirNONES); // 2. Query//from w w w . java 2 s . c om String querystr = busqueda; Query q = new QueryParser(Version.LUCENE_43, "text", analyzer).parse(querystr); //Query qNONES = new QueryParser(Version.LUCENE_43, "contenido", analyzer).parse(querystr); // 3. Search int hitsPage = 1024; IndexReader reader = DirectoryReader.open(indexES); IndexSearcher searcher = new IndexSearcher(reader); //IndexReader readerNONES = DirectoryReader.open(indexNONES); //IndexSearcher searcherNONES = new IndexSearcher(readerNONES); TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPage, true); //TopScoreDocCollector collectorNONES = TopScoreDocCollector.create(hitsPage, true); searcher.search(q, collector); //searcherNONES.search(q, collectorNONES); ScoreDoc[] hits = collector.topDocs().scoreDocs; // ScoreDoc[] hitsNONES = collectorNONES.topDocs().scoreDocs; // 4. Return results for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document data = searcher.doc(docId); info = new Informacion(Integer.parseInt(data.get("idUser")), Long.parseLong(data.get("timestamp")), data.get("text"), Double.parseDouble(data.get("objective")), Double.parseDouble(data.get("subjective")), Double.parseDouble(data.get("positive")), Double.parseDouble(data.get("negative")), Integer.parseInt(data.get("need"))); listaInfo.add(info); } /*System.out.println("No ES Found " + hitsNONES.length + " hits."); for(int i=0;i<hitsNONES.length;++i) { int docId = hitsNONES[i].doc; Document d = searcherNONES.doc(docId); System.out.println((i + 1) + ". " + d.get("es") + "\t" + d.get("contenido")); }*/ reader.close(); //readerNONES.close(); return listaInfo; }
From source file:bbejeck.nosql.lucene.LuceneSqlFileSystemSearchBase.java
License:Apache License
@Override public void openSearcher() throws Exception { ireader = DirectoryReader.open(fsDirectory); isearcher = new IndexSearcher(ireader); }
From source file:bbejeck.nosql.lucene.LuceneSqlSearchBase.java
License:Apache License
public void openSearcher() throws Exception { ireader = DirectoryReader.open(ramDirectory); isearcher = new IndexSearcher(ireader); }
From source file:be.ugent.tiwi.sleroux.newsrec.newsreclib.termExtract.LuceneTopTermExtract.java
License:Apache License
/** * Returns the 10 most important terms in the document with the specified * id.//from w ww. j a va 2 s.c o m * * @param id * @param reader * @param numberOfTerms * @return */ public Map<String, Double> getTopTerms(String id, IndexReader reader, int numberOfTerms) { try { IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(1, true); Query q = new TermQuery(new Term("id", id)); searcher.search(q, collector); if (collector.getTotalHits() > 0) { int docNr = collector.topDocs().scoreDocs[0].doc; return getTopTerms(docNr, reader, numberOfTerms); } else { logger.warn("No document found with id=" + id); } } catch (IOException ex) { logger.error(ex); } return new HashMap<>(0); }
From source file:be.ugent.tiwi.sleroux.newsrec.recommendationstester.LuceneTopTermExtract.java
License:Apache License
public Map<String, Double> getTopTerms(String id, IndexReader reader, int numberOfTerms) { try {/* w w w . ja v a2s .c o m*/ IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(1, true); Query q = new TermQuery(new Term("id", id)); searcher.search(q, collector); if (collector.getTotalHits() > 0) { int docNr = collector.topDocs().scoreDocs[0].doc; return getTopTerms(docNr, reader, numberOfTerms); } else { logger.warn("No document found with id=" + id); } } catch (IOException ex) { logger.error(ex); } return new HashMap<>(0); }
From source file:biospectra.classify.Classifier.java
License:Apache License
private void initialize(File indexPath, int kmerSize, int kmerSkips, boolean minStrandKmer, double minShouldMatch, QueryGenerationAlgorithm queryGenerationAlgorithm, Similarity similarity) throws Exception { if (!indexPath.exists() || !indexPath.isDirectory()) { throw new IllegalArgumentException("indexPath is not a directory or does not exist"); }/*from ww w . ja v a 2 s .com*/ this.indexPath = indexPath; this.kmerSize = kmerSize; this.kmerSkips = kmerSkips; this.minStrandKmer = minStrandKmer; this.queryAnalyzer = new KmerQueryAnalyzer(this.kmerSize, this.kmerSkips, this.minStrandKmer); Directory dir = new MMapDirectory(this.indexPath.toPath()); this.indexReader = DirectoryReader.open(dir); this.indexSearcher = new IndexSearcher(this.indexReader); if (similarity != null) { this.indexSearcher.setSimilarity(similarity); } this.minShouldMatch = minShouldMatch; this.queryGenerationAlgorithm = queryGenerationAlgorithm; BooleanQuery.setMaxClauseCount(10000); }
From source file:book.Searcher.java
License:Apache License
public static void search(String indexDir, String q) throws IOException, ParseException { IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir).toPath())); IndexSearcher is = new IndexSearcher(reader); //alt: Directory dir = FSDirectory.open(new File(indexDir)); // 3 //alt: IndexSearcher is = IndexSearcher(dir); // 3 QueryParser parser = new QueryParser("contents", // 4 new StandardAnalyzer()); // 4 Query query = parser.parse(q); // 4 long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); // 5 long end = System.currentTimeMillis(); System.err.println("Found " + hits.totalHits + // 6 " document(s) (in " + (end - start) + // 6 " milliseconds) that matched query '" + // 6 q + "':"); // 6 for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); // 7 System.out.println(doc.get("fullpath")); // 8 }//from w ww. ja v a 2s.c om //was: is.close(); // 9 }
From source file:bostoncase.widgets.SearchFiles.java
License:Apache License
private static void init(String index) throws IOException { reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); searcher = new IndexSearcher(reader); analyzer = new StandardAnalyzer(); parser = new QueryParser(field, analyzer); }
From source file:bostoncase.widgets.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);//w w w. j a va 2s. c o m } String index = "/Users/michaelhundt/Documents/Meine/Studium/MASTER/MasterProject/data/lucene_index/"; String field = "content"; 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(Paths.get(index))); // IndexReader reader = StandardDirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); BufferedReader in = null; if (queries != null) { in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8); } else { in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); } QueryParser parser = new QueryParser(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); if (line.equals("geo")) { // query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -70.9235, -71.1908); // query = new GeoPointInBBoxQuery("geo", -71.1908, -70.9235, 42.2279, 42.3969); query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -71.1908, -70.9235); } 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, 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:br.andrew.lucene.testing.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(final String[] args) throws Exception { final 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 ww . j a v a2s. c o m } 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++; } } final IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); final IndexSearcher searcher = new IndexSearcher(reader); final 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")); } final 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; } final Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark final Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } final Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } SearchFiles.doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }