Message Boards
Required terms in different fields doesn't work in custom search
Florencia Gadea, modified 10 Years ago.
Required terms in different fields doesn't work in custom search
Regular Member Posts: 146 Join Date: 3/27/12 Recent Posts
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:
Is it possible what I want to achieve?
Regards,
Florencia.
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.
Ray Augé, modified 10 Years ago.
RE: Required terms in different fields doesn't work in custom search
Liferay Legend Posts: 1197 Join Date: 2/8/05 Recent Posts
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.
That way you can force the input into exactly the strategy you want for each of those fields.
Florencia Gadea, modified 10 Years ago.
RE: Required terms in different fields doesn't work in custom search (Answer)
Regular Member Posts: 146 Join Date: 3/27/12 Recent Posts
Hi Ray,
I finally managed to achieve it with this code:
Thanks,
Flor.
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.
Ray Augé, modified 10 Years ago.
RE: Required terms in different fields doesn't work in custom search
Liferay Legend Posts: 1197 Join Date: 2/8/05 Recent Posts
Great!
Karthik Nainupatruni, modified 3 Years ago.
RE: Required terms in different fields doesn't work in custom search
Junior Member Posts: 28 Join Date: 5/5/15 Recent Posts
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.
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.