[opencms-dev] Problem with search in opencms 6

Ernesto De Santis ernesto.desantis at colaborativa.net
Fri Sep 2 19:38:36 CEST 2005


OK, the code and comments:

<%

  CmsJspActionElement cmsa = new CmsJspActionElement(pageContext, 
request, response );
  CmsObject cmso = cmsa.getCmsObject();
  CmsRequestContext requestContext = cmsa.getRequestContext();

//get the query string from a user input
  String query = request.getParameter("query");
  if(query == null || "".equals(query))
     throw new Exception("empty query is invalid query");

  int currentPage;
  String pageNumberStr = request.getParameter("page");
  if(pageNumberStr == null || "".equals(pageNumberStr)){
     currentPage = 1;
  } else {
     currentPage = new Integer(pageNumberStr).intValue();
  }

//the searcher instance
  CmsSearch searcher = new CmsSearch();
  searcher.init(cmso);

//you need have configured your index. This is documented by alcakon in 
documentation.
  searcher.setIndex("YOURINDEX");

//set over which fields you want search.
  searcher.setField(new String[] {I_CmsDocumentFactory.DOC_TITLE_INDEXED,
                                  I_CmsDocumentFactory.DOC_KEYWORDS,
                                  I_CmsDocumentFactory.DOC_DESCRIPTION,
                                  I_CmsDocumentFactory.DOC_CONTENT});

  searcher.setMatchesPerPage(10);
  searcher.setPage(currentPage);

//I don't are completly sure about this features. Are commented in my code.
//searcher.setQueryLength(3); //maybe the string query lenght need by > to 3
//searcher.setDisplayPages(11); //maybe the results need by: 
resultsCount < displayPages * matchesPerPage.

  searcher.setQuery(query);
  List results = searcher.getSearchResult();

  int resultsCount = searcher.getSearchResultCount();
  int resultsPerPage = searcher.getMatchesPerPage();

  int pages= resultsCount / resultsPerPage;
  if ((resultsCount % resultsPerPage) != 0) {
     pages++;
  }

%>

<br><br>
<h1>UCDWorks Search</h1>

<h2><%= resultsCount %> results: </h2>

<%
int i = (currentPage - 1) * resultsPerPage;
Iterator itResults = results.iterator();
while(itResults.hasNext()){
//get a hit
     CmsSearchResult result = (CmsSearchResult) itResults.next();
     String path = requestContext.removeSiteRoot(result.getPath());
     String title = result.getTitle();
     String description = result.getDescription();

     if(!CmsStringUtil.isNotEmpty(title)){
         title = path;
     }
%>
     <%= ++i + "- " %><a href="<cms:link><%= path %></cms:link>"><%= 
title %></a><br>  
     <%= "Path: " + path %><br>
     <% if(CmsStringUtil.isNotEmpty(description)) %><%= "Summary: " + 
description + "<br>"%>
     <br>

<%
}//while
%>

<%

//paging
for(int j = 0; j < pages; j++){
   int tempPage = j + 1;
   if(currentPage != tempPage){
      %><a href='search.html?query=<%= query %>&page=<%= tempPage 
%>'><%= tempPage %></a> <%
   } else {
      %><%= tempPage %> <%
   }
}


%>

Good luck.
Ernesto.

philip lau wrote:

>Hi Ernesto,
>
>I am currently creating an intranet site using opencms. I would also
>like to know how to implement a search function embedded in my web
>page. Please could you also include me when sending your 'search
>code'.
>
>Many thanks,
>
>Philip Lau
>
>On 01/09/05, Ernesto De Santis <ernesto.desantis at colaborativa.net> wrote:
>  
>
>>Hi Jose
>>
>>You need to see the classes under org.opencms.search package.
>>I can send my search code later, in this moment I are out of office.
>>
>>Bye,
>>Ernesto.
>>
>>jose manuel morenas brenes wrote:
>>
>>    
>>
>>>Hello to all:
>>>
>>>I know that in OPENCMS6 exists un module in administration to search,
>>>but my ask is how can i make a searcher in my application? (without
>>>have to go inside administration).
>>>
>>>Gracias.
>>>
>>>------------------------------------------------------------------------
>>>
>>>
>>>
>>>_______________________________________________
>>>This mail is send to you from the opencms-dev mailing list
>>>To change your list options, or to unsubscribe from the list, please visit
>>>http://mail.opencms.org/mailman/listinfo/opencms-dev
>>>
>>>      
>>>
>>
>>_______________________________________________
>>This mail is send to you from the opencms-dev mailing list
>>To change your list options, or to unsubscribe from the list, please visit
>>http://mail.opencms.org/mailman/listinfo/opencms-dev
>>
>>    
>>
>
>
>_______________________________________________
>This mail is send to you from the opencms-dev mailing list
>To change your list options, or to unsubscribe from the list, please visit
>http://mail.opencms.org/mailman/listinfo/opencms-dev
>  
>




More information about the opencms-dev mailing list