List of usage examples for org.apache.lucene.index IndexWriter updateDocument
private long updateDocument(final DocumentsWriterDeleteQueue.Node<?> delNode, Iterable<? extends IndexableField> doc) throws IOException
From source file:cn.fql.blogspider.IndexMain.java
License:Open Source License
static void indexDocs(IndexWriter writer, File file) throws IOException { if (file.canRead()) if (file.isDirectory()) { String[] files = file.list(); if (files != null) for (int i = 0; i < files.length; ++i) indexDocs(writer, new File(file, files[i])); } else {//from www. j av a 2s . c om FileInputStream fis; try { fis = new FileInputStream(file); } catch (FileNotFoundException fnfe) { return; } try { Document doc = new Document(); Field pathField = new StringField("path", file.getPath(), Field.Store.YES); doc.add(pathField); doc.add(new LongField("modified", file.lastModified(), Field.Store.YES)); doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8")))); if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) { System.out.println("adding " + file); writer.addDocument(doc); } else { System.out.println("updating " + file); writer.updateDocument(new Term("path", file.getPath()), doc); } } finally { fis.close(); } } }
From source file:cn.larry.search.book.index.IndexFiles.java
License:Apache License
/** Indexes a single document */ static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException { try (InputStream stream = Files.newInputStream(file)) { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a // field that is indexed (i.e. searchable), but don't tokenize // the field into separate words and don't index term frequency // or positional information: Field pathField = new StringField("path", file.toString(), Field.Store.YES); doc.add(pathField);//w w w . j a va 2 s. co m // Add the last modified date of the file a field named "modified". // Use a LongPoint that is indexed (i.e. efficiently filterable with // PointRangeQuery). This indexes to milli-second resolution, which // is often too fine. You could instead create a number based on // year/month/day/hour/minutes/seconds, down the resolution you require. // For example the long value 4 would mean // February 17, 1, 2-3 PM. doc.add(new LongPoint("modified", lastModified)); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in UTF-8 encoding. // If that's not the case searching for special characters will fail. doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)))); if (writer.getConfig().getOpenMode() == OpenMode.CREATE) { // New index, so we just add the document (no old document can be there): System.out.println("adding " + file); writer.addDocument(doc); } else { // Existing index (an old copy of this document may have been indexed) so // we use updateDocument instead to replace the old one matching the exact // path, if present: System.out.println("updating " + file); writer.updateDocument(new Term("path", file.toString()), doc); } } }
From source file:com.barchart.feed.ddf.resolver.provider.CodecHelper.java
License:BSD License
/** re index instrument in lucene store */ static void update(final IndexWriter writer, final Instrument entry) throws Exception { final Term key = getKeyTerm(entry); final Document doc = instrumentEncode(entry); writer.updateDocument(key, doc); }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
void setStatus(final Status status) throws Exception { final Document doc = Status.encode(status); final IndexWriterConfig config = new IndexWriterConfig(ConstResolver.VERSION, analyzer); final IndexWriter writer = new IndexWriter(getDirectory(), config); writer.updateDocument(Status.TERM, doc); writer.close();//from w w w. j av a 2s . c om }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
protected void updateEntity(SEntity src) { if (src.getId().length() == 0) return;// www. j a v a 2s .c o m 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.bugull.mongo.lucene.backend.IndexUpdateJob.java
License:Apache License
@Override public void doJob() { Class<?> clazz = obj.getClass(); String name = MapperUtil.getEntityName(clazz); IndexWriterHolder holder = IndexWriterHolder.getInstance(); IndexWriter writer = holder.get(name); Document doc = new Document(); IndexCreator creator = new IndexCreator(obj, ""); creator.create(doc);//from w w w .ja v a 2 s . c o m Term term = new Term(FieldsCache.getInstance().getIdFieldName(clazz), obj.getId()); try { writer.updateDocument(term, doc); } catch (CorruptIndexException ex) { logger.error("IndexWriter can not update the document", ex); } catch (IOException ex) { logger.error("IndexWriter can not update the document", ex); } }
From source file:com.bugull.mongo.lucene.backend.IndexUpdateTask.java
License:Apache License
@Override public void run() { Class<?> clazz = obj.getClass(); String name = MapperUtil.getEntityName(clazz); IndexWriterCache cache = IndexWriterCache.getInstance(); IndexWriter writer = cache.get(name); Document doc = new Document(); IndexCreator creator = new IndexCreator(obj, ""); creator.create(doc);//from w w w.ja va 2 s . c om Term term = new Term(FieldsCache.getInstance().getIdFieldName(clazz), obj.getId()); try { writer.updateDocument(term, doc); } catch (CorruptIndexException ex) { logger.error("IndexWriter can not update the document", ex); } catch (IOException ex) { logger.error("IndexWriter can not update the document", ex); } }
From source file:com.cep.darkstar.onramp.djnews.IndexFiles.java
License:Apache License
/** * Indexes the given file using the given writer, or if a directory is given, * recurses over files and directories found under the given directory. * //from ww w .jav a 2s . c om * NOTE: This method indexes one document per input file. This is slow. For good * throughput, put multiple documents into your input file(s). An example of this is * in the benchmark module, which can create "line doc" files, one document per line, * using the * <a href="../../../../../contrib-benchmark/org/apache/lucene/benchmark/byTask/tasks/WriteLineDocTask.html" * >WriteLineDocTask</a>. * * @param writer Writer to the index where the given file/dir info will be stored * @param file The file to index, or the directory to recurse into to find files to index * @throws IOException */ static void indexDocs(IndexWriter writer, File file) throws IOException { // do not try to index files that cannot be read if (file.canRead()) { if (file.isDirectory()) { String[] files = file.list(); // an IO error could occur if (files != null) { for (int i = 0; i < files.length; i++) { indexDocs(writer, new File(file, files[i])); } } } else { FileInputStream fis; try { fis = new FileInputStream(file); } catch (FileNotFoundException fnfe) { // at least on windows, some temporary files raise this exception with an "access denied" message // checking if the file can be read doesn't help return; } try { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a // field that is indexed (i.e. searchable), but don't tokenize // the field into separate words and don't index term frequency // or positional information: Field pathField = new Field("path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS); pathField.setOmitTermFreqAndPositions(true); doc.add(pathField); // Add the last modified date of the file a field named "modified". // Use a NumericField that is indexed (i.e. efficiently filterable with // NumericRangeFilter). This indexes to milli-second resolution, which // is often too fine. You could instead create a number based on // year/month/day/hour/minutes/seconds, down the resolution you require. // For example the long value 2011021714 would mean // February 17, 2011, 2-3 PM. NumericField modifiedField = new NumericField("modified"); modifiedField.setLongValue(file.lastModified()); doc.add(modifiedField); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in UTF-8 encoding. // If that's not the case searching for special characters will fail. doc.add(new Field("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8")))); if (writer.getConfig().getOpenMode() == OpenMode.CREATE) { // New index, so we just add the document (no old document can be there): System.out.println("adding " + file); writer.addDocument(doc); } else { // Existing index (an old copy of this document may have been indexed) so // we use updateDocument instead to replace the old one matching the exact // path, if present: System.out.println("updating " + file); writer.updateDocument(new Term("path", file.getPath()), doc); } } finally { fis.close(); } } } }
From source file:com.chenyi.langeasy.lucene.IndexFiles.java
License:Apache License
/** Indexes a single document */ static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException { try (InputStream stream = Files.newInputStream(file)) { count++;/*from w w w . j av a 2s.c om*/ if (count % 500 == 499) { System.out.println(count + "/" + new Date()); } // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a // field that is indexed (i.e. searchable), but don't tokenize // the field into separate words and don't index term frequency // or positional information: Field pathField = new StringField("path", file.toString(), Field.Store.YES); doc.add(pathField); // Add the last modified date of the file a field named "modified". // Use a LongField that is indexed (i.e. efficiently filterable with // NumericRangeFilter). This indexes to milli-second resolution, // which // is often too fine. You could instead create a number based on // year/month/day/hour/minutes/seconds, down the resolution you // require. // For example the long value 2011021714 would mean // February 17, 2011, 2-3 PM. doc.add(new LongField("modified", lastModified, Field.Store.NO)); // Add the contents of the file to a field named "contents". Specify // a Reader, // so that the text of the file is tokenized and indexed, but not // stored. // Note that FileReader expects the file to be in UTF-8 encoding. // If that's not the case searching for special characters will // fail. doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)))); if (writer.getConfig().getOpenMode() == OpenMode.CREATE) { // New index, so we just add the document (no old document can // be there): System.out.println("adding " + file); writer.addDocument(doc); } else { // Existing index (an old copy of this document may have been // indexed) so // we use updateDocument instead to replace the old one matching // the exact // path, if present: System.out.println("updating " + file); writer.updateDocument(new Term("path", file.toString()), doc); } } }
From source file:com.codenvy.test.lucene.DeleteFilesWithSameName.java
License:Open Source License
private static void indexDocs(IndexWriter writer, Path file) throws IOException { try (InputStream stream = Files.newInputStream(file)) { Document doc = new Document(); System.out.println("file path " + file.toAbsolutePath().toString()); Field pathField = new StringField(PATH, file.toAbsolutePath().toString(), Field.Store.YES); doc.add(pathField);/*from w w w . j ava 2s.co m*/ doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)))); if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) { System.out.println("adding " + file); writer.addDocument(doc); } else { System.out.println("updating " + file); writer.updateDocument(new Term(PATH, file.toString()), doc); } } }