RE: Required terms in different fields doesn't work in custom search

thumbnail
Florencia Gadea, modificado 12 Anos atrás. Regular Member Postagens: 146 Data de Entrada: 27/03/12 Postagens Recentes
Hi Everyone,

I'm developing a custom search portlet for JournalArticle and DLFileEntry. I would like to have required terms, but in different fields. If I have the phrase "tekst bolsius" (without double quotes), I would like the query to be something like:

+(((+title:tekst +title:bolsius)^4.0) (+content:tekst +content:bolsius) (+description:tekst +description:bolsius)))

So both terms should be present in the title OR in the content OR in the description. What I get right now is something like:

+(+title:tekst +title:bolsius +content:het +content:riool +description:tekst +description:bolsius)

Here is my code:
SearchContext searchContext = SearchContextFactory.getInstance(servletRequest);

Map<string, serializable> attributes =	new HashMap<string, serializable>();
attributes.put(Field.TITLE, query);		
attributes.put(Field.DESCRIPTION, query);
attributes.put(Field.CONTENT, query);
attributes.put("paginationType", "regular");
searchContext.setAttributes(attributes);

// add facets
Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);
assetEntriesFacet.setStatic(true);			
searchContext.addFacet(assetEntriesFacet);

Facet scopeFacet = new ScopeFacet(searchContext);
scopeFacet.setStatic(true);			
searchContext.addFacet(scopeFacet);

// add asset type
String[] entryClassNames = { JournalArticle.class.getName(), DLFileEntry.class.getName() };
searchContext.setEntryClassNames(entryClassNames);

searchContext.setGroupIds(null);
searchContext.setStart(start);
searchContext.setEnd(end);
searchContext.setLocale(LocaleUtil.fromLanguageId("nl_NL"));

BooleanQuery booleanQueryCategoryId = BooleanQueryFactoryUtil.create(searchContext);
booleanQueryCategoryId.addTerm(Field.ASSET_CATEGORY_IDS, String.valueOf(leidraadCategoryId), false, BooleanClauseOccur.SHOULD);
booleanQueryCategoryId.addTerm(Field.ASSET_CATEGORY_IDS, String.valueOf(bestandenDerdenCategoryId), false, BooleanClauseOccur.SHOULD);
BooleanClause booleanClauseCategoryId = BooleanClauseFactoryUtil.create(booleanQueryCategoryId, BooleanClauseOccur.MUST.getName());
searchContext.setBooleanClauses(new BooleanClause[] { booleanClauseCategoryId });

List<document> docs = hits.toList();
</document></string,></string,>


Is it possible what I want to achieve?

Regards,

Florencia.
thumbnail
Ray Augé, modificado 12 Anos atrás. Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
Rather than using attributes for passing in the input, create more booleanClauses as you have done from line 28 to 31.

That way you can force the input into exactly the strategy you want for each of those fields.
thumbnail
Florencia Gadea, modificado 12 Anos atrás. Regular Member Postagens: 146 Data de Entrada: 27/03/12 Postagens Recentes
Hi Ray,

I finally managed to achieve it with this code:

BooleanQuery booleanQueryTitle = BooleanQueryFactoryUtil.create(searchContext);
BooleanQuery booleanQueryContent = BooleanQueryFactoryUtil.create(searchContext);
BooleanQuery booleanQueryDescription = BooleanQueryFactoryUtil.create(searchContext);
booleanQueryTitle.addRequiredTerm("title", term, false);				
booleanQueryContent.addRequiredTerm("content", term, false);
booleanQueryDescription.addRequiredTerm("description", term, false);
BooleanQuery innerBooleanQuery = BooleanQueryFactoryUtil.create(searchContext);
innerBooleanQuery.add(booleanQueryTitle, BooleanClauseOccur.SHOULD);	
innerBooleanQuery.add(booleanQueryContent, BooleanClauseOccur.SHOULD);
innerBooleanQuery.add(booleanQueryDescription, BooleanClauseOccur.SHOULD);				
BooleanClause booleanClauseGeneral = BooleanClauseFactoryUtil.create(innerBooleanQuery, BooleanClauseOccur.MUST.getName());

Thanks,

Flor.
thumbnail
Ray Augé, modificado 12 Anos atrás. Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
Great!
thumbnail
Karthik Nainupatruni, modificado 6 Anos atrás. Junior Member Postagens: 28 Data de Entrada: 05/05/15 Postagens Recentes
Hi 
I would like to know more on this, Here how we can get hits object from SearchContext is not clear.
Are you using  IndexerRegistryUtil  API or different API, Please let me know.