热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

lucene(全文搜索)_luceneweb例子

先来看看效果图:由于我没"D:\opt\lucene\index",所以不能搜索出东东下载地址:http:apache.dataguru.cnlucenejava2

先来看看效果图:

由于我没"D:\opt\lucene\index",所以不能搜索出东东...

下载地址:

http://apache.dataguru.cn/lucene/java/2.9.4/

lucene-2.9.4-src.zip (包含源码)

lucene-2.9.4.zip

项目结构:

=======================================================

源码部分

=======================================================

/luceneweb/WebContent/configuration.jsp

 1 
17 <%
18 /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */
19 String appTitle = "Apache Lucene Example - Intranet Server Search Application";
20 /* make sure you point the below string to the index you created with IndexHTML */
21 String indexLocation = "/opt/lucene/index";
22 String appfooter = "Apache Lucene Template WebApp 1.0";
23 %>

/luceneweb/WebContent/footer.jsp

 1 
17 <% /* Author Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %>
18 <p align="center">
19 <%=appfooter%>
20 p>
21 body>
22 html>

/luceneweb/WebContent/header.jsp

 1 
17 <%@include file="configuration.jsp"%>
18 <% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net */ %>
19 <html>
20 <head>
21 <title><%=appTitle%>title>
22 head>
23 <body>
24
25 <p align="center">
26 Welcome to the Lucene Template application. (This is the header)
27 p>

/luceneweb/WebContent/index.jsp

 1 
17 <%@include file="header.jsp"%>
18 <% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %>
19 <center>
20 <form name="search" action="results.jsp" method="get">
21 <p>
22 <input name="query" size="44"/> Search Criteria
23 p>
24 <p>
25 <input name="maxresults" size="4" value="100"/> Results Per Page 
26 <input type="submit" value="Search"/>
27 p>
28 form>
29 center>
30 <%@include file="footer.jsp"%>

/luceneweb/WebContent/results.jsp

  1 
17 <%@ page import="javax.servlet.*, javax.servlet.http.*, java.io.*, org.apache.lucene.analysis.*, org.apache.lucene.analysis.standard.StandardAnalyzer, org.apache.lucene.document.*, org.apache.lucene.index.*, org.apache.lucene.store.*, org.apache.lucene.search.*, org.apache.lucene.queryParser.*, org.apache.lucene.demo.*, org.apache.lucene.demo.html.Entities, java.net.URLEncoder, org.apache.lucene.util.Version"%>
18
19 <%
20 /*
21 Author: Andrew C. Oliver, SuperLink Software, Inc. (acoliver2@users.sourceforge.net)
22
23 This jsp page is deliberatly written in the horrible java directly embedded
24 in the page style for an easy and concise demonstration of Lucene.
25 Due note...if you write pages that look like this...sooner or later
26 you'll have a maintenance nightmare. If you use jsps...use taglibs
27 and beans! That being said, this should be acceptable for a small
28 page demonstrating how one uses Lucene in a web app.
29
30 This is also deliberately overcommented. ;-)
31
32 */
33 %>
34 <%!public String escapeHTML(String s) {
35 s = s.replaceAll("&", "&");
36 s = s.replaceAll("<", "<");
37 s = s.replaceAll(">", ">");
38 s = s.replaceAll("\"", "&quot;");
39 s = s.replaceAll("'", "'");
40 return s;
41 }%>
42 <%@include file="header.jsp"%>
43 <%
44 boolean error = false; //used to control flow for error messages
45 String indexName = indexLocation; //local copy of the configuration variable
46 IndexSearcher searcher = null; //the searcher used to open/search the index
47 Query query = null; //the Query created by the QueryParser
48 TopDocs hits = null; //the search results
49 int startindex = 0; //the first index displayed on this page
50 int maxpage = 50; //the maximum items displayed on this page
51 String queryString = null; //the query entered in the previous page
52 String startVal = null; //string version of startindex
53 String maxresults = null; //string version of maxpage
54 int thispage = 0; //used for the for/next either maxpage or
55 //hits.totalHits - startindex - whichever is
56 //less
57
58 try {
59 IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexName)), true); // only searching, so read-only=true
60 searcher = new IndexSearcher(reader); //create an indexSearcher for our page
61 //NOTE: this operation is slow for large
62 //indices (much slower than the search itself)
63 //so you might want to keep an IndexSearcher
64 //open
65
66 } catch (Exception e) { //any error that happens is probably due
67 //to a permission problem or non-existant
68 //or otherwise corrupt index
69 %>
70 <p>ERROR opening the Index - contact sysadmin!p>
71 <p>
72 Error message:
73 <%=escapeHTML(e.getMessage())%>p>
74 <%
75 error = true; //don't do anything up to the footer
76 }
77 %>
78 <%
79 if (error == false) { //did we open the index?
80 queryString = request.getParameter("query"); //get the search criteria
81 startVal = request.getParameter("startat"); //get the start index
82 maxresults = request.getParameter("maxresults"); //get max results per page
83 try {
84 maxpage = Integer.parseInt(maxresults); //parse the max results first
85 startindex = Integer.parseInt(startVal); //then the start index
86 } catch (Exception e) {
87 } //we don't care if something happens we'll just start at 0
88 //or end at 50
89
90 if (queryString == null)
91 throw new ServletException("no query " + //if you don't have a query then
92 "specified"); //you probably played on the
93 //query string so you get the
94 //treatment
95
96 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); //construct our usual analyzer
97 try {
98 QueryParser qp = new QueryParser("contents", analyzer);
99 query = qp.parse(queryString); //parse the
100 } catch (ParseException e) { //query and construct the Query
101 //object
102 //if it's just "operator error"
103 //send them a nice error HTML
104 %>
105 <p>
106 Error while parsing query:
107 <%=escapeHTML(e.getMessage())%>p>
108 <%
109 error = true; //don't bother with the rest of
110 //the page
111 }
112 }
113 %>
114 <%
115 if (error == false && searcher != null) { // if we've had no errors
116 // searcher != null was to handle
117 // a weird compilation bug
118 thispage = maxpage; // default last element to maxpage
119 hits = searcher.search(query, maxpage + startindex); // run the query
120 if (hits.totalHits == 0) { // if we got no results tell the user
121 %>
122 <p>I'm sorry I couldn't find what you were looking for.p>
123 <%
124 error = true; // don't bother with the rest of the
125 // page
126 }
127 }
128
129 if (error == false && searcher != null) {
130 %>
131 <table>
132 <tr>
133 <td>Documenttd>
134 <td>Summarytd>
135 tr>
136 <%
137 if ((startindex + maxpage) > hits.totalHits) {
138 thispage = hits.totalHits - startindex; // set the max index to maxpage or last
139 } // actual search result whichever is less
140
141 for (int i = startindex; i < (thispage + startindex); i++) { // for each element
142 %>
143 <tr>
144 <%
145 Document doc = searcher.doc(hits.scoreDocs[i].doc); //get the next document
146 String doctitle = doc.get("title"); //get its title
147 String url = doc.get("path"); //get its path field
148 if (url != null && url.startsWith("../webapps/")) { // strip off ../webapps prefix if present
149 url = url.substring(10);
150 }
151 if ((doctitle == null) || doctitle.equals("")) //use the path if it has no title
152 doctitle = url;
153 //then output!
154 %>
155 <td><a href="<%=url%>"><%=doctitle%>a>td>
156 <td><%=doc.get("summary")%>td>
157 tr>
158 <%
159 }
160 %>
161 <%
162 if ((startindex + maxpage) < hits.totalHits) { //if there are more results...display
163 //the more link
164
165 String moreurl = "results.jsp?query=" + URLEncoder.encode(queryString) + //construct the "more" link
166 "&maxresults=" + maxpage + "&startat=" + (startindex + maxpage);
167 %>
168 <tr>
169 <td>td>
170 <td><a href="<%=moreurl%>">More Results>>a>td>
171 tr>
172 <%
173 }
174 %>
175 table>
176
177 <%
178 } //then include our footer.
179 if (searcher != null)
180 searcher.close();
181 %>
182 <%@include file="footer.jsp"%>



本文出处:http://www.cnblogs.com/hongten/archive/2012/11/27/hongten_lucene_luceneweb.html

感谢作者

推荐阅读
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • NotSupportedException无法将类型“System.DateTime”强制转换为类型“System.Object”
    本文介绍了在使用LINQ to Entities时出现的NotSupportedException异常,该异常是由于无法将类型“System.DateTime”强制转换为类型“System.Object”所导致的。同时还介绍了相关的错误信息和解决方法。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • OpenCV4.5.0+contrib编译流程及解决错误方法
    本文介绍了OpenCV4.5.0+contrib的编译流程,并提供了解决常见错误的方法,包括下载失败和路径修改等。同时提供了相关参考链接。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • Sleuth+zipkin链路追踪SpringCloud微服务的解决方案
    在庞大的微服务群中,随着业务扩展,微服务个数增多,系统调用链路复杂化。Sleuth+zipkin是解决SpringCloud微服务定位和追踪的方案。通过TraceId将不同服务调用的日志串联起来,实现请求链路跟踪。通过Feign调用和Request传递TraceId,将整个调用链路的服务日志归组合并,提供定位和追踪的功能。 ... [详细]
  • 本文介绍了在Go语言中可见性与scope的规则,包括在函数内外声明的可见性、命名规范和命名风格,以及变量声明和短变量声明的语法。同时,还介绍了变量的生命周期,包括包级别变量和局部变量的生命周期,以及变量在堆和栈上分配的规则和逃逸分析的概念。 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • PatchODAX8: ... [详细]
author-avatar
跟随自己的2502917817
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有