[opencms-dev] To build/remove lucene index whenever publishing in opencms 5.0.1(opencms-lucene module 1.5 required).

??? shiys at langhua.cn
Sun Sep 26 19:10:37 CEST 2004


hi there,

To  build/remove lucene index whenever publishing, i added the following code in CmsObject.java of opencms 5.0.1(opencms-lucene module 1.5 required):

publishProject(int id, I_CmsReport report)
......
    }finally{
        if(changedResources == null || changedResources.size()<1){
            String stamp1 = "[" + this.getClass().getName() + ".publishProject()/2] Project:" + id + " Time:" + new Date();
            String stamp2 = "[" + this.getClass().getName() + ".publishProject()/2] User: " + m_context.currentUser().toString();
            String stamp3 = "[" + this.getClass().getName() + ".publishProject()/2] Vector was null or empty";
            if (DEBUG > 0) {
                System.err.println("###################################");
                System.err.println(stamp1);
                System.err.println(stamp2);
                System.err.println(stamp3);
            }
            if (I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_INFO)) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, stamp1);
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, stamp2);
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, stamp3);
            }
            success = false;
        } else {
	// update the lucene index
	// modified by Shi Yusen, shiys at langhua.cn
        	String filename = null;
        	for (int i=0; i<changedResources.size(); i++) {
        		filename = changedResources.get(i).toString().replaceFirst("/default/vfs/", "/");
				try {
					// add the file to the lucene index
					IndexManager im = null;
					im = new IndexManager(this);
					im.updateIndexForFile(this.readFile(filename));
				} catch (CmsException e) {
					// delete the file from the lucene index if the file cannot be read
					SearchParameters sParams = null;
					IndexReader reader = null;
					try {
						sParams = new SearchParameters(this);
						reader = IndexReader.open(sParams.getIndexPath());
						int j = 0;
						j = reader.delete(new Term(I_DocumentConstants.FIELD_PATH, filename));
					} catch (IOException ioe) {
						throw new CmsException("Could not open Index.");
					} finally {
						if (reader!=null) {
							try {
								reader.close();
							} catch (IOException ignore) {
							}
						}
					}
				}
        	}
        }
......

Note: 
	Tested in OpenCMS 5.0.1 and opencms-lucene module is required.
	The index path must be defined in opencms's registry.xml.
	It works whenever resource(s) is(are) created, modified or removed in opencms.

Regards,

Shi Yusen/Langhua


More information about the opencms-dev mailing list