Foren
Liferay Default Search
Geovan Goes, geändert vor 7 Jahren.
Liferay Default Search
New Member Beiträge: 4 Beitrittsdatum: 07.03.16 Neueste Beiträge
Hello,
I'm trying to create a service of search, i need of this function in a app (Liferay Screens) and i would to use the same engine that search portlet... but i'm having troubles to do this...
Someone can help me ?
In fact, i dont know if it's possible to do this.
Anyway, thank you!
I'm trying to create a service of search, i need of this function in a app (Liferay Screens) and i would to use the same engine that search portlet... but i'm having troubles to do this...
Someone can help me ?
In fact, i dont know if it's possible to do this.
Anyway, thank you!
Pavel Savinov, geändert vor 7 Jahren.
RE: Liferay Default Search
Junior Member Beiträge: 54 Beitrittsdatum: 29.05.15 Neueste Beiträge
Hey Geovan,
What type of troubles do you have? You have got some errors while implementing your search? Maybe you have some error trace?
You can take a look at this knowledge base page:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/implementing-search-and-indexing
What type of troubles do you have? You have got some errors while implementing your search? Maybe you have some error trace?
You can take a look at this knowledge base page:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/implementing-search-and-indexing
Geovan Goes, geändert vor 7 Jahren.
RE: Liferay Default Search
New Member Beiträge: 4 Beitrittsdatum: 07.03.16 Neueste Beiträge
Hi,i has evolved using this link, thak you!
But I still have problems...
Now i wanna search DLFileEntry and JournalArticle that have in your title OR in your content the keywords that user send to method, but I can't do the search in these two fields.
below my code...He has some comments, some tests I did.
If anyone can help me with this.
Thank you!
But I still have problems...
Now i wanna search DLFileEntry and JournalArticle that have in your title OR in your content the keywords that user send to method, but I can't do the search in these two fields.
below my code...He has some comments, some tests I did.
If anyone can help me with this.
Thank you!
public JSONArray search(String keywords, long userId, long groupId)
{
try
{
SearchContext searchContext = buildSearchContext();
QueryConfig queryConfig = searchContext.getQueryConfig();
setDefaultQueryConfig(queryConfig);
searchContext.setQueryConfig(queryConfig);
//TermQuery termQuery1 = TermQueryFactoryUtil.create(searchContext, Field.CONTENT, keywords);
TermQuery termQuery0 = TermQueryFactoryUtil.create(searchContext, Field.TITLE, "%"+keywords);
TermQuery termQuery2 = TermQueryFactoryUtil.create(searchContext, "visible", "true");
Hits hits;
Query stringQuery = getQuery();
BooleanClause clause = BooleanClauseFactoryUtil.create(searchContext, stringQuery, BooleanClauseOccur.MUST.getName());
BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);
//booleanQuery.addRequiredTerm(Field.TITLE, keywords);
booleanQuery.addRequiredTerm("head", Boolean.TRUE);
//booleanQuery.add(termQuery1, BooleanClauseOccur.MUST);
booleanQuery.add(termQuery2, BooleanClauseOccur.MUST);
booleanQuery.add(termQuery0, BooleanClauseOccur.MUST);
searchContext.setBooleanClauses(new BooleanClause[] {clause});
User user = UserLocalServiceUtil.getUser(userId);
//Indexer indexer = FacetedSearcher.getInstance();
//hits = indexer.search(searchContext);
hits = SearchEngineUtil.search(searchContext, booleanQuery);
//String[] queryTerms = hits.getQueryTerms();
JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
PermissionChecker pc = PermissionCheckerFactoryUtil.create(user);
for(Document doc : hits.getDocs())
{
String className = doc.get(Field.CLASS_NAME_ID);
String id = doc.get(Field.CLASS_PK);
String actionId = ActionKeys.VIEW;
boolean hasPermission = pc.hasPermission(groupId, className, id, actionId);
if(hasPermission)
{
JSONObject json = JSONFactoryUtil.createJSONObject();
if(!doc.get(Field.TITLE).isEmpty())
{
Map<string, field> fields = doc.getFields();
for (String key : fields.keySet())
{
json.put(key, fields.get(key).getValue());
}
jsonArray.put(json);
}
}
}
return jsonArray;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
private Query getQuery()
{
return StringQueryFactoryUtil.create(
"entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry" +
" (+entryClassName:com.liferay.portlet.journal.model.JournalArticle)"
);
}
private SearchContext buildSearchContext()
{
SearchContext searchContext = new SearchContext();
long companyId = PortalUtil.getDefaultCompanyId();
Locale locale = new Locale("pt_BR");
searchContext.setLocale(locale);
long groupId = UserPersistenceUtil.findSiteId(companyId);
searchContext.setCompanyId(companyId);
searchContext.setGroupIds(new long[] {groupId} );
searchContext.setStart(0);
searchContext.setLike(true);
searchContext.setEnd(Integer.MAX_VALUE);
searchContext.setEntryClassNames(new String[]
{
JournalArticle.class.toString()
});
return searchContext;
}
private void setDefaultQueryConfig(QueryConfig queryConfig) {
queryConfig.setCollatedSpellCheckResultEnabled(Boolean.parseBoolean(PropsUtil.get(PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED)));
queryConfig.setCollatedSpellCheckResultScoresThreshold(Integer.parseInt(PropsUtil.get(PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD)));
queryConfig.setHighlightEnabled(Boolean.parseBoolean(PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED)));
queryConfig.setQueryIndexingEnabled(Boolean.parseBoolean(PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED)));
queryConfig.setQueryIndexingThreshold(Integer.parseInt(PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD)));
queryConfig.setQuerySuggestionEnabled(Boolean.parseBoolean(PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED)));
queryConfig.setQuerySuggestionScoresThreshold(Integer.parseInt(PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD)));
queryConfig.setQuerySuggestionsMax(Integer.parseInt(PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX)));
}
</string,>