RE: Liferay 7.1 - Filtering search results by specific custom field

Jan Tošovský, modified 6 Years ago. Liferay Master Posts: 576 Join Date: 7/22/10 Recent Posts

In LR 6.2 I could filter the results by modifying the search context this way:

searchContext.setKeywords(keywords + " AND expando/custom_fields/region:" + myRegion);

 

In ElasticSearch custom fields are named differently, but this doesn't work (returns no results):

searchContext.setKeywords(keywords + " AND expando__custom_fields__region:" + myRegion);

 

How this kind of filter can be accomplished? (I am customizing the User Admin search code).

 

thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts

For searching by expandos, maybe try something similar to this?

 

https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-search/portal-search-test-util/src/main/java/com/liferay/portal/search/test/util/expando/BaseExpandoTestCase.java#L303

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Hey Amos, 

 

I just checked your link -- very cool! Is that something that is new to 7.1?

thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts

Andrew,

 

I'm not sure, I just found it myself while looking for something else :)

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Hahaha love it when that happens. I can't even count the number bet of things I've learned about Liferay that started with me stumbling into something :). Thanks for sharing! 
thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Hi Jan,

 

Usually when I I want to accomplish something like this I would use the setBooleanClauses() method on teh search context, rather than changing the keywords that the user entered. It's still working with the searchContext (as you are right now) but you would do something like ..

 

BooleanClause expandoClause = BooleanClauseFactoryUtil.create(<expando field name>, <your value>,BooleanClauseOccur.MUST.toString());
searchContext.setBooleanClauses(new BooleanClause[]{expandoClause});

 

That usually works for me.