⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.java

📁 一个jsp写的bbs
💻 JAVA
字号:
package com.laoer.bbscs.web.action;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import com.laoer.bbscs.web.form.*;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.cw.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import java.io.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.lang.*;
import com.laoer.bbscs.service.config.UserConfig;

public class Search
    extends BaseAction {

  private static final Log logger = LogFactory.getLog(Search.class);

  private UserConfig userConfig;

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                               HttpServletResponse response) {

    SearchForm searchForm = (SearchForm) form;

    ActionMessages messages = new ActionMessages();

    if (StringUtils.isBlank(searchForm.getQuery())) {
      messages.add("error.nullerror", new ActionMessage("error.nullerror"));
      this.saveErrors(request, messages);
      return mapping.findForward(this.FORWARD_ERROR);
    }

    File indexFilePath = this.getUserConfig().getIndexFilePath();

    IndexSearcher searcher = null;

    Hits hits = null;
    int startindex = searchForm.getStartat();
    int maxpage = searchForm.getMaxresults();
    int thispage = 0;

    if (searchForm.getBid() == 0) {
      try {
        searcher = new IndexSearcher(IndexReader.open(indexFilePath));
        Analyzer analyzer = new CWordAnalyzer();
        BooleanQuery currentQuery = new BooleanQuery();
        QueryParser qpTitle = new QueryParser("title", analyzer);
        Query queryTitle = qpTitle.parse(searchForm.getQuery());
        QueryParser qpContents = new QueryParser("contents", analyzer);
        Query queryContents = qpContents.parse(searchForm.getQuery());
        currentQuery.add(queryTitle, BooleanClause.Occur.SHOULD);
        currentQuery.add(queryContents, BooleanClause.Occur.SHOULD);
        //System.out.println(currentQuery.toString());
        thispage = maxpage;
        hits = searcher.search(currentQuery);
        request.setAttribute("istartindex", new Integer(startindex));
        request.setAttribute("ithispage", new Integer(thispage));
        request.setAttribute("hits", hits);
        request.setAttribute("queryString", searchForm.getQuery());
      }
      catch (Exception ex) {
        logger.error(ex);
        messages.add("error.search.noresult", new ActionMessage("error.search.noresult"));
        this.saveErrors(request, messages);
        return mapping.findForward(this.FORWARD_ERROR);
      }
    }
    else {
      try {
        searcher = new IndexSearcher(IndexReader.open(indexFilePath));
        Analyzer analyzer = new CWordAnalyzer();
        BooleanQuery currentQuery = new BooleanQuery();

        TermQuery searchingType = new TermQuery(new Term("boardId", String.valueOf(searchForm.getBid())));
        currentQuery.add(searchingType, BooleanClause.Occur.MUST);

        QueryParser qpTitle = new QueryParser("title", analyzer);
        Query queryTitle = qpTitle.parse(searchForm.getQuery());
        QueryParser qpContents = new QueryParser("contents", analyzer);
        Query queryContents = qpContents.parse(searchForm.getQuery());
        BooleanQuery currentQuery1 = new BooleanQuery();
        currentQuery1.add(queryTitle, BooleanClause.Occur.SHOULD);
        currentQuery1.add(queryContents, BooleanClause.Occur.SHOULD);

        currentQuery.add(currentQuery1, BooleanClause.Occur.MUST);
        //System.out.println(currentQuery.toString());
        thispage = maxpage;
        hits = searcher.search(currentQuery);
        request.setAttribute("istartindex", new Integer(startindex));
        request.setAttribute("ithispage", new Integer(thispage));
        request.setAttribute("hits", hits);
        request.setAttribute("queryString", searchForm.getQuery());
      }
      catch (Exception ex1) {
        logger.error(ex1);
        messages.add("error.search.noresult", new ActionMessage("error.search.noresult"));
        this.saveErrors(request, messages);
        return mapping.findForward(this.FORWARD_ERROR);
      }

    }

    return mapping.findForward("search");
  }

  public UserConfig getUserConfig() {
    return userConfig;
  }

  public void setUserConfig(UserConfig userConfig) {
    this.userConfig = userConfig;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -