无法使用search util在liferay portlet中搜索期刊文章

时间:2013-04-09 16:24:52

标签: java java-ee liferay portlet

我使用以下代码,但无法在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();
        }

    }

  }

我得到的输出是......

fullQuery ===============&gt;&gt; +(+((title:mridul title:test)))

请帮助..

2 个答案:

答案 0 :(得分:0)

Lucene使用字段来索引数据。

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

或者

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

答案 1 :(得分:-1)

看起来您正在搜索确切的短语“mridul test”。我想你可能想搜索“mridul”和“test”。如果是这样,请给它一个旋转:

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