Foros de discusión

Search Portlet - Only show journal articles and PDF files as search result

thumbnail
Renata Willi, modificado hace 9 años.

Search Portlet - Only show journal articles and PDF files as search result

Junior Member Mensajes: 30 Fecha de incorporación: 15/02/13 Mensajes recientes
Hi everyone,

I've got some questions concerning the customization of the search portlet. I read the user guide with the faceted search https://www.liferay.com/de/documentation/liferay-portal/6.1/user-guide/-/ai/lp-6-1-ugen05-using-faceted-search-0 and I customized the search portlet so that only web content and documents and media files are displayed as search result. This works fine.

Advanced Search Settings:
{"facets": [
    {
        "displayStyle": "asset_entries",
        "static": false,
        "weight": 1.5,
        "order": "OrderHitsDesc",
        "data": {
            "values": [
                "com.liferay.portlet.documentlibrary.model.DLFileEntry",
                "com.liferay.portlet.journal.model.JournalArticle"
            ],
            "frequencyThreshold": 1
        },
        "className": "com.liferay.portal.kernel.search.facet.AssetEntriesFacet",
        "label": "asset-type",
        "fieldName": "entryClassName"
    },
    {
        "displayStyle": "asset_tags",
        "static": false,
        "weight": 1.4,
        "order": "OrderHitsDesc",
        "data": {
            "displayStyle": "list",
            "maxTerms": 10,
            "frequencyThreshold": 1,
            "showAssetCount": true
        },
        "className": "com.liferay.portal.kernel.search.facet.MultiValueFacet",
        "label": "tag",
        "fieldName": "assetTagNames"
    },
    {
        "displayStyle": "asset_tags",
        "static": false,
        "weight": 1.3,
        "order": "OrderHitsDesc",
        "data": {
            "displayStyle": "list",
            "maxTerms": 10,
            "frequencyThreshold": 1,
            "showAssetCount": true
        },
        "className": "com.liferay.portal.kernel.search.facet.MultiValueFacet",
        "label": "category",
        "fieldName": "assetCategoryTitles"
    }
]}


My question is, is it possible to restrict the search result further more, that only PDF files from documents & media are displayed and no .jpg and .png files.
Is it also possible to restrict the web content, so that not every web content is displayed in the search result. I mean, can I tag or mark some web contents as 'hidden' for the search?

Thanks for help!

Cheers,
Renata
thumbnail
Jan Geißler, modificado hace 8 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

Liferay Master Mensajes: 735 Fecha de incorporación: 5/07/11 Mensajes recientes
As for the Webcontent:
There is a checkbox, called idexable or searchable on the bottom of the edit.jsp. Uncheck it it will not be found by the search.

To the first question: Restrict the search to ONLY pdf, I have no clue if that is feasable at all.

Regards
Jan
thumbnail
Renata Willi, modificado hace 8 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

Junior Member Mensajes: 30 Fecha de incorporación: 15/02/13 Mensajes recientes
Thanks, Jan. The solution with the checkbox works! Somehow I didn't see that checkbox, when I created the web content....

And here comes the solution to restrict the search result only to PDF files and journal articles:

I'm still on Liferay 6.1. The problem here is that the document library indexer does not index document extension. But it's only a small change in the indexer hook to fix this problem.

1. First create a Liferay Plugin Project of type "hook".
2. Open docroot/WEB-INF/liferay-hook.xml and add the following lines:

<!--?xml version="1.0"?-->

<hook>
   <indexer-post-processor>
 	<indexer-class-name>com.liferay.portlet.documentlibrary.model.DLFileEntry</indexer-class-name>
 	<indexer-post-processor-impl>com.example.hook.indexer.DLFileEntryIndexerPostProcessor</indexer-post-processor-impl>
   </indexer-post-processor>
</hook>

3. Create a new class "DLFileEntryIndexerPostProcessor" in docroot/WEB-INF/src and add the following lines:

package com.example.hook.indexer;

import com.liferay.portal.kernel.search.BaseIndexerPostProcessor;

public class DLFileEntryIndexerPostProcessor extends BaseIndexerPostProcessor {
     public void postProcessDocument(Document document, Object object) {
          DLFileEntry dlFileEntry = (DLFileEntry) object;
          document.addKeyword("extension", dlFileEntry.getExtension());
     }
}

With this code you have indexed the extensions of your files in the Documents and Media library.
To filter all other extensions like .png or .jpg and show only .pdf in your search result you need another hook - this time for the search portlet.

4. Create another Liferay Plugin Project of type "hook".
5. Create a new Liferay Hook Configuration --> Custom JSPs --> Add from Liferay... --> html/portlet/search --> main_search.jspf.
6. Add the following code to main_search.jspf somewhere above the search, i.e. before "Hits hits = indexer.search(searchContext);. With the method setBooleanClause(BooleanClause[] booleanClause) you can pass an arbitrary number of filter criteria as an array of boolean clauses.

...
Query stringQuery = StringQueryFactoryUtil.create("entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:pdf)");
BooleanClause clause = BooleanClauseFactoryUtil.create(searchContext, stringQuery, BooleanClauseOccur.MUST.getName());
searchContext.setBooleanClauses(new BooleanClause[] {clause});
...

7. Finally, you have to add some imports to init.jsp. For that, create another Liferay Hook Configuration --> Custom JSPs --> Next --> Add from Liferay... --> html/portlet/search --> init.jsp

...
page import="com.liferay.portal.kernel.search.SearchContext" %&gt;&lt;%@
page import="com.liferay.portal.kernel.search.BooleanClause" %&gt;&lt;%@
page import="com.liferay.portal.kernel.search.BooleanClauseFactoryUtil" %&gt;&lt;%@
page import="com.liferay.portal.kernel.search.BooleanClauseOccur" %&gt;&lt;%@
page import="com.liferay.portal.kernel.search.StringQueryFactoryUtil" %&gt;&lt;%@
page import="com.liferay.portal.kernel.search.Query" %&gt;&lt;%@
...

8. Finally deploy both hooks.

This code worked for me, I hope for others too....

For more information have a look at the following two blog posts:
https://www.liferay.com/de/web/jonas.yuan/blog/-/blogs/indexer-post-processor-hook-in-liferay-6-1
http://www.liferay.com/de/web/raymond.auge/blog/-/blogs/faceted-search-and-customized-filtering

Best regards,
Renata
thumbnail
Renata Willi, modificado hace 8 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

Junior Member Mensajes: 30 Fecha de incorporación: 15/02/13 Mensajes recientes
Hi everyone,

I modified the search portlet like described above.
Can anyone tell me, why the search result is now sorted that first all relevant PDFs are listed and then all relevant journal articles (web contents). Why isn't the search result listed according search relevancy any more? Why is is separated by type? How can I change the sorting back to search relevancy?

Thanks for help emoticon

Best regards,
Renata
srini vasulu, modificado hace 7 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

Regular Member Mensajes: 139 Fecha de incorporación: 22/02/11 Mensajes recientes
Hi Every one,

In start start.jsp i am added text filed(File Extension) and passing the value pdf and getting the result as per below code.

Query stringQuery = StringQueryFactoryUtil.create("entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:pdf)");
BooleanClause clause = BooleanClauseFactoryUtil.create(searchContext, stringQuery, BooleanClauseOccur.MUST.getName());
searchContext.setBooleanClauses(new BooleanClause[] {clause});


But i passed the values like png,jpeg file extensions in text filed that values passed in above code with OR ,AND conditions and i am getting data with only first value only.

I tried with below code passing multiple extension but i am not getting any result.

Query stringQuery = StringQueryFactoryUtil.create("entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:jpeg)");

Query stringQuery1 = StringQueryFactoryUtil.create("entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:png)");
BooleanClause clause = BooleanClauseFactoryUtil.create(searchContext, stringQuery, BooleanClauseOccur.MUST.getName());
BooleanClause clause1 = BooleanClauseFactoryUtil.create(searchContext, stringQuery1, BooleanClauseOccur.MUST.getName());
searchContext.setBooleanClauses(new BooleanClause[] {clause,clause1 })


i am not getting any result.
my query like below:

+(+(companyId:20155) +((+(entryClassName:com.liferay.portal.model.User) +(status:0)) (+(entryClassName:com.liferay.portlet.bookmarks.model.BookmarksEntry) +(status:0)) (+(entryClassName:com.liferay.portlet.bookmarks.model.BookmarksFolder) +(status:0)) (+(entryClassName:com.liferay.portlet.blogs.model.BlogsEntry) +(status:0)) (+(entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry) +(status:0) +(hidden:false)) (+(entryClassName:com.liferay.portlet.documentlibrary.model.DLFolder) +(status:0) +(hidden:false)) (+(entryClassName:com.liferay.portlet.journal.model.JournalArticle) +(status:0) +(head:true)) (+(entryClassName:com.liferay.portlet.journal.model.JournalFolder) +(status:0)) (+(entryClassName:com.liferay.portlet.messageboards.model.MBMessage) +(status:0) +(discussion:false)) (+(entryClassName:com.liferay.portlet.wiki.model.WikiPage) +(status:0)))) +(entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:jpg)) +(entryClassName:com.liferay.portlet.journal.model.JournalArticle (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +extension:png))


how i can pass multiple extensions same time?

Thank You.
Srini
srini vasulu, modificado hace 7 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

Regular Member Mensajes: 139 Fecha de incorporación: 22/02/11 Mensajes recientes
Hi ,
Any one having any idea about passing multiple extensions?.
thumbnail
Mad Mass, modificado hace 5 años.

RE: Search Portlet - Only show journal articles and PDF files as search res

New Member Mensaje: 1 Fecha de incorporación: 16/03/14 Mensajes recientes
Hi,

I have implemented a facet that, in static configuration, applies a multi-value filter only to a specific class. You can download the code at this address:
https://goo.gl/drxVFC

For deployment, you need to create a plug-in in the EXT environment and place this class in ext-service. Once deployed, this facet is available at the portal level and can be used by both the portal and the plungin portlets.

To the standard configuration of the multi-value facet, I have added the applyTo parameter that contains the name of the class to which the filter is to be applied.

The example below, allows you to search among all articles and only files with extension pdf, doc, docx

{"facets": [
    {
        "static": true,
        "weight": 1.5,
        "order": "OrderHitsDesc",
        "data": {
            "values": [
                "com.liferay.portlet.documentlibrary.model.DLFileEntry",
                "com.liferay.portlet.journal.model.JournalArticle"
            ]
        },
        "className": "com.liferay.portal.kernel.search.facet.AssetEntriesFacet",
        "label": "asset-type",
        "fieldName": "entryClassName"
    },
    {
        "static": true,
        "weight": 1.5,
        "order": "OrderHitsDesc",
        "data": {
            "values": ["docx","pdf","doc"],
            "applyTo": "com.liferay.portlet.documentlibrary.model.DLFileEntry"
        },
        "className": "biz.ilmassa.liferay.portal.kernel.search.facet.MultiValueFilterFacet",
        "fieldName": "extension"
    }
]}