@Override public void modifyIndex(final IndexWriter writer, final IndexSearcher searcher) throws ModifyKnowledgeBaseException { for (final Map.Entry<String, HashMap<String, String>> entry : this.attributes.entrySet()) { final String key = entry.getKey(); final HashMap<String, String> hash = entry.getValue(); final QueryParser qp = new QueryParser(this.docPrimaryKey, new DoserIDAnalyzer()); try { final TopDocs top = searcher.search(qp.parse(QueryParserBase.escape(key)), 1); final ScoreDoc[] scores = top.scoreDocs; if (scores.length > 0) { final Document doc = new Document(); final Document currentDoc = searcher.getIndexReader().document(scores[0].doc); // BugFix create new Document und copy Fields. final List<IndexableField> fields = currentDoc.getFields(); for (final IndexableField field : fields) { if (field.stringValue() != null) { if (field.name().equalsIgnoreCase(docPrimaryKey)) { doc.add(new StringField(field.name(), field.stringValue(), Field.Store.YES)); } else { doc.add(new TextField(field.name(), field.stringValue(), Field.Store.YES)); } } } final List<Document> docListToAdd = new LinkedList<Document>(); docListToAdd.add(doc); for (final Map.Entry<String, String> subentry : hash.entrySet()) { final IndexableField field = doc.getField(subentry.getKey()); if (field == null) { throw new ModifyKnowledgeBaseException("UpdateField no found", null); } if (this.action.equals(KBModifications.OVERRIDEFIELD)) { doc.removeFields(subentry.getKey()); String[] newentries = generateSeperatedFieldStrings(subentry.getValue()); for (int i = 0; i < newentries.length; i++) { doc.add(new TextField(subentry.getKey(), newentries[i], Field.Store.YES)); } } else if (this.action.equals(KBModifications.UPDATERELATEDLABELS)) { doc.removeFields(subentry.getKey()); doc.add(updateOccurrences(subentry.getValue(), field, "surroundinglabels")); } else if (this.action.equals(KBModifications.UPDATEOCCURRENCES)) { doc.removeFields(subentry.getKey()); IndexableField f = updateOccurrences(subentry.getValue(), field, "occurrences"); doc.add(f); } } writer.updateDocuments(new Term(this.docPrimaryKey, key), docListToAdd); } else { throw new ModifyKnowledgeBaseException("Document not found", null); } } catch (final IOException e) { throw new ModifyKnowledgeBaseException("IOException in IndexSearcher", e); } catch (ParseException e) { throw new ModifyKnowledgeBaseException("Queryparser Exception", e); } } }
private static boolean update(Index index, VersionValue versionValue, IndexWriter indexWriter) throws IOException { boolean created; if (versionValue != null) { created = versionValue.delete(); // we have a delete which is not GC'ed... } else { created = false; } if (index.docs().size() > 1) { indexWriter.updateDocuments(index.uid(), index.docs()); } else { indexWriter.updateDocument(index.uid(), index.docs().get(0)); } return created; }
public void updateDocuments(Term term, List<Document> documents, String collectionName) { IndexWriter indexWriter = indexWriterManager.getIndexWriter(collectionName); try { logger.info("collectionName : {}", collectionName); logger.info("update indexing start................{}, size : {}", term, documents.size()); indexWriter.updateDocuments(term, documents); logger.info("end"); } catch (IOException e) { logger.error("error : ", e); throw new RuntimeException("색인 중 에러가 발생하였습니다. [" + e.getMessage() + "]"); } }
/** * Updates a document. * * @see IndexWriter#updateDocument(Term, org.apache.lucene.index.IndexDocument) */ public <T extends IndexableField> void updateDocument(Term t, final IndexDocument doc) throws IOException { if (r.nextInt(5) == 3) { w.updateDocuments( t, new Iterable<IndexDocument>() { @Override public Iterator<IndexDocument> iterator() { return new Iterator<IndexDocument>() { boolean done; @Override public boolean hasNext() { return !done; } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public IndexDocument next() { if (done) { throw new IllegalStateException(); } done = true; return doc; } }; } }); } else { w.updateDocument(t, doc); } maybeCommit(); }
public void updateDocuments(Term delTerm, Iterable<? extends IndexDocument> docs) throws IOException { w.updateDocuments(delTerm, docs); maybeCommit(); }