Liferay boolean query for searching


  By default liferay supports lucene, so how to retrieve entries

 Code as follows ,

        BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

        BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);
 
      Searching the blog

          searchQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());

      Adding required terms to search query 

           searchQuery.addRequiredTerm(Field.ASSET_CATEGORY_IDS, CategoryId);

              fullQuery.add(searchQuery, BooleanClauseOccur.MUST);

     Adding optional terms where the content should belong to at lease one of these categories

           BooleanQuery categoryQuery = BooleanQueryFactoryUtil.create(searchContext);
            categoryQuery.addExactTerm(Field.ASSET_CATEGORY_IDS, CategoryId);

            fullQuery.add(categoryQuery, BooleanClauseOccur.MUST);

Then use fullquery to search blogs

SearchContext searchContext =
 SearchContextFactory. getInstance(PortalUtil.getHttpServletRequest(renderRequest));

Hits hits =  SearchEngineUtil.search(searchContext, fullQuery);

It will return hits , convert to document and iterate document to get the entries.

  List<Document> docList = resultHits.toList();

for ( Document document : docList ){

        doc.get(Field.ENTRY_CLASS_PK);  //classPK
        doc.getValues(Field.ASSET_TAG_NAMES) // tags
       doc.get(Field.MODIFIED_DATE)// modified date
}


Comments

  1. I have found very useful information over there, thanks for sharing this. please try to add more informative posts in order to keep us in touch.

    ReplyDelete
  2. actually the documentation about these topics is very rare , thanks for that

    ReplyDelete
  3. Your words of interest in this article almost match my own thoughts. This article is really

    commendable. I liked this post very much. Thank you very much for sharing such posts.

    Skip Hire Leicester

    ReplyDelete

Post a Comment

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate