Foren

Search custom structure fields (Lucene)

thumbnail
Andew Jardine, geändert vor 10 Jahren.

Search custom structure fields (Lucene)

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
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 --


		BooleanQuery searchQuery = BooleanQueryFactoryUtil.create( searchContext );
		searchQuery.addRequiredTerm( Field.ENTRY_CLASS_NAME, JournalArticle.class.getName() );
		searchQuery.addRequiredTerm( "structureId", structureId );
		searchQuery.addRequiredTerm( SEARCH_INDEX_IS_FEATURED_ATTRIBUTE, Boolean.TRUE );
		searchQuery.addRequiredTerm( Field.STATUS, WorkflowConstants.STATUS_APPROVED );

		Sort[] querySorts = { SortFactoryUtil.getSort( JournalArticle.class, "modified", "desc" ) };

		searchContext.setSorts( querySorts );

		try
		{
			Hits hits = SearchEngineUtil.search( searchContext.getSearchEngineId(), companyId, searchQuery, querySorts, 0, 1 );			
			documentList = hits.toList();
			
			if ( documentList.size() <= 0 )
				_log.warn( "NO Featured Articles were found! At least one Article content item should be set to featured." );
		}
		catch ( SearchException e )
		{
			_log.error( "Problem occurred while trying to query the seach index for the latest featured article.", e );
		}

		if ( documentList.size() > 0 )
		{
			if ( _log.isInfoEnabled() ) _log.info( documentList.size() + " results came back from search. Selecting FIRST item in the list." );
			return documentList.get( 0 ); // return the first item in the list
		}
		else
		{
			if ( _log.isInfoEnabled() ) _log.info( "Search returns an empty set of results." );
			return null;
		}


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, geändert vor 10 Jahren.

RE: Search custom structure fields (Lucene)

New Member Beiträge: 3 Beitrittsdatum: 04.07.12 Neueste Beiträge
Andrew,

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

[]'s
thumbnail
Andew Jardine, geändert vor 10 Jahren.

RE: Search custom structure fields (Lucene)

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
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, geändert vor 10 Jahren.

RE: Search custom structure fields (Lucene)

New Member Beiträge: 3 Beitrittsdatum: 04.07.12 Neueste Beiträge
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:
<indexer-post-processor>
<indexer-class-name> com.liferay.portlet.journal.model.JournalArticle <!-- indexer-class-name-->
<indexer-post-processor-impl>you/CustomJournalArticleIndexerPostProcessor</indexer-post-processor-impl>
<!-- indexer-post-processor--></indexer-class-name></indexer-post-processor>


[]'s