Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Required terms in different fields doesn't work in custom search
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.
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.
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.
Great!
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.