List of usage examples for org.apache.lucene.index IndexReader close
@Override public final synchronized void close() throws IOException
From source file:com.browseengine.bobo.tools.CarDataDigest.java
License:Open Source License
public void digest(DataHandler handler) throws IOException { int numcars = getMaxDocs(); Random rand = new Random(); IndexReader reader = null; try {// w w w.j a v a2 s . co m reader = IndexReader.open(FSDirectory.open(getDataFile()), true); int carcount = reader.maxDoc(); Document[] docCache = new Document[carcount]; for (int i = 0; i < carcount; ++i) { docCache[i] = reader.document(i); } for (int i = 0; i < numcars; ++i) { if (i != 0 && i % 1000 == 0) { System.out.println(i + " cars indexed."); } Document doc = new Document(); int n = rand.nextInt(10); if (n == 0) { makeCar(doc, cars[rand.nextInt(cars.length)]); } else { Document srcDoc = docCache[rand.nextInt(carcount)]; makeCar(doc, srcDoc); } populateDocument(doc, null); handler.handleDocument(doc); } } finally { if (reader != null) { reader.close(); } } }
From source file:com.browseengine.local.service.geoindex.GeoResourceWriter.java
License:Open Source License
public synchronized void optimize() throws IOException, GeoIndexingException { if (_writer != null) { if (_path != null) { _writer.optimize();// w w w. j ava 2 s . co m File path2 = new File(_path.getParentFile(), _path.getName() + ".tmp"); _writer.close(); _writer = null; if (_path.renameTo(path2)) { IndexReader reader = null; TermEnum termEnum = null; TermDocs termDocs = null; try { reader = IndexReader.open(path2); int maxDoc = reader.maxDoc(); if (maxDoc <= 0) { throw new GeoIndexingException("can't optimize an index with " + maxDoc + " docs"); } LonDocid[] lonDocids = new LonDocid[maxDoc]; String fld = GeoSearchFields.LON.getField().intern(); Term term = new Term(fld, ""); termEnum = reader.terms(term); termDocs = reader.termDocs(); while ((term = termEnum.term()) != null && term.field() == fld) { double lon = Double.parseDouble(term.text()); termDocs.seek(term); while (termDocs.next()) { int docid = termDocs.doc(); lonDocids[docid] = new LonDocid(docid, lon); } termEnum.next(); } termDocs.close(); termDocs = null; termEnum.close(); termEnum = null; Arrays.sort(lonDocids); init(_path, true); for (int i = 0; i < lonDocids.length; i++) { int docid = lonDocids[i].docid; Document doc = reader.document(docid); // all fields are stored String name = doc.get(GeoSearchFields.NAME.getField()); String description = doc.get(GeoSearchFields.DESCRIPTION.getField()); String addressStr = doc.get(GeoSearchFields.ADDRESS.getField()); String phoneStr = doc.get(GeoSearchFields.PHONE.getField()); long phoneNumber = LocalResource.NO_PHONE_NUMBER; if (phoneStr != null && phoneStr.length() > 0) { phoneNumber = Long.parseLong(phoneStr); } String lonStr = doc.get(GeoSearchFields.LON.getField()); double lon = Double.parseDouble(lonStr); String latStr = doc.get(GeoSearchFields.LAT.getField()); double lat = Double.parseDouble(latStr); LocalResource resource = new LocalResource(name, description, addressStr, phoneNumber, lon, lat); addResource(resource); } reader.close(); reader = null; _writer.optimize(); LOGGER.info("successfully completed optimization of index at " + _path.getAbsolutePath()); } finally { try { // erase the tmp dir recursiveDelete(path2); } finally { try { if (reader != null) { reader.close(); } } finally { try { if (termEnum != null) { termEnum.close(); } } finally { try { if (termDocs != null) { termDocs.close(); } } finally { reader = null; termDocs = null; termEnum = null; } } } } } } else { init(_path, false); throw new GeoIndexingException("trouble doing the rename from " + _path.getAbsolutePath() + " to " + path2.getAbsolutePath() + "; check permissions"); } } else { _writer.optimize(); } } else { throw new GeoIndexingException("attempt to optimize a closed " + GeoResourceWriter.class.getName()); } }
From source file:com.browseengine.local.service.impl.test.BrowseGeoSearchTest.java
License:Open Source License
protected void setUp() throws Exception { super.setUp(); // register the plugin first! BrowseGeoSearchInitializer.init();//from w w w. j a v a 2s . c o m String path = System.getProperty(INDEX_KEY); if (path == null) { throw new Exception("no '" + INDEX_KEY + "' set; can't run tests"); } IndexReader reader = null; BoboIndexReader breader = null; Directory dir = null; boolean success = false; try { dir = FSDirectory.getDirectory(path, false); File f = new File(path, "field.xml"); FieldConfiguration config = FieldConfiguration.loadFieldConfiguration(f); GeoScoreAdjusterFactory factory = new GeoScoreAdjusterFactory(); reader = IndexReader.open(dir); breader = new BoboIndexReader(reader, dir, config); breader.setScoreAdjusterFactory(factory); // find the first geosearch field plugin GeoSearchFieldPlugin gplugin = new GeoSearchFieldPlugin(); String[] fieldNames = config.getFieldNames(); for (String fieldName : fieldNames) { FieldPlugin plugin = config.getFieldPlugin(fieldName); if (plugin.getTypeString().equals(gplugin.getTypeString())) { _geosearchField = fieldName; success = true; } } if (success) { success = false; _browseService = BrowseServiceFactory.createBrowseService(breader); success = true; } } finally { if (!success) { try { if (breader != null) { breader.close(); } } finally { try { if (reader != null) { reader.close(); } } finally { try { if (dir != null) { dir.close(); } } finally { // } } } } } }
From source file:com.chenyi.langeasy.lucene.SearchTest.java
License:Apache License
public static void search() throws Exception { String index = "F:/Personal/ws_indigo/lucene/index"; String field = "contents"; IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); QueryParser parser = new QueryParser(field, analyzer); String[] wordLst = new String[] { "abaft", "abase", "abnegation", "abominate", "abridge", "adjure", "adroit", "adulterate", "aggrandize", "allegory", "amalgam", "amalgamate", "amiable", "amortize", "anachronism", "apposite", "approbatory", "archetype", "arrogate", "artifice", "aseptic", "asperity", "assay", "attenuate", "baneful", "bauble", "behoove", "bemuse", "besmirch", "betroth", "boor", "bumptious", "bungler", "burgeon", "canard", "castigate", "catharsis", "cavil", "chaffing", "chary", "choleric", "chortle", "churlishness", "circumlocution", "circumlocutory", "cloture", "coda", "coffer", "cogitate", "cognate", "cognizant", "comeliness", "commiserate", "complaisance", "conjoin", "connotative", "contumacious", "contusion", "conviviality", "convoke", "corpulence", "curmudgeon", "dally", "dauntless", "decadence", "decisiveness", "delineate", "deliquesce", "denigrate", "deprecate", "depredation", "descant", "despoil", "determinate", "dichotomy", "digress", "dilettante", "disapprobation", "disheartened", "disputatious", "dissemble", "dissonant", "distention", "divestiture", "ebullience", "ecclesiastic", "edify", "educe", "efface", "effervescence", "effluvium", "effrontery", "effusive", "egress", "elaboration", "ellipsis", "embarkation", "enamored", "encomium", "encumber", "enervate", "enfeeble", "epicure", "epigram", "equinox", "equivocations", "estimable", "euphony", "evanescent", "exculpate", "exigent", "extemporize", "extricable", "exultation", "fatuous", "fecund", "fervid", "fetter", "froward", "fulsome", "fustian", "garrulous", "germane", "gerrymander", "gibber", "hackneyed", "harangue", "heretic", "homeostasis", "idiosyncrasy", "impale", "impenitent", "imperturbable", "impiety", "impolitic", "imprecate", "improvident", "impugn", "imputation", "inchoate", "incommodious", "incorporeal", "indigence", "indolent", "indubitably", "infamy", "ingenuous", "inimical", "instigate", "insubordinate", "intercede", "inveterate", "iota", "irreproachable", "kith", "knavery", "lambaste", "lambent", "latency", "levity", "licentious", "ligneous", "litigate", "lugubrious", "macerate", "maculate", "malediction", "malinger", "maudlin", "mendacious", "mesmerize", "minatory", "miscreant", "miser", "mollify", "morose", "munificent", "nefariousness", "nettle", "noisome", "obdurate", "obeisance", "obfuscate", "objurgate", "obstinate", "obtrude", "obviate", "odium", "omniscient", "opalescent", "opprobrious", "ossify", "ostensible", "ostracize", "palindrome", "palliate", "pallor", "panegyric", "parsimonious", "peccadillo", "pedagogue", "pellucid", "penitent", "perdition", "perfidious", "perquisite", "phlegmatic", "pinioned", "plenary", "polemicist", "portend", "prate", "prefatory", "preponderate", "prescience", "prevaricate", "promontory", "propinquity", "propitiate", "proselytize", "provident", "qualm", "quiescence", "raconteur", "ramification", "recondite", "recumbent", "recusant", "redolent", "refurbish", "relegate", "remonstrate", "renascence", "repast", "reprehend", "reprobate", "reproof", "repudiate", "retroaction", "revile", "rivet", "roseate", "ruffian", "sagacious", "salutatory", "sapid", "saturnine", "savant", "scanty", "sedulous", "servile", "slovenly", "soliloquy", "solubility", "spelunker", "splenetic", "suffuse", "sunder", "suppliant", "surreptitious", "torpid", "traduce", "tranquillity", "transmutation", "transpire", "troth", "truncate", "tumid", "turpitude", "unfeigned", "unwonted", "usury", "winsome", "zealot", "zephyr" }; List<String> unmatchList = new ArrayList<>(); for (String word : wordLst) { boolean result = search(searcher, parser, word); if (!result) { unmatchList.add(word);// w ww. jav a 2s . co m } } reader.close(); System.out.println("findWTotal: " + findWTotal); System.out.println("findSTotal: " + findSTotal); System.out.println(new JSONArray(unmatchList).toString(3)); System.out.println(unmatchList.size()); System.out.println(); String[] arr2 = new String[] { "abaft", "approbatory", "betroth", "bungler", "churlishness", "contumacious", "deliquesce", "extricable", "froward", "imprecate", "incommodious", "ligneous", "minatory", "nefariousness", "objurgate", "obtrude", "opprobrious", "preponderate", "recusant", "retroaction", "sapid", "sedulous", "traduce", "tumid", "unwonted" }; String[] unmatchArr = new String[unmatchList.size()]; unmatchArr = unmatchList.toArray(unmatchArr); CompareResult.compare(arr2, unmatchArr); // boolean result = search(searcher, parser, "abc"); }
From source file:com.chimpler.example.FacetLuceneAdvancedSearcher.java
License:Apache License
public static void main(String args[]) throws Exception { if (args.length != 5) { System.err.println(/*from w ww. j a v a 2s .c o m*/ "Parameters: [index directory] [taxonomy directory] [query] [field drilldown] [value drilldown]"); System.exit(1); } String indexDirectory = args[0]; String taxonomyDirectory = args[1]; String query = args[2]; String fieldDrilldown = args[3]; String valueDrilldown = args[4]; IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory))); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); CategoryPath drillDownCategoryPath = new CategoryPath(fieldDrilldown + "/" + valueDrilldown, '/'); FacetSearchParams searchParams = new FacetSearchParams(); searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); searchParams.facetRequests.add(new CountFacetRequest(drillDownCategoryPath, 100)); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); //luceneQuery = DrillDownQuery.query(luceneQuery, drillDownCategoryPath); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); }
From source file:com.chimpler.example.FacetLuceneIndexer.java
License:Apache License
public static void main(String args[]) throws Exception { // if (args.length != 3) { // System.err.println("Parameters: [index directory] [taxonomy directory] [json file]"); // System.exit(1); // }// ww w . j a va 2s . co m String indexDirectory = "index"; String taxonomyDirectory = "taxonomy"; String jsonFileName = "/home/qiuqiang/workspace/facet-lucene-example/books.json"; IndexWriterConfig writerConfig = new IndexWriterConfig(LUCENE_VERSION, new WhitespaceAnalyzer(LUCENE_VERSION)); writerConfig.setOpenMode(OpenMode.APPEND); IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File(indexDirectory)), writerConfig); TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(MMapDirectory.open(new File(taxonomyDirectory)), OpenMode.APPEND); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); String content = IOUtils.toString(new FileInputStream(jsonFileName)); JSONArray bookArray = new JSONArray(content); Field idField = new IntField("id", 0, Store.YES); Field titleField = new TextField("title", "", Store.YES); Field authorsField = new TextField("authors", "", Store.YES); Field bookCategoryField = new TextField("book_category", "", Store.YES); indexWriter.deleteAll(); FacetFields facetFields = new FacetFields(taxonomyWriter); for (int i = 0; i < bookArray.length(); i++) { Document document = new Document(); JSONObject book = bookArray.getJSONObject(i); int id = book.getInt("id"); String title = book.getString("title"); String bookCategory = book.getString("book_category"); List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>(); String authorsString = ""; JSONArray authors = book.getJSONArray("authors"); for (int j = 0; j < authors.length(); j++) { String author = authors.getString(j); if (j > 0) { authorsString += ", "; } categoryPaths.add(new CategoryPath("author", author)); authorsString += author; } categoryPaths.add(new CategoryPath("book_category" + bookCategory, '/')); idField.setIntValue(id); titleField.setStringValue(title); authorsField.setStringValue(authorsString); bookCategoryField.setStringValue(bookCategory); facetFields.addFields(document, categoryPaths); document.add(idField); document.add(titleField); document.add(authorsField); document.add(bookCategoryField); indexWriter.addDocument(document); System.out.printf("Book: id=%d, title=%s, book_category=%s, authors=%s\n", id, title, bookCategory, authors); } taxonomyWriter.prepareCommit(); try { taxonomyWriter.commit(); } catch (Exception e) { taxonomyWriter.rollback(); } // taxonomyWriter.close(); // // indexWriter.commit(); // indexWriter.close(); String query = "story"; IndexReader indexReader = DirectoryReader.open(indexWriter, false); IndexReader indexReader2 = DirectoryReader.open(indexWriter, false); System.out.println(indexReader == indexReader2); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader newTaxonomyReader = DirectoryTaxonomyReader.openIfChanged(taxonomyReader); if (newTaxonomyReader != null) { TaxonomyReader tmp = taxonomyReader; taxonomyReader = newTaxonomyReader; tmp.close(); } else { System.out.println("null"); } ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>(); facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); FacetSearchParams searchParams = new FacetSearchParams(facetRequests); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); taxonomyWriter.commit(); taxonomyWriter.close(); indexWriter.commit(); indexWriter.close(); }
From source file:com.chimpler.example.FacetLuceneSearcher.java
License:Apache License
public static void main(String args[]) throws Exception { // if (args.length != 3) { // System.err.println("Parameters: [index directory] [taxonomy directory] [query]"); // System.exit(1); // }/* w ww . java 2 s. c o m*/ String indexDirectory = "index"; String taxonomyDirectory = "taxonomy"; String query = "story"; IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory))); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>(); facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); FacetSearchParams searchParams = new FacetSearchParams(facetRequests); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); }
From source file:com.codecrate.shard.search.ObjectIndexer.java
License:Apache License
private void closeReader(IndexReader reader) { if (null != reader) { try {/* ww w. j ava 2s.co m*/ reader.close(); } catch (IOException e) { LOG.warn("Error closing index reader for index " + directory, e); } } }
From source file:com.codenvy.test.lucene.DeleteFilesWithSameName.java
License:Open Source License
public static void searchAndPrintResult(Path indexPath) throws Exception { IndexReader reader = DirectoryReader.open(FSDirectory.open(indexPath)); IndexSearcher searcher = new IndexSearcher(reader); Query query = new PrefixQuery(new Term(PATH, filesDirPath + "/File1")); TopDocs topDocs = searcher.search(query, 100); System.out.println();/*w w w. j a v a 2 s .c o m*/ System.out.println("=========================== search ================================="); final String[] result = new String[topDocs.scoreDocs.length]; for (int i = 0, length = result.length; i < length; i++) { result[i] = searcher.doc(topDocs.scoreDocs[i].doc).getField(PATH).stringValue(); System.out.println(result[i]); } reader.close(); }
From source file:com.codeReading.core.opengrok.SearchHelper.java
License:Open Source License
/** * If a search did not return a hit, one may use this method to obtain * suggestions for a new search.//from w w w . ja v a2 s. c o m * * <p> Parameters which should be populated/set at this time: <ul> * <li>{@link #projects}</li> <li>{@link #dataRoot}</li> * <li>{@link #builder}</li> </ul> * * @return a possible empty list of suggestions. */ public List<Suggestion> getSuggestions() { if (projects == null) { return new ArrayList<>(0); } String name[]; if (projects.isEmpty()) { name = new String[] { "/" }; } else if (projects.size() == 1) { name = new String[] { projects.first() }; } else { name = new String[projects.size()]; int ii = 0; for (String proj : projects) { name[ii++] = proj; } } List<Suggestion> res = new ArrayList<>(); List<String> dummy = new ArrayList<>(); FSDirectory dir; IndexReader ir = null; Term t; for (int idx = 0; idx < name.length; idx++) { Suggestion s = new Suggestion(name[idx]); try { dir = FSDirectory.open(new File(indexDir, name[idx])); ir = DirectoryReader.open(dir); if (builder.getFreetext() != null && !builder.getFreetext().isEmpty()) { t = new Term(QueryBuilder.FULL, builder.getFreetext()); getSuggestion(t, ir, dummy); s.freetext = dummy.toArray(new String[dummy.size()]); dummy.clear(); } if (builder.getRefs() != null && !builder.getRefs().isEmpty()) { t = new Term(QueryBuilder.REFS, builder.getRefs()); getSuggestion(t, ir, dummy); s.refs = dummy.toArray(new String[dummy.size()]); dummy.clear(); } if (builder.getDefs() != null && !builder.getDefs().isEmpty()) { t = new Term(QueryBuilder.DEFS, builder.getDefs()); getSuggestion(t, ir, dummy); s.defs = dummy.toArray(new String[dummy.size()]); dummy.clear(); } //TODO suggest also for path and history? if ((s.freetext != null && s.freetext.length > 0) || (s.defs != null && s.defs.length > 0) || (s.refs != null && s.refs.length > 0)) { res.add(s); } } catch (IOException e) { log.log(Level.WARNING, "Got exception while getting " + "spelling suggestions: ", e); } finally { if (ir != null) { try { ir.close(); } catch (IOException ex) { log.log(Level.WARNING, "Got exception while " + "getting spelling suggestions: ", ex); } } } } return res; }