?? testcmssearchindocuments.java
字號:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearchInDocuments.java,v $
* Date : $Date: 2006/03/27 14:52:51 $
* Version: $Revision: 1.11 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.search;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.types.CmsResourceTypeBinary;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.i18n.CmsEncoder;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.search.documents.I_CmsDocumentFactory;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit test for searching in extracted document text.<p>
*
* @author Alexander Kandzior
* @version $Revision: 1.11 $
*/
public class TestCmsSearchInDocuments extends OpenCmsTestCase {
/** Name of the index used for testing. */
public static final String INDEX_OFFLINE = "Offline project (VFS)";
/** The index used for testing. */
public static final String INDEX_ONLINE = "Online project (VFS)";
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestCmsSearchInDocuments(String arg0) {
super(arg0);
}
/**
* Test suite for this test class.<p>
*
* @return the test suite
*/
public static Test suite() {
OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
TestSuite suite = new TestSuite();
suite.setName(TestCmsSearchInDocuments.class.getName());
suite.addTest(new TestCmsSearchInDocuments("testSearchIndexGeneration"));
suite.addTest(new TestCmsSearchInDocuments("testSearchInDocuments"));
suite.addTest(new TestCmsSearchInDocuments("testSearchBoost"));
suite.addTest(new TestCmsSearchInDocuments("testSearchBoostInMeta"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Tests search boosting.<p>
*
* @throws Exception if the test fails
*/
public void testSearchBoost() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing search boosting");
// perform a search on the newly generated index
CmsSearch searchBean = new CmsSearch();
List searchResult;
// count depend on the number of documents indexed
int expected = 6;
searchBean.init(cms);
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setSearchRoot("/search/");
searchBean.setQuery("Alkacon Software");
searchResult = searchBean.getSearchResult();
Iterator i = searchResult.iterator();
while (i.hasNext()) {
CmsSearchResult res = (CmsSearchResult)i.next();
System.out.print(res.getPath() + " ");
System.out.println(res.getScore());
}
assertEquals(expected, searchResult.size());
CmsSearchResult res1 = (CmsSearchResult)searchResult.get(searchResult.size() - 1);
CmsSearchResult res2 = (CmsSearchResult)searchResult.get(searchResult.size() - 2);
CmsSearchResult res3 = (CmsSearchResult)searchResult.get(0);
String path1 = cms.getRequestContext().removeSiteRoot(res1.getPath());
String path2 = cms.getRequestContext().removeSiteRoot(res2.getPath());
String path3 = cms.getRequestContext().removeSiteRoot(res3.getPath());
CmsProperty maxBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
I_CmsDocumentFactory.SEARCH_PRIORITY_MAX_VALUE,
null,
true);
CmsProperty highBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
I_CmsDocumentFactory.SEARCH_PRIORITY_HIGH_VALUE,
null,
true);
CmsProperty lowBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
I_CmsDocumentFactory.SEARCH_PRIORITY_LOW_VALUE,
null,
true);
cms.lockResource(path1);
cms.writePropertyObject(path1, maxBoost);
cms.unlockResource(path1);
cms.lockResource(path2);
cms.writePropertyObject(path2, highBoost);
cms.unlockResource(path2);
cms.lockResource(path3);
cms.writePropertyObject(path3, lowBoost);
cms.unlockResource(path3);
// update the search indexes
OpenCms.getSearchManager().rebuildAllIndexes(new CmsShellReport(cms.getRequestContext().getLocale()));
// perform the same search again in the online index - must be same result as before
searchBean.setIndex(INDEX_ONLINE);
searchBean.setQuery("Alkacon Software");
searchResult = searchBean.getSearchResult();
i = searchResult.iterator();
while (i.hasNext()) {
CmsSearchResult res = (CmsSearchResult)i.next();
System.out.print(res.getPath() + " ");
System.out.println(res.getScore() + " ");
System.out.println(res.getExcerpt());
}
assertEquals(expected, searchResult.size());
assertEquals(res1.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 1)).getPath());
assertEquals(res2.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 2)).getPath());
assertEquals(res3.getPath(), ((CmsSearchResult)searchResult.get(0)).getPath());
// now the search in the offline index - the boosted docs should now be on top
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setQuery("Alkacon Software");
searchResult = searchBean.getSearchResult();
i = searchResult.iterator();
while (i.hasNext()) {
CmsSearchResult res = (CmsSearchResult)i.next();
System.out.print(res.getPath() + " ");
System.out.println(res.getScore() + " ");
System.out.println(res.getExcerpt());
}
assertEquals(expected, searchResult.size());
// ensure boosted results are on top
assertEquals(res1.getPath(), ((CmsSearchResult)searchResult.get(0)).getPath());
assertEquals(res2.getPath(), ((CmsSearchResult)searchResult.get(1)).getPath());
// low boosted document should be on last position
assertEquals(res3.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 1)).getPath());
}
/**
* Tests search boosting when seachrching in meta information only.<p>
*
* @throws Exception if the test fails
*/
public void testSearchBoostInMeta() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing search boosting in meta information");
// perform a search on the newly generated index
CmsSearch searchBean = new CmsSearch();
List searchResult;
// count depend on the number of documents indexed
int expected = 6;
String path = "/search/";
String query = "OpenCms by Alkacon";
searchBean.init(cms);
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setSearchRoot(path);
searchBean.setQuery(query);
// ensure only meta information is searched
searchBean.setField(new String[] {I_CmsDocumentFactory.DOC_META});
searchResult = searchBean.getSearchResult();
// since no resource has any description, no results should be found
Iterator i = searchResult.iterator();
while (i.hasNext()) {
CmsSearchResult res = (CmsSearchResult)i.next();
System.out.print(res.getPath() + " ");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -