List of usage examples for org.apache.lucene.index IndexWriter IndexWriter
public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException
conf
. From source file:com.berico.clavin.index.IndexDirectoryBuilder.java
License:Apache License
/** * Turns a GeoNames gazetteer file into a Lucene index, and adds * some supplementary gazetteer records at the end. * /*w w w . j a va 2 s . c om*/ * @param args not used * @throws IOException */ public static void main(String[] args) throws IOException { logger.info("Indexing... please wait."); // Create a new index file on disk, allowing Lucene to choose // the best FSDirectory implementation given the environment. // TODO: delete this directory first, if it exists FSDirectory index = FSDirectory.open(new File("./IndexDirectory")); // indexing by lower-casing & tokenizing on whitespace Analyzer indexAnalyzer = new WhitespaceLowerCaseAnalyzer(); // create the object that will actually build the Lucene index IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(Version.LUCENE_40, indexAnalyzer)); // open the gazetteer files to be loaded BufferedReader r = new BufferedReader( new InputStreamReader(new FileInputStream(new File(pathToGazetteer)), "UTF-8")); BufferedReader r2 = new BufferedReader(new InputStreamReader( new FileInputStream(new File("./src/main/resources/SupplementaryGazetteer.txt")), "UTF-8")); String line; // let's see how long this takes... Date start = new Date(); // load GeoNames gazetteer into Lucene index while ((line = r.readLine()) != null) addToIndex(indexWriter, line); // add supplementary gazetteer records to index while ((line = r2.readLine()) != null) addToIndex(indexWriter, line); // that wasn't so long, was it? Date stop = new Date(); logger.info("[DONE]"); logger.info(indexWriter.maxDoc() + " geonames added to index."); logger.info("Merging indices... please wait."); indexWriter.close(); index.close(); r.close(); r2.close(); logger.info("[DONE]"); DateFormat df = new SimpleDateFormat("HH:mm:ss"); long elapsed_MILLIS = stop.getTime() - start.getTime(); logger.info("Process started: " + df.format(start) + ", ended: " + df.format(stop) + "; elapsed time: " + MILLISECONDS.toSeconds(elapsed_MILLIS) + " seconds."); }
From source file:com.berico.clavin.resolver.impl.lucene.LuceneComponentsFactory.java
License:Apache License
/** * Initialize the IndexWriter (and other components). * @return This object (it's needlessly fluent!). * @throws IOException/* ww w . j a va 2s. c om*/ */ public LuceneComponentsFactory initializeWriter() throws IOException { initializeCommon(); // instantiate the index writer indexWriter = new IndexWriter(index, new IndexWriterConfig(Version.LUCENE_43, indexAnalyzer)); return this; }
From source file:com.bericotech.clavin.index.IndexDirectoryBuilder.java
License:Apache License
public void buildIndex(final File indexDir, final List<File> gazetteerFiles, final File altNamesFile) throws IOException { LOG.info("Indexing... please wait."); indexCount = 0;/* w w w .j a v a2 s .c o m*/ // Create a new index file on disk, allowing Lucene to choose // the best FSDirectory implementation given the environment. FSDirectory index = FSDirectory.open(indexDir); // indexing by lower-casing & tokenizing on whitespace Analyzer indexAnalyzer = new WhitespaceLowerCaseAnalyzer(); // create the object that will actually build the Lucene index indexWriter = new IndexWriter(index, new IndexWriterConfig(Version.LUCENE_4_9, indexAnalyzer)); // let's see how long this takes... Date start = new Date(); // if we were given an alternate names file, process it if (altNamesFile != null) { loadAlternateNames(altNamesFile); } // load GeoNames gazetteer into Lucene index String line; int count = 0; for (File gazetteer : gazetteerFiles) { LOG.info("Processing Gazetteer: {}", gazetteer.getAbsolutePath()); BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(gazetteer), "UTF-8")); while ((line = reader.readLine()) != null) { try { count += 1; // print progress update to console if (count % 100000 == 0) { LOG.info("rowcount: " + count); } GeoName geoName = BasicGeoName.parseFromGeoNamesRecord(line); resolveAncestry(geoName); } catch (IOException e) { LOG.info("Skipping... Error on line: {}", line); } catch (RuntimeException re) { LOG.info("Skipping... Error on line: {}", line); } } reader.close(); } // that wasn't so long, was it? Date stop = new Date(); LOG.info("Unresolved GeoNames (Pre-resolution)"); logUnresolved(); resolveUnresolved(); LOG.info("Unresolved GeoNames (Post-resolution)"); logUnresolved(); LOG.info("Indexing unresolved GeoNames."); for (Set<GeoName> geos : unresolvedMap.values()) { for (GeoName nm : geos) { indexGeoName(nm); } } LOG.info("[DONE]"); LOG.info("{} geonames added to index. ({} records)", indexWriter.maxDoc(), indexCount); LOG.info("Merging indices... please wait."); indexWriter.close(); index.close(); LOG.info("[DONE]"); DateFormat df = new SimpleDateFormat("HH:mm:ss"); long elapsed_MILLIS = stop.getTime() - start.getTime(); LOG.info("Process started: " + df.format(start) + ", ended: " + df.format(stop) + "; elapsed time: " + MILLISECONDS.toSeconds(elapsed_MILLIS) + " seconds."); }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
protected void createEntity(SEntity src) { if (src.getId().length() == 0) return;/*from w ww. j av a 2s . c om*/ if (src.getKind().length() == 0) return; try { if (!src.getKind().equals(KIND_QUOTA)) { if (!quotaCreate(src)) return; } backup(src); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc); Document doc = new Document(); write(src, doc); writer.addDocument(doc); writer.close(); } catch (Exception e) { } }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
protected void updateEntity(SEntity src) { if (src.getId().length() == 0) return;//from w w w .j a v a 2 s .c om if (src.getKind().length() == 0) return; try { if (!src.getKind().equals(KIND_QUOTA)) { if (!quotaUpdate(src)) return; } backup(src); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc); Document doc = new Document(); write(src, doc); writer.updateDocument(new Term(SEntity.ID, src.getId()), doc); writer.close(); } catch (Exception e) { } }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
protected void deleteEntity(String id) { if (id.length() == 0) return;/*from w w w. ja v a 2s. c om*/ String kind = ""; try { 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); kind = doc.get(SEntity.KIND); } searcher.close(); reader.close(); } catch (Exception e) { } if (kind.length() == 0) return; if (!allowDelete(id, kind)) return; try { if (!kind.equals(KIND_QUOTA)) { if (!quotaDelete(id, kind)) return; } removeBackup(id, kind); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc); writer.deleteDocuments(new Term(SEntity.ID, id)); writer.close(); } catch (Exception e) { } }
From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForCustomer.java
License:Open Source License
@SuppressWarnings("deprecation") public void createIndex() throws IOException { // store in the directory Directory directory = new SimpleFSDirectory(indexFile); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, indexWriterConfig);// FetchOption option = new FetchOption(); customerlist = customerDao.findAll(option); if (customerlist.size() > 0) { long time1 = System.currentTimeMillis(); for (int i = 0; i < customerlist.size(); i++) { Document document = new Document(); document.add(new Field("nickname", customerlist.get(i).getNickname(), Field.Store.YES, Field.Index.ANALYZED)); document.add(/*www . j a v a 2 s.com*/ new Field("email", customerlist.get(i).getEmail(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("password", customerlist.get(i).getPassword(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("gender", String.valueOf(customerlist.get(i).getGender()), Field.Store.YES, Field.Index.ANALYZED)); document.add( new Field("name", customerlist.get(i).getName(), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(document); } long time2 = System.currentTimeMillis(); System.out.println("" + writer.numDocs() + ""); System.out.println("" + (time2 - time1) + ""); } writer.close(); }
From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForItem.java
License:Open Source License
@SuppressWarnings("deprecation") public void createIndex() throws IOException { // store in the directory Directory directory = new SimpleFSDirectory(indexFile); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, indexWriterConfig);// itemlist = itemDao.findAll();//w ww . java2 s . c o m if (itemlist.size() > 0) { long time1 = System.currentTimeMillis(); for (int i = 0; i < itemlist.size(); i++) { Document document = new Document(); document.add(new Field("name", itemlist.get(i).getName(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("introduction", itemlist.get(i).getIntroduction(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("price", String.valueOf(itemlist.get(i).getPrice()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("urlPicture", itemlist.get(i).getUrlPicture(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("extra1", String.valueOf(itemlist.get(i).getExtra1()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("extra2", String.valueOf(itemlist.get(i).getExtra2()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("countPurchase", String.valueOf(itemlist.get(i).getCountPurchase()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("countFavourite", String.valueOf(itemlist.get(i).getCountFavourite()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("countClick", String.valueOf(itemlist.get(i).getCountClick()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("vote", String.valueOf(itemlist.get(i).getVote()), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(document); } long time2 = System.currentTimeMillis(); System.out.println("" + writer.numDocs() + ""); System.out.println("" + (time2 - time1) + ""); } writer.close(); }
From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForOrder.java
License:Open Source License
@SuppressWarnings("deprecation") public void createIndex() throws IOException { // store in the directory Directory directory = new SimpleFSDirectory(indexFile); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, indexWriterConfig);// Date from = new Date(), to = new Date(); orderlist = orderDao.findAll(from, to); if (orderlist.size() > 0) { long time1 = System.currentTimeMillis(); for (int i = 0; i < orderlist.size(); i++) { Document document = new Document(); document.add(new Field("id", String.valueOf(orderlist.get(i).getId()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("psCustomer", String.valueOf(orderlist.get(i).getPsCustomer()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("idShop", String.valueOf(orderlist.get(i).getId()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("status", String.valueOf(orderlist.get(i).getStatus()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("trackingNumber", orderlist.get(i).getTrackingNumber(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("address", orderlist.get(i).getPsRecipient().getAddresss(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("postalCode", orderlist.get(i).getTrackingNumber(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("nameRecipient", orderlist.get(i).getPsRecipient().getPsCustomer().getName(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("phoneRecipient", orderlist.get(i).getPsRecipient().getPhoneNumber(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("priceTotal", String.valueOf(orderlist.get(i).getPriceTotal()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("timeCreated", String.valueOf(orderlist.get(i).getTimeCreated()), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(document); }/*from w w w . jav a 2 s . c o m*/ long time2 = System.currentTimeMillis(); System.out.println("" + writer.numDocs() + ""); System.out.println("" + (time2 - time1) + ""); } writer.close(); }
From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForSeller.java
License:Open Source License
@SuppressWarnings("deprecation") public void createIndex() throws IOException { // store in the directory Directory directory = new SimpleFSDirectory(indexFile); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, indexWriterConfig);// FetchOption option = new FetchOption(); sellerlist = sellerDao.findAll(new FetchOption().limit(100)); if (sellerlist.size() > 0) { long time1 = System.currentTimeMillis(); for (int i = 0; i < sellerlist.size(); i++) { Document document = new Document(); document.add(new Field("id", String.valueOf(sellerlist.get(i).getId()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("nickname", sellerlist.get(i).getNickname(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("personIdNum", String.valueOf(sellerlist.get(i).getPersonIdNum()), Field.Store.YES, Field.Index.ANALYZED)); document.add(/*from w w w.j a va 2s. c o m*/ new Field("email", sellerlist.get(i).getEmail(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("password", sellerlist.get(i).getPassword(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("status", String.valueOf(sellerlist.get(i).getStatus()), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("timeCreated", String.valueOf(sellerlist.get(i).getTimeCreated()), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(document); } long time2 = System.currentTimeMillis(); System.out.println("" + writer.numDocs() + ""); System.out.println("" + (time2 - time1) + ""); } writer.close(); }