List of usage examples for org.apache.lucene.index IndexWriter deleteDocuments
public long deleteDocuments(Query... queries) throws IOException
From source file:psidev.psi.mi.search.index.impl.InteractorIndexWriter.java
License:Apache License
private void indexBinaryInteraction(IndexWriter indexWriter, SearchEngine searchEngine, BinaryInteraction binaryInteraction, ColumnBasedDocumentDefinition docDefinition) throws IOException, MitabLineException { final String idAColumnName = docDefinition.getColumnByPosition(0).getKey(); final String identifier = getMainIdentifier(binaryInteraction.getInteractorA()); SearchResult<BinaryInteraction> result = searchEngine.search(idAColumnName + ":" + identifier.toLowerCase(), null, null);/*w w w . j av a2s .c o m*/ final BinaryInteraction interactionToIndex; boolean disableCVexpansion = false; if (result.getTotalCount() == 1) { // merge lines BinaryInteraction existingInteraction = result.getData().iterator().next(); interactionToIndex = mergeBinaryInteractions(existingInteraction, binaryInteraction); if (log.isDebugEnabled()) log.debug("Deleting existing document for interactor: " + identifier); indexWriter .deleteDocuments(searchEngine.createQueryFor(idAColumnName + ":" + identifier.toLowerCase())); indexWriter.commit(); disableCVexpansion = true; } else if (result.getTotalCount() > 1) { StringBuilder sb = new StringBuilder(1024); final String newLine = System.getProperty("line.separator"); for (BinaryInteraction bi : result.getData()) { sb.append(MitabWriterUtils.buildLine(bi, PsimiTabVersion.v2_5)); } throw new IllegalStateException( "More than one document existing for identifier A: " + identifier + newLine + sb.toString()); } else { interactionToIndex = binaryInteraction; } if (log.isDebugEnabled()) log.debug("Adding document for interactor: " + identifier); getDocumentBuilder().setDisableExpandInteractorsProperties(disableCVexpansion); indexWriter.addDocument(getDocumentBuilder().createDocument(interactionToIndex)); indexWriter.commit(); }
From source file:Search.TranscriptionIndexer.java
License:Educational Community License
/** * Update the text/comment/image positioning of a transcription that has previusly been indexed * @param t the transcription that was updated * @throws IOException/* w w w .ja v a 2 s. c o m*/ * @throws SQLException */ public static void update(Transcription t) throws IOException, SQLException { IndexWriter writer = null; Analyzer analyser; Directory directory; /**@TODO parameterize this location*/ String dest = "/usr/indexTranscriptions"; directory = FSDirectory.getDirectory(dest, true); analyser = new StandardAnalyzer(); writer = new IndexWriter(directory, analyser, true); Term j = new Term("id", "" + t.getLineID()); writer.deleteDocuments(j); Document doc = new Document(); Field field; field = new Field("text", t.getText(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("comment", t.getComment(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("creator", "" + t.UID, Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("security", "" + "private", Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("line", "" + t.getLine(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("page", "" + t.getFolio(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("id", "" + t.getLineID(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("manuscript", "" + new Manuscript(t.getFolio()).getID(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); field = new Field("projectID", "" + t.getProjectID(), Field.Store.YES, Field.Index.ANALYZED); doc.add(field); writer.addDocument(doc); writer.commit(); writer.close(); }
From source file:shnakkydoodle.searching.provider.Lucene.java
License:Open Source License
/** * Delete data//from ww w .ja v a 2s . c o m * * @param : * key */ @Override public synchronized void deleteData(String searchQuery) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexDirectory, new IndexWriterConfig(analyzer)); indexWriter.deleteDocuments(new QueryParser("", analyzer).parse(searchQuery)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { indexWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:uk.ac.ebi.arrayexpress.utils.saxon.search.AbstractIndexEnvironment.java
License:Apache License
public void indexIncrementalFromXmlDB(String indexLocationDirectory, String dbHost, int dbPort, String dbPassword, String dbName) throws Exception { // I'm upgrading so the baseline is the current nodes number int countNodes = getCountDocuments(); String driverXml = ""; String connectionString = ""; Collection coll;/*from w w w. jav a2 s . com*/ IndexWriter w = null; Map<String, XPathExpression> fieldXpe = new HashMap<String, XPathExpression>(); logger.info("indexIncrementalFromXmlDB(generic) is starting for [{}], and initially I have[{}] ... ", new Object[] { indexId, countNodes }); try { Directory indexTempDirectory = FSDirectory.open(new File(indexLocationDirectory, indexId)); w = openIndex(indexTempDirectory, indexAnalyzer); HierarchicalConfiguration connsConf = (HierarchicalConfiguration) Application.getInstance() .getPreferences().getConfSubset("bs.xmldatabase"); if (null != connsConf) { driverXml = connsConf.getString("driver"); connectionString = connsConf.getString("base") + "://" + dbHost + ":" + dbPort + "/" + dbName; } else { logger.error("bs.xmldatabase Configuration is missing!!"); } logger.debug("connectionString->" + connectionString); coll = DatabaseManager.getCollection(connectionString); XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); DocumentInfo source = null; Configuration config = ((SaxonEngine) Application.getAppComponent("SaxonEngine")).trFactory .getConfiguration(); XPath xp = new XPathEvaluator(config); for (FieldInfo field : fields.values()) { fieldXpe.put(field.name, xp.compile(field.path)); logger.debug("Field Path->[{}]", field.path); } // the xmldatabase is not very correct and have memory problem for // queires with huge results, so its necessary to implement our own // iteration mechanism // // // I will collect all the results // ResourceSet set = service.query(this.env.indexDocumentPath); long numberResults = 0; ResourceSet set = service.query("count(" + indexDocumentPath + ")"); if (set.getIterator().hasMoreResources()) { numberResults = Integer.parseInt((String) set.getIterator().nextResource().getContent()); } // TODO:######################################Change this after - // this is just a performance test // float percentage=0.1F; // numberResults=Math.round(numberResults * percentage); logger.debug("Number of results->" + numberResults); long pageSizeDefault = 50000; if (numberResults > 1000000) { pageSizeDefault = 1000000; } long pageNumber = 1; int count = 0; Map<String, AttsInfo[]> cacheAtt = new HashMap<String, AttsInfo[]>(); Map<String, XPathExpression> cacheXpathAtt = new HashMap<String, XPathExpression>(); Map<String, XPathExpression> cacheXpathAttValue = new HashMap<String, XPathExpression>(); while ((pageNumber * pageSizeDefault) <= (numberResults + pageSizeDefault - 1)) { // calculate the last hit long pageInit = (pageNumber - 1) * pageSizeDefault + 1; long pageSize = (pageNumber * pageSizeDefault < numberResults) ? pageSizeDefault : (numberResults - pageInit + 1); service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); // xquery paging using subsequence function long time = System.nanoTime(); // TODO: I'm assuming that there is always an attribute @id in // each element set = service.query("for $x in(subsequence(" + indexDocumentPath + "/@id," + pageInit + "," + pageSize + ")) return string($x)"); double ms = (System.nanoTime() - time) / 1000000d; logger.info("Query XMLDB took ->[{}]", ms); ResourceIterator iter = set.getIterator(); XPath xp2; XPathExpression xpe2; List documentNodes; StringReader reader; // cache of distinct attributes fora each sample group while (iter.hasMoreResources()) { count++; logger.debug("its beeing processed the number ->" + count); String idToProcess = (String) iter.nextResource().getContent(); logger.debug("@id that is being processed->" + idToProcess); // I need to get the sample ResourceSet setid = service.query(indexDocumentPath + "[@id='" + idToProcess + "']"); ResourceIterator iterid = setid.getIterator(); while (iterid.hasMoreResources()) { StringBuilder xml = new StringBuilder(); xml.append((String) iterid.nextResource().getContent()); // logger.debug(xml.toString()); reader = new StringReader(xml.toString()); source = config.buildDocument(new StreamSource(reader)); // logger.debug("XML DB->[{}]", // PrintUtils.printNodeInfo((NodeInfo) source, config)); Document d = new Document(); xp2 = new XPathEvaluator(source.getConfiguration()); int position = indexDocumentPath.lastIndexOf("/"); // TODO: I also need to change this String pathRoot = ""; if (position != -1) { pathRoot = indexDocumentPath.substring(position); } else { pathRoot = indexDocumentPath; } // logger.debug("PathRoot->[{}]",pathRoot); xpe2 = xp2.compile(pathRoot); documentNodes = (List) xpe2.evaluate(source, XPathConstants.NODESET); for (Object node : documentNodes) { // logger.debug("XML DB->[{}]",PrintUtils.printNodeInfo((NodeInfo)node,config)); String idElement = (String) fieldXpe.get("id").evaluate(node, XPathConstants.STRING); // I need to see if it already exists // I will also add this document if it is nor marked // as "todelete" Boolean toDelete = (Boolean) fieldXpe.get("delete").evaluate(node, XPathConstants.BOOLEAN); // TODO:######################################Change // this after - this is just a performance test int deletePercentage = 10; toDelete = (count % deletePercentage) == 0 ? true : false; logger.debug( "Incremental Update - The document [{}] is being processed and is marked to delete?[{}]", new Object[] { idElement, toDelete }); // I will always try to delete the document (i don't // know if it is new or if it was changed) Term idTerm = new Term("id", idElement.toLowerCase()); int countToDelete = getIndexReader().docFreq(idTerm); if (countToDelete > 0) { // if has more than one, I have to send an email // to warn if (countToDelete > 1) { Application.getInstance().sendEmail(null, null, "BIOSAMPLES ERROR - Incremental Update - Removing more than one document! id-> " + idElement, " documents found:" + countToDelete); // I will launch an exception throw new Exception( "BIOSAMPLES ERROR - Incremental Update - Removing more than one document in incremental update id-> " + idElement + " documents found:" + countToDelete); } logger.debug("The document with id [{}] is being deleted from Lucene", idElement); w.deleteDocuments(idTerm); // need to remove one from the number of // documents count countNodes--; } // the element doesn't exist on GUI else { // if it is marked to delete I will just an // warning email - it's possible that it was // inserted and deleted on the Backend but it // had never been sent to the GUI before if (toDelete) { Application.getInstance().sendEmail(null, null, "BIOSAMPLES WARNING - Incremental Update - Id marked for deletion but the id doesn't exist on the GUI! id-> " + idElement, ""); } } // if (toDelete) { // logger.debug( // "The document with id [{}] was marked to deletion so I will not process it", // idElement); // } else { // I just process it is it is not for deletion) if (!toDelete) { try { d = processEntryIndex(node, config, service, fieldXpe); } catch (Exception x) { String xmlError = PrintUtils.printNodeInfo((NodeInfo) node, config); logger.error("XML that was being processed when the error occurred DB->[{}]", xmlError); // to avoid the next running to stop // because its not able to delete the // newSetup directory w.close(); throw new Exception("Xml that is being processed:" + xmlError, x); } countNodes++; addIndexDocument(w, d); } } // } documentNodes = null; source = null; reader = null; xml = null; // logger.debug("count->[{}]", countNodes); } } logger.debug("until now it were processed->[{}]", pageNumber * pageSizeDefault); pageNumber++; // if (coll != null) { // try { // // coll.close(); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } set = null; } setCountDocuments(countNodes); // add metadata to the lucene index Map<String, String> map = new HashMap<String, String>(); map.put("numberDocs", Integer.toString(countNodes)); map.put("date", Long.toString(System.nanoTime())); // logger.debug(Application.getInstance().getComponent("XmlDbConnectionPool").getMetaDataInformation()); // I cannot call directly // getComponent("XmlDbConnectionPool").getMetaDataInformation(), // because I can be working in a did String dbInfo = ((XmlDbConnectionPool) Application.getInstance().getComponent("XmlDbConnectionPool")) .getDBInfo(dbHost, dbPort, dbPassword, dbName); // TODO: I need to put here what I have before - to track all the // changes (old numberDocs + old date + oldDBInfo) map.put("DBInfo", dbInfo + "<BR>##################################################<BR>" + getMetadataInformation()); commitIndex(w, map); } catch (Exception x) { logger.error("Caught an exception:", x); w.close(); throw x; } }
From source file:uk.ac.ebi.fg.biostudies.utils.saxon.search.AbstractIndexEnvironment.java
License:Apache License
public void indexIncrementalFromXmlDB(String indexLocationDirectory, String dbHost, int dbPort, String dbPassword, String dbName) throws Exception { // I'm upgrading so the baseline is the current nodes number int countNodes = getCountDocuments(); String driverXml = ""; String connectionString = ""; Collection coll;//from w w w. j a va 2 s.c o m IndexWriter w = null; Map<String, XPathExpression> fieldXpe = new HashMap<String, XPathExpression>(); logger.info("indexIncrementalFromXmlDB(generic) is starting for [{}], and initially I have[{}] ... ", new Object[] { indexId, countNodes }); try { Directory indexTempDirectory = FSDirectory.open(new File(indexLocationDirectory, indexId)); w = openIndex(indexTempDirectory, indexAnalyzer); HierarchicalConfiguration connsConf = (HierarchicalConfiguration) Application.getInstance() .getPreferences().getConfSubset("bs.xmldatabase"); if (null != connsConf) { driverXml = connsConf.getString("driver"); connectionString = connsConf.getString("base") + "://" + dbHost + ":" + dbPort + "/" + dbName; } else { logger.error("bs.xmldatabase Configuration is missing!!"); } logger.debug("connectionString->" + connectionString); coll = DatabaseManager.getCollection(connectionString); XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); DocumentInfo source = null; Configuration config = ((SaxonEngine) Application.getAppComponent("SaxonEngine")).trFactory .getConfiguration(); XPath xp = new XPathEvaluator(config); for (FieldInfo field : fields.values()) { fieldXpe.put(field.name, xp.compile(field.path)); logger.debug("Field Path->[{}]", field.path); } // the xmldatabase is not very correct and have memory problem for // queires with huge results, so its necessary to implement our own // iteration mechanism // // // I will collect all the results // ResourceSet set = service.query(this.env.indexDocumentPath); long numberResults = 0; ResourceSet set = service.query("count(" + indexDocumentPath + ")"); if (set.getIterator().hasMoreResources()) { numberResults = Integer.parseInt((String) set.getIterator().nextResource().getContent()); } // TODO:######################################Change this after - // this is just a performance test // float percentage=0.1F; // numberResults=Math.round(numberResults * percentage); logger.debug("Number of results->" + numberResults); long pageSizeDefault = 50000; if (numberResults > 1000000) { pageSizeDefault = 1000000; } long pageNumber = 1; int count = 0; Map<String, AttsInfo[]> cacheAtt = new HashMap<String, AttsInfo[]>(); Map<String, XPathExpression> cacheXpathAtt = new HashMap<String, XPathExpression>(); Map<String, XPathExpression> cacheXpathAttValue = new HashMap<String, XPathExpression>(); while ((pageNumber * pageSizeDefault) <= (numberResults + pageSizeDefault - 1)) { // calculate the last hit long pageInit = (pageNumber - 1) * pageSizeDefault + 1; long pageSize = (pageNumber * pageSizeDefault < numberResults) ? pageSizeDefault : (numberResults - pageInit + 1); service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); // xquery paging using subsequence function long time = System.nanoTime(); // TODO: I'm assuming that there is always an attribute @id in // each element set = service.query("for $x in(subsequence(" + indexDocumentPath + "/@id," + pageInit + "," + pageSize + ")) return string($x)"); double ms = (System.nanoTime() - time) / 1000000d; logger.info("Query XMLDB took ->[{}]", ms); ResourceIterator iter = set.getIterator(); XPath xp2; XPathExpression xpe2; List documentNodes; StringReader reader; // cache of distinct attributes fora each sample group while (iter.hasMoreResources()) { count++; logger.debug("its beeing processed the number ->" + count); String idToProcess = (String) iter.nextResource().getContent(); logger.debug("@id that is being processed->" + idToProcess); // I need to get the sample ResourceSet setid = service.query(indexDocumentPath + "[@id='" + idToProcess + "']"); ResourceIterator iterid = setid.getIterator(); while (iterid.hasMoreResources()) { StringBuilder xml = new StringBuilder(); xml.append((String) iterid.nextResource().getContent()); // logger.debug(xml.toString()); reader = new StringReader(xml.toString()); source = config.buildDocument(new StreamSource(reader)); // logger.debug("XML DB->[{}]", // PrintUtils.printNodeInfo((NodeInfo) source, config)); Document d = new Document(); xp2 = new XPathEvaluator(source.getConfiguration()); int position = indexDocumentPath.lastIndexOf("/"); // TODO: I also need to change this String pathRoot = ""; if (position != -1) { pathRoot = indexDocumentPath.substring(position); } else { pathRoot = indexDocumentPath; } // logger.debug("PathRoot->[{}]",pathRoot); xpe2 = xp2.compile(pathRoot); documentNodes = (List) xpe2.evaluate(source, XPathConstants.NODESET); for (Object node : documentNodes) { // logger.debug("XML DB->[{}]",PrintUtils.printNodeInfo((NodeInfo)node,config)); String idElement = (String) fieldXpe.get("id").evaluate(node, XPathConstants.STRING); // I need to see if it already exists // I will also add this document if it is nor marked // as "todelete" Boolean toDelete = (Boolean) fieldXpe.get("delete").evaluate(node, XPathConstants.BOOLEAN); // TODO:######################################Change // this after - this is just a performance test int deletePercentage = 10; toDelete = (count % deletePercentage) == 0 ? true : false; logger.debug( "Incremental Update - The document [{}] is being processed and is marked to delete?[{}]", new Object[] { idElement, toDelete }); // I will always try to delete the document (i don't // know if it is new or if it was changed) Term idTerm = new Term("id", idElement.toLowerCase()); int countToDelete = getIndexReader().docFreq(idTerm); if (countToDelete > 0) { // if has more than one, I have to send an email // to warn if (countToDelete > 1) { Application.getInstance().sendEmail(null, null, "BIOSAMPLES ERROR - Incremental Update - Removing more than one document! id-> " + idElement, " documents found:" + countToDelete); // I will launch an exception throw new Exception( "BIOSAMPLES ERROR - Incremental Update - Removing more than one document in incremental update id-> " + idElement + " documents found:" + countToDelete); } logger.debug("The document with id [{}] is being deleted from Lucene", idElement); w.deleteDocuments(idTerm); // need to remove one from the number of // documents count countNodes--; } // the element doesn't exist on GUI else { // if it is marked to delete I will just an // warning email - it's possible that it was // inserted and deleted on the Backend but it // had never been sent to the GUI before if (toDelete) { Application.getInstance().sendEmail(null, null, "BIOSAMPLES WARNING - Incremental Update - Id marked for deletion but the id doesn't exist on the GUI! id-> " + idElement, ""); } } // if (toDelete) { // logger.debug( // "The document with id [{}] was marked to deletion so I will not process it", // idElement); // } else { // I just process it is it is not for deletion) if (!toDelete) { try { d = processEntryIndex(node, config, service, fieldXpe); } catch (Exception x) { String xmlError = PrintUtils.printNodeInfo((NodeInfo) node, config); logger.error("XML that was being processed when the error occurred DB->[{}]", xmlError); // to avoid the next running to stop // because its not able to delete the // newSetup directory w.close(); throw new Exception("Xml that is being processed:" + xmlError, x); } countNodes++; addIndexDocument(w, d); } } // } documentNodes = null; source = null; reader = null; xml = null; // logger.debug("count->[{}]", countNodes); } } logger.debug("until now it were processed->[{}]", pageNumber * pageSizeDefault); pageNumber++; // if (coll != null) { // try { // // coll.close(); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } set = null; } setCountDocuments(countNodes); // add metadata to the lucene index Map<String, String> map = new HashMap<String, String>(); map.put("numberDocs", Integer.toString(countNodes)); map.put("date", Long.toString(System.nanoTime())); // logger.debug(Application.getInstance().getComponent("XmlDbConnectionPool").getMetaDataInformation()); // I cannot call directly // getComponent("XmlDbConnectionPool").getMetaDataInformation(), // because I can be working in a did String dbInfo = ((XmlDbConnectionPool) Application.getInstance().getComponent("XmlDbConnectionPool")) .getDBInfo(dbHost, dbPort, dbPassword, dbName); // TODO: I need to put here what I have before - to track all the // changes (old numberDocs + old date + oldDBInfo) map.put("DBInfo", dbInfo + "<BR>##################################################<BR>" + getMetadataInformation()); commitIndex(w, map); } catch (Exception x) { logger.error("Caught an exception:", x); w.close(); throw x; } }
From source file:uk.ac.ebi.mdk.service.loader.single.MoleculeCollectionConnectivityLoader.java
License:Open Source License
public void deleteCollection() throws IOException { Query queryCollection = new TermQuery(new Term( MoleculeCollectionConnectivityLuceneFields.CollectionName.toString(), this.collectionName)); Directory indexDirectory = getIndex().getDirectory(); IndexWriter writer = new IndexWriter(indexDirectory, new IndexWriterConfig(Version.LUCENE_34, getIndex().getAnalyzer())); writer.deleteDocuments(queryCollection); writer.close();//www .j a v a 2 s . c o m indexDirectory.close(); }
From source file:uk.ac.open.kmi.squire.index.RDFDatasetIndexer.java
public Document indexSignature(String urlAddress, String graphName, IRDFDataset indexand, Collection<String> propertySet, boolean overwrite) { if (alreadyIndexed(urlAddress, graphName) && !overwrite) { log.warn("Already indexed: {}{}", urlAddress, graphName == null ? "" : "::" + graphName); log.warn(" ... overwrite not set, so not indexing."); return null; }//from w ww . j a v a 2 s . c om Analyzer analyzer = new StandardAnalyzer(); // = new WhitespaceAnalyzer(); IndexWriter indexWriter; /* * IndexWriterConfig.OpenMode.CREATE_OR_APPEND if used IndexWriter will create a * new index if there is not already an index at the provided path and otherwise * open the existing index. */ IndexWriterConfig config = new IndexWriterConfig(analyzer);// .setOpenMode(OpenMode.CREATE_OR_APPEND); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); try { indexWriter = new IndexWriter(getIndex(), config); } catch (IOException e) { throw new RuntimeException(e); } // For every dataset a document Document doc = new Document(); // XXX AA I think the values are so because it is assumed that Set#toString() // prints [ one, two, ... ] but can it be trusted? doc.add(new Field("URL", urlAddress, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("GraphName", graphName, Field.Store.YES, Field.Index.NOT_ANALYZED)); // doc.add(new Field("ClassSet", indexand.getClassSet().toString(), // Field.Store.YES, Field.Index.NO)); doc.add(new Field("ObjectPropertySet", indexand.getObjectPropertySet().toString(), Field.Store.YES, Field.Index.NO)); doc.add(new Field("DatatypePropertySet", indexand.getDatatypePropertySet().toString(), Field.Store.YES, Field.Index.NO)); doc.add(new Field("LiteralSet", indexand.getLiteralSet().toString(), Field.Store.YES, Field.Index.NO)); doc.add(new Field("IndividualSet", indexand.getIndividualSet().toString(), Field.Store.YES, Field.Index.NO)); doc.add(new Field("RDFVocabulary", indexand.getRDFVocabulary().toString(), Field.Store.YES, Field.Index.NO)); if (propertySet != null && !propertySet.isEmpty()) doc.add(new Field("PropertySet", propertySet.toString(), Field.Store.YES, Field.Index.NO)); // TODO the new way of using Lucene, apply to rest doc.add(new StoredField(Fieldd.ClassSet.toString(), indexand.getClassSet().toString())); JsonObject jSign = new JsonObject(); for (Entry<String, ClassSignature> entry : indexand.getClassSignatures().entrySet()) jSign.put(entry.getKey(), entry.getValue().jsonifyPaths()); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); JSON.write(os, jSign); doc.add(new StoredField(Fieldd.CLASS_SIGNATURES.toString(), new String(os.toByteArray(), "UTF-8"))); } catch (UnsupportedEncodingException e1) { shutdown(indexWriter); throw new RuntimeException("UTF-8 not supported. Seriously?", e1); } // Remove the old one(s) if any Builder queryBuilder = new Builder(); queryBuilder.add(new TermQuery(new Term("URL", urlAddress)), BooleanClause.Occur.MUST); if (graphName != null && !graphName.isEmpty()) queryBuilder.add(new TermQuery(new Term("GraphName", graphName)), BooleanClause.Occur.MUST); try { indexWriter.deleteDocuments(queryBuilder.build()); indexWriter.addDocument(doc); } catch (IOException e) { throw new RuntimeException(e); } finally { shutdown(indexWriter); } return doc; }
From source file:uk.gov.nationalarchives.discovery.taxonomy.common.repository.lucene.TrainingSetRepository.java
License:Mozilla Public License
public void deleteTrainingDocumentsForCategory(IndexWriter writer, Category category) { try {//from ww w . j a va 2s . c om logger.info(".deleteAndUpdateTraingSetIndexForCategory: removed elements for category: {}", category.getTtl()); writer.deleteDocuments(new Term(InformationAssetViewFields.TAXONOMY.toString(), category.getTtl())); } catch (IOException e) { throw new TaxonomyException(TaxonomyErrorType.LUCENE_IO_EXCEPTION, e); } }