简体   繁体   中英

Unable to search journal article in liferay portlet using search util

I am using the below code but it is not able to search the journal article/web content in liferay 6.1

  package com.abp.portlets;

  import java.io.IOException;

  import javax.portlet.PortletException;
  import javax.portlet.RenderRequest;
  import javax.portlet.RenderResponse;

  import com.liferay.portal.kernel.search.BooleanClauseOccur;
  import com.liferay.portal.kernel.search.BooleanQuery;
  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
  import com.liferay.portal.kernel.search.Field;
  import com.liferay.portal.kernel.search.Hits;
  import com.liferay.portal.kernel.search.ParseException;
  import com.liferay.portal.kernel.search.SearchContext;
  import com.liferay.portal.kernel.search.SearchEngineUtil;
  import com.liferay.portal.kernel.search.SearchException;
  import com.liferay.portal.kernel.util.Validator;
  import com.liferay.portal.kernel.util.WebKeys;
  import com.liferay.portal.theme.ThemeDisplay;
  import com.liferay.util.bridges.mvc.MVCPortlet;

  /**
   * Portlet implementation class Search
   */
  public class Search extends MVCPortlet {

    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException 
    {
        ThemeDisplay themeDisplay = (ThemeDisplay) 
                renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

        SearchContext searchContext = new SearchContext();
        searchContext.setSearchEngineId(SearchEngineUtil.SYSTEM_ENGINE_ID);
        BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(searchContext);
        contextQuery.addRequiredTerm(Field.COMPANY_ID, themeDisplay.getCompanyId());
        contextQuery.addRequiredTerm(Field.GROUP_ID, themeDisplay.getScopeGroupId());

        BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

        String keywords = "mridul test"; 
         BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);

                    if (Validator.isNotNull(keywords)) {
                         keywords = keywords.trim();
                         try {
                            searchQuery.addTerm(Field.TITLE, keywords,true);


                         } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                    }



                  //  BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

                    //fullQuery.add(contextQuery, BooleanClauseOccur.MUST);

  //                    if (searchQuery.clauses().size() > 0) {
  //                        fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
  //                    }

         System.out.println("fullQuery===============>>"+fullQuery);
        try {
            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
            for(int i=0;i<hits.getLength();i++)
            {
            System.out.println(hits.snippet(i).toString());
            }
        } catch (SearchException e) { 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

  }

The output I am getting is...

fullQuery===============>>+(+((title:mridul title:test)))

Please help..

Lucene uses fields to index data.

searchQuery.addTerm(**Field.CONTENT**, keywords,true);

Or

searchQuery.addTerms(new String[]{Field.TITLE,Field.DESCRIPTION,Field.CONTENT}, keywords, true)

It looks like you are searching for the exact phrase "mridul test". I think you probably want to search for "mridul" and "test". If so, give this a spin:

String[] terms = keywords.split(" ");
for(String term : terms){
    searchQuery.addTerm(Field.TITLE, term,true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM