Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Andew Jardine
Search custom structure fields (Lucene)
December 19, 2013 6:51 AM
Answer

Andew Jardine

Rank: Regular Member

Posts: 240

Join Date: December 22, 2010

Recent Posts

Hey Guys,

I can't seem to find anything out there with respect to this issue I am having. I have a structure -- oddly enough called article. One of the attributes of the structure is a boolean field "isFeatured". I have written a portlet that does a search query to try to locate the article content that has the flag set to true (if there are multiple I just grab the most recent). The code looks like this --

 1
 2        BooleanQuery searchQuery = BooleanQueryFactoryUtil.create( searchContext );
 3        searchQuery.addRequiredTerm( Field.ENTRY_CLASS_NAME, JournalArticle.class.getName() );
 4        searchQuery.addRequiredTerm( "structureId", structureId );
 5        searchQuery.addRequiredTerm( SEARCH_INDEX_IS_FEATURED_ATTRIBUTE, Boolean.TRUE );
 6        searchQuery.addRequiredTerm( Field.STATUS, WorkflowConstants.STATUS_APPROVED );
 7
 8        Sort[] querySorts = { SortFactoryUtil.getSort( JournalArticle.class, "modified", "desc" ) };
 9
10        searchContext.setSorts( querySorts );
11
12        try
13        {
14            Hits hits = SearchEngineUtil.search( searchContext.getSearchEngineId(), companyId, searchQuery, querySorts, 0, 1 );            
15            documentList = hits.toList();
16            
17            if ( documentList.size() <= 0 )
18                _log.warn( "NO Featured Articles were found! At least one Article content item should be set to featured." );
19        }
20        catch ( SearchException e )
21        {
22            _log.error( "Problem occurred while trying to query the seach index for the latest featured article.", e );
23        }
24
25        if ( documentList.size() > 0 )
26        {
27            if ( _log.isInfoEnabled() ) _log.info( documentList.size() + " results came back from search. Selecting FIRST item in the list." );
28            return documentList.get( 0 ); // return the first item in the list
29        }
30        else
31        {
32            if ( _log.isInfoEnabled() ) _log.info( "Search returns an empty set of results." );
33            return null;
34        }


Despite having several items marked as featured, I can't seem to get results. I used Luke to dig into the search index and can see that the instance-id is being added to the name so searching for "...isFeatured:true" is failing because the name is actually "isFeataured_XnGFQ2" (for example).

Does anyone know how to either wild card the instance portion? Or if there is another parameter or way to do this?
Felipe Araujo
RE: Search custom structure fields (Lucene)
January 3, 2014 5:12 AM
Answer

Felipe Araujo

Rank: New Member

Posts: 3

Join Date: July 4, 2012

Recent Posts

Andrew,

Have you tried creating a "IndexerPostProcessor" and add the name of your field manually in the Document of JournalArticle?

[]'s
Andew Jardine
RE: Search custom structure fields (Lucene)
January 3, 2014 7:43 AM
Answer

Andew Jardine

Rank: Regular Member

Posts: 240

Join Date: December 22, 2010

Recent Posts

Hi Felipe,

Thanks for answering. I have not tried that for two reasons. One, I didn't think to/know to do that. I honestly thought that there must be an eaiser way to deal with this situation as it must be something that is common.

Is that the only way that you know of? Would your solution basically mean that at the end of the indexing I would add my own field without the instance value?

Seems strange to me that there isn't a mechanism already to do this.

aj
Felipe Araujo
RE: Search custom structure fields (Lucene)
January 6, 2014 4:33 AM
Answer

Felipe Araujo

Rank: New Member

Posts: 3

Join Date: July 4, 2012

Recent Posts

Well, I do not know if this is the only way ...
So it was that we did here.
We create a class that implements IndexerPostProcessor postProcessDocument method and add logic to create custom fields indexed in a JournalArticle.
And in liferay-hook.xml file add the following line:
1<indexer-post-processor>
2<indexer-class-name> com.liferay.portlet.journal.model.JournalArticle </ indexer-class-name>
3<indexer-post-processor-impl>you/CustomJournalArticleIndexerPostProcessor</indexer-post-processor-impl>
4</ indexer-post-processor>


[]'s