List of usage examples for org.apache.lucene.index IndexReader close
@Override public final synchronized void close() throws IOException
From source file:com.appeligo.lucene.DidYouMeanIndexer.java
License:Apache License
public static void createSpellIndex(String field, Directory originalIndexDirectory, Directory spellIndexDirectory) throws IOException { IndexReader indexReader = null; try {/* w w w . j av a 2 s . c o m*/ indexReader = IndexReader.open(originalIndexDirectory); Dictionary dictionary = new LuceneDictionary(indexReader, field); SpellChecker spellChecker = new SpellChecker(spellIndexDirectory); spellChecker.indexDictionary(dictionary); if (log.isDebugEnabled()) { spellChecker = new SpellChecker(spellIndexDirectory); // need to re-open to see it work log.debug("Does 'next' exist in the dictionary? " + spellChecker.exist("next")); StringBuilder sb = new StringBuilder(); for (String s : spellChecker.suggestSimilar("noxt", 5, indexReader, "compositeField", true)) { sb.append(s + ", "); } log.debug("Best suggestions for 'noxt': " + sb); } } finally { if (indexReader != null) { indexReader.close(); } } }
From source file:com.appspot.socialinquirer.server.service.impl.AnalysisServiceImpl.java
License:Apache License
/** * Close index reader./*from ww w .jav a2 s. c om*/ * * @param reader the reader */ private void closeIndexReader(IndexReader reader) { if (reader != null) { try { reader.close(); } catch (Exception e) { } } }
From source file:com.aurel.track.lucene.search.LuceneSearcher.java
License:Open Source License
public static void closeIndexSearcherAndUnderlyingIndexReader(IndexSearcher indexSearcher, String index) { if (indexSearcher != null) { IndexReader indexReader = indexSearcher.getIndexReader(); try {//www . j a va2s . c o m if (indexReader != null) { indexReader.close(); } } catch (IOException e1) { LOGGER.error("Closing the " + index + " IndexReader failed with " + e1.getMessage()); } } }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
private synchronized IndexSearcher getSearcher() throws Exception { if (searcher == null) { searcher = new IndexSearcher(getDirectory(), true); }//from w w w . j ava 2s . com final IndexReader readerOld = searcher.getIndexReader(); final IndexReader readerNew = readerOld.reopen(true); /** get new instance only when index is updated */ if (readerNew != readerOld) { readerOld.close(); searcher = new IndexSearcher(readerNew); } return searcher; }
From source file:com.basistech.lucene.tools.LuceneQueryTool.java
License:Apache License
public static void main(String[] args) throws IOException, org.apache.lucene.queryparser.classic.ParseException { String charsetName = Charset.defaultCharset().name(); if (!"UTF-8".equals(charsetName)) { // Really only a problem on mac, where the default charset is MacRoman, // and it cannot be changed via the system Locale. System.err.println(String.format("defaultCharset is %s, but we require UTF-8.", charsetName)); System.err.println("Set -Dfile.encoding=UTF-8 on the Java command line, or"); System.err.println("set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 in the environment."); System.exit(1);/*w w w . j ava2s . c om*/ } Options options = LuceneQueryTool.createOptions(); CommandLineParser parser = new GnuParser(); CommandLine cmdline = null; try { cmdline = parser.parse(options, args); validateOptions(options, args); } catch (org.apache.commons.cli.ParseException e) { System.err.println(e.getMessage()); usage(options); System.exit(1); } String[] remaining = cmdline.getArgs(); if (remaining != null && remaining.length > 0) { System.err.println("unknown extra args found: " + Lists.newArrayList(remaining)); usage(options); System.exit(1); } String[] indexPaths = cmdline.getOptionValues("index"); IndexReader[] readers = new IndexReader[indexPaths.length]; for (int i = 0; i < indexPaths.length; i++) { Path path = FileSystems.getDefault().getPath(indexPaths[i]); readers[i] = DirectoryReader.open(FSDirectory.open(path)); } IndexReader reader = new MultiReader(readers, true); LuceneQueryTool that = new LuceneQueryTool(reader); String opt; opt = cmdline.getOptionValue("query-limit"); if (opt != null) { that.setQueryLimit(Integer.parseInt(opt)); } opt = cmdline.getOptionValue("output-limit"); if (opt != null) { that.setOutputLimit(Integer.parseInt(opt)); } opt = cmdline.getOptionValue("analyzer"); if (opt != null) { that.setAnalyzer(opt); } opt = cmdline.getOptionValue("query-field"); if (opt != null) { that.setDefaultField(opt); } opt = cmdline.getOptionValue("output"); PrintStream out = null; if (opt != null) { out = new PrintStream(new FileOutputStream(new File(opt)), true); that.setOutputStream(out); } if (cmdline.hasOption("show-id")) { that.setShowId(true); } if (cmdline.hasOption("show-hits")) { that.setShowHits(true); } if (cmdline.hasOption("show-score")) { that.setShowScore(true); } if (cmdline.hasOption("sort-fields")) { that.setSortFields(true); } boolean suppressNames = cmdline.hasOption("suppress-names"); Formatter.Format format = Formatter.Format.MULTILINE; opt = cmdline.getOptionValue("format"); if (opt != null) { format = Formatter.Format.fromName(opt); } if (cmdline.hasOption("tabular")) { // compatibility option format = Formatter.Format.TABULAR; } that.setFormatter(Formatter.newInstance(format, suppressNames)); String[] opts; opts = cmdline.getOptionValues("fields"); if (opts != null) { that.setFieldNames(Lists.newArrayList(opts)); } opt = cmdline.getOptionValue("regex"); if (opt != null) { Pattern p = Pattern.compile("^(.*?):/(.*)/$"); Matcher m = p.matcher(opt); if (m.matches()) { that.setRegex(m.group(1), Pattern.compile(m.group(2))); } else { System.err.println("Invalid regex, should be field:/regex/"); usage(options); System.exit(1); } } opts = cmdline.getOptionValues("query"); that.run(opts); if (out != null) { out.close(); } reader.close(); }
From source file:com.bdaum.zoom.lal.internal.LireActivator.java
License:Open Source License
public void closeIndexReader(IndexReader reader) throws IOException { searcherMap.remove(reader);//from w w w . j a v a 2 s . c o m try { if (reader instanceof CompositeReader) for (LeafReaderContext context : ((CompositeReader) reader).leaves()) context.reader().close(); reader.close(); } catch (AlreadyClosedException e) { // do nothing } }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public boolean exists(String id) { boolean tag = false; if (id.length() == 0) return tag; try {/*www. j a v a 2 s . co m*/ IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1); if (td.totalHits > 0) { tag = true; } searcher.close(); reader.close(); } catch (Exception e) { } return tag; }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public void load(String id, SEntity src) { try {/*from w w w .j ava 2 s . c om*/ IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1); if (td.totalHits > 0) { Document doc = searcher.doc(td.scoreDocs[0].doc); if (allowLoad(id, doc.get(SEntity.KIND))) { src.setSchema(doc.get(SEntity.SCHEMA)); read(src, doc); } } searcher.close(); reader.close(); } catch (Exception e) { } }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public int count(String kind, Query query, Filter filter, Sort sort, int max) { int tag = 0;//from www . j a v a 2s . co m try { IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST)); if (query != null) { boolQuery.add(new BooleanClause(query, Occur.MUST)); } TopDocs td = null; if (filter != null && sort != null) { td = searcher.search(boolQuery, filter, max, sort); } else if (filter != null) { td = searcher.search(boolQuery, filter, max); } else if (sort != null) { td = searcher.search(boolQuery, max, sort); } else { td = searcher.search(boolQuery, max); } tag = td.totalHits; searcher.close(); reader.close(); } catch (Exception e) { } return tag; }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int max) { List<SEntity> tag = new ArrayList<SEntity>(); try {/* w w w .java 2 s . co m*/ IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST)); if (query != null) { boolQuery.add(new BooleanClause(query, Occur.MUST)); } TopDocs td = null; if (filter != null && sort != null) { td = searcher.search(boolQuery, filter, max, sort); } else if (filter != null) { td = searcher.search(boolQuery, filter, max); } else if (sort != null) { td = searcher.search(boolQuery, max, sort); } else { td = searcher.search(boolQuery, max); } for (int i = 0; i < td.totalHits; i++) { SEntity item = new SEntity(this); Document doc = searcher.doc(td.scoreDocs[i].doc); item.setSchema(doc.get(SEntity.SCHEMA)); read(item, doc); tag.add(item); } searcher.close(); reader.close(); } catch (Exception e) { } return tag; }