⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcmssearchindocuments.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        CmsProperty descripion = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, query, null, true);
        CmsProperty delete = new CmsProperty(
            CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
            CmsProperty.DELETE_VALUE,
            CmsProperty.DELETE_VALUE);

        List resources = cms.getFilesInFolder(path);

        Iterator i = resources.iterator();
        while (i.hasNext()) {
            CmsResource res = (CmsResource)i.next();
            String sitePath = cms.getSitePath(res);
            System.out.println(sitePath);
            cms.lockResource(sitePath);
            cms.writePropertyObject(sitePath, descripion);
            // delete potential "search.priority" setting from earlier tests
            cms.writePropertyObject(sitePath, delete);
            cms.unlockResource(sitePath);
        }
        assertEquals(expected, resources.size());

        // update the search indexes
        I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
        // this call does not throws the rebuild index event
        OpenCms.getSearchManager().rebuildAllIndexes(report);

        // perform the same search again in the online index - must be same result as before
        searchBean.setIndex(INDEX_ONLINE);
        searchBean.setQuery(query);
        searchResult = searchBean.getSearchResult();
        assertEquals(0, searchResult.size());

        // now the search in the offline index - documents should now be found
        searchBean.setIndex(INDEX_OFFLINE);
        searchBean.setQuery(query);
        List firstSearchResult = searchBean.getSearchResult();

        System.out.println("\n\n-----  Results searching 'meta' field in OFFLINE index");
        TestCmsSearch.printResults(searchResult, cms);
        assertEquals(expected, firstSearchResult.size());

        CmsSearchResult res1 = (CmsSearchResult)firstSearchResult.get(firstSearchResult.size() - 1);
        CmsSearchResult res2 = (CmsSearchResult)firstSearchResult.get(firstSearchResult.size() - 2);
        CmsSearchResult res3 = (CmsSearchResult)firstSearchResult.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,
            A_CmsVfsDocument.SEARCH_PRIORITY_MAX_VALUE,
            null,
            true);
        CmsProperty highBoost = new CmsProperty(
            CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
            A_CmsVfsDocument.SEARCH_PRIORITY_HIGH_VALUE,
            null,
            true);
        CmsProperty lowBoost = new CmsProperty(
            CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
            A_CmsVfsDocument.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
        // this call does not throws the rebuild index event
        OpenCms.getSearchManager().rebuildAllIndexes(report);

        // perform the same search again in the online index - must be same result as before
        searchBean.setIndex(INDEX_ONLINE);
        searchBean.setQuery(query);
        searchResult = searchBean.getSearchResult();
        assertEquals(0, searchResult.size());

        // just output the first seach result again, just for convenient comparison on the console
        System.out.println("\n\n-----  Results searching 'meta' field in ONLINE index (repeat)");
        TestCmsSearch.printResults(firstSearchResult, cms);

        // now the search in the offline index - the boosted docs should now be on top
        searchBean.setIndex(INDEX_OFFLINE);
        searchBean.setQuery(query);
        searchResult = searchBean.getSearchResult();
        System.out.println("\n\n-----  Results searching 'meta' field in OFFLINE index (with changes)");
        TestCmsSearch.printResults(searchResult, cms);

        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());
    }

    /**
     * Imports the documents for the test cases in the VFS an generates the index.<p>
     * 
     * Please note: This method need to be called first in this test suite, the
     * other methods depend on the index generated here.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testSearchIndexGeneration() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing search index generation with different resource types");

        // create test folder
        cms.createResource("/search/", CmsResourceTypeFolder.RESOURCE_TYPE_ID, null, null);
        cms.unlockResource("/search/");

        // import the sample documents to the VFS
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.pdf",
            "/search/test1.pdf",
            CmsResourceTypeBinary.getStaticTypeId(),
            Collections.EMPTY_LIST);
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.doc",
            "/search/test1.doc",
            CmsResourceTypeBinary.getStaticTypeId(),
            Collections.EMPTY_LIST);
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.rtf",
            "/search/test1.rtf",
            CmsResourceTypeBinary.getStaticTypeId(),
            Collections.EMPTY_LIST);
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.xls",
            "/search/test1.xls",
            CmsResourceTypeBinary.getStaticTypeId(),
            Collections.EMPTY_LIST);
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.ppt",
            "/search/test1.ppt",
            CmsResourceTypeBinary.getStaticTypeId(),
            Collections.EMPTY_LIST);

        // HTML page is encoded using UTF-8
        List properties = new ArrayList();
        properties.add(new CmsProperty(
            CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
            CmsEncoder.ENCODING_UTF_8,
            null,
            true));
        importTestResource(
            cms,
            "org/opencms/search/extractors/test1.html",
            "/search/test1.html",
            CmsResourceTypePlain.getStaticTypeId(),
            properties);

        assertTrue(cms.existsResource("/search/test1.pdf"));
        assertTrue(cms.existsResource("/search/test1.html"));
        assertTrue(cms.existsResource("/search/test1.doc"));
        assertTrue(cms.existsResource("/search/test1.rtf"));
        assertTrue(cms.existsResource("/search/test1.xls"));
        assertTrue(cms.existsResource("/search/test1.ppt"));

        // publish the project
        OpenCms.getPublishManager().publishProject(cms, new CmsShellReport(cms.getRequestContext().getLocale()));
        OpenCms.getPublishManager().waitWhileRunning();

        // update the search indexes
        I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
        // this call does not throws the rebuild index event
        OpenCms.getSearchManager().rebuildAllIndexes(report);

        // check the online project
        cms.getRequestContext().setCurrentProject(cms.readProject("Online"));

        assertTrue(cms.existsResource("/search/test1.pdf"));
        assertTrue(cms.existsResource("/search/test1.html"));
        assertTrue(cms.existsResource("/search/test1.doc"));
        assertTrue(cms.existsResource("/search/test1.rtf"));
        assertTrue(cms.existsResource("/search/test1.xls"));
        assertTrue(cms.existsResource("/search/test1.ppt"));
    }

    /**
     * Tests searching in the VFS for specific Strings that are placed in 
     * various document formats.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testSearchInDocuments() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing searching in different (complex) document types");

        // 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_ONLINE);
        searchBean.setSearchRoot("/search/");

        searchBean.setQuery("Alkacon Software");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("The OpenCms experts");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("Some content here.");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("Some content there.");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("Some content on a second sheet.");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("Some content on the third sheet.");
        searchResult = searchBean.getSearchResult();
        assertEquals(expected, searchResult.size());

        searchBean.setQuery("漩

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -