`
deepfuture
  • 浏览: 4375456 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79862
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:69455
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:102784
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:283972
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14910
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:66977
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31807
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45791
社区版块
存档分类
最新评论

lucene入门-简单的WEB搜索界面

阅读更多

使用servlet和tomcat6,注意事项:

1,将lucene-core-2.9.0.jar复制到WEB-INF\lib

2,按照tomcat的要求组织好目录

3,编写好web.xml

4,编译产生的SluceneSearcher.class类拷到WEB-INF\classes\bservlet

web.xml配置如下:

<?xml version="1.0" encoding="GB2312"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>testsearch2</servlet-name>
<servlet-class>bservlet.SluceneSearcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testsearch2</servlet-name>
<url-pattern>/deepfuturesou</url-pattern>
</servlet-mapping>
</web-app>

建立索引代码

package bindex;
import java.io.File;
import tool.FileText;
import tool.FileList;
import java.io.*;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.store.LockObtainFailedException;
public class FileIndexer {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String indexPath ="indexes";
try {
IndexWriter indexWriter = new IndexWriter(indexPath,new MMAnalyzer());
String[] files=FileList.getFiles("htmls");
int num=files.length;
for(int i=0;i<num;i++){
Document doc=new Document();
File f=new File(files[i]);

String name=f.getName();
String content=FileText.getText(f);
String path=f.getPath();
Field field=new Field("name",path+name,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
field=new Field("path",path,Field.Store.YES,Field.Index.NO);
doc.add(field);
indexWriter.addDocument(doc);
System.out.println("File:"+path+name+" indexed!");
}
System.out.println("OK!");
indexWriter.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

搜索代码

package bservlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;

import java.io.*;


public class SluceneSearcher extends HttpServlet {
private String indexpath="D:/workspace/testsearch2/indexes";
public void doPost(HttpServletRequest request,HttpServletResponse response){
StringBuffer sb=new StringBuffer("");
try {
request.setCharacterEncoding("GBK");
String phrase=request.getParameter("phrase");
IndexSearcher searcher;
searcher = new IndexSearcher(indexpath);
Term t=new Term("content",phrase);
Query q=new TermQuery(t);
Hits hs=searcher.search(q);
int num=hs.length();
sb.append("<h1>您搜索到的记录数:"+num+"</h1>");
for (int i=0;i<num;i++){
Document doc=hs.doc(i);
if (doc==null){
continue;
}
Field field=doc.getField("name");
String name="<br>name:<br>"+field.stringValue();
field=doc.getField("path");
String path="<br>path:<br>"+field.stringValue();
sb.append(name);
sb.append(path);
}
searcher.close();
} catch (CorruptIndexException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter out;
try {
response.setContentType("text/html;charset=GBK");
out = response.getWriter();
out.print(sb.toString());
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void doGet(HttpServletRequest request,HttpServletResponse response){
doPost(request,response);
}

}

搜索网页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>搜索例子</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="deepfuturesou">
搜索关键字
<input name="phrase" type="text" id="phrase" />
<input type="submit" name="Submit" value="搜索" />
</form>
</body>
</html>

2
0
分享到:
评论

相关推荐

    Lucene简单应用

    - 通过一个简单的示例来展示如何使用Lucene创建索引以及如何执行基本的全文搜索操作。 #### 三、Lucene内置Query对象 - Lucene提供了多种查询对象,如TermQuery、BooleanQuery、PhraseQuery等,这些对象用于构造...

    搜索篇:Struts、Lucene的Web实例

    2. **Lucene入门**:解释Lucene的核心概念,如Document、Field、Analyzer和IndexWriter,以及如何建立和管理索引。 3. **Struts与Lucene整合**:演示如何在Struts的Action类中调用Lucene进行搜索,以及如何在JSP页面...

    lucene学习资料

    一份适合初学者的Lucene入门指南,可能包含基本的安装步骤、简单的索引和搜索示例,帮助读者快速上手。 8. **lucene性能.txt** 这份文档可能涉及Lucene的性能优化技巧,包括索引存储、内存管理、查询效率等方面的...

    nutch+lucene开发自己的搜索引擎ch3.pdf

    - **标题与描述**: 本资料“nutch+lucene开发自己的搜索引擎ch3.pdf”聚焦于介绍如何使用Nutch和Lucene来构建自己的搜索引擎,特别强调了第三章:开源搜索引擎入门。 - **标签与内容**: 标签为“Nutch”,表明主要...

    一个专业搜索公司关于lucene和solar资料

    - **你也可以做搜索引擎**:提供了入门指南,鼓励读者尝试构建自己的搜索引擎。 ##### 第2章 遍历搜索引擎技术 - **网络蜘蛛**:介绍网络爬虫的基本原理和技术要点。 - **全文索引结构**:深入分析全文索引的数据...

    第一个Lucene 3.6 (3.X) 入门实例

    创建一个简单的Lucene索引是入门的关键。首先,定义一个文档类,用于封装你要索引的数据。例如,你可以创建一个`Document`对象,包含标题、内容、作者等字段。然后,使用`Field`类来指定每个字段的类型,如`...

    Lucene初级教程.doc

    标题为“Lucene初级教程.doc”,我们可以推断这是一份关于Lucene的入门级文档,Lucene是Apache软件基金会的一个开放源代码项目,它是一个全文搜索引擎库,广泛应用于Java开发中,用于实现高效、可扩展的信息检索服务...

    solr_开发入门例子

    - 启动:运行Solr服务器,可以通过Web界面(默认为`http://localhost:8983/solr/`)进行管理。 3. **创建索引** - 导入数据:使用Solr的`bin/post`命令或SolrJ库,将数据导入到Solr索引中。例如,如果你有MySQL...

    solr学习入门教程

    Solr利用Lucene的核心技术实现了更高级的功能,如Web服务接口、多语言支持、分布式搜索等。因此,虽然它们之间存在依赖关系,但Solr提供了比Lucene更为丰富的特性和更易于集成的解决方案。 #### 三、Solr与Lucene的...

    solr技术总结

    - **易用性**:提供了Web管理界面,便于配置和监控。 - **可配置性强**:可以根据业务需求自定义字段类型、分析器等。 #### 四、Solr与Lucene的区别 - **Lucene**:核心是一个搜索引擎库,提供索引和查询功能,但...

    Java基础入门教程

    ### Java基础入门教程知识点概述 #### 一、Java语言简介 - **定义**: Java是一种广泛使用的面向对象编程语言,由Sun Microsystems(后被Oracle收购)于1995年发布。它结合了C++的优点,并摒弃了一些复杂难懂的概念...

    入门搜索引擎(原码+环境jar包+运行说明文档)

    搜索引擎是信息检索的重要工具,本资源提供了一个入门级的搜索引擎实现,涵盖了网络爬虫和基于Lucene的搜索索引构建。下面将详细讲解其中涉及的关键技术。 首先,我们来看看网络爬虫部分。网络爬虫是一种自动抓取...

    nutch入门实例教程.pdf

    ### Nutch 入门实例教程知识点总结 #### 1. Nutch 简介 - **定义**: Nutch 是一个开源的 Java 实现的搜索引擎框架,它提供了构建和运行自己搜索引擎所需的所有工具。 - **研究动机**: - **透明度**: 作为开源...

    Elasticsearch入门视频教程下载

    - **Kibana**:提供Web界面进行数据可视化展示。 #### 八、学习资源推荐 - **官方文档**:最权威的学习资源,覆盖所有版本的详细介绍。 - **在线课程**:如Coursera、Udemy等平台提供的付费或免费课程。 - **社区...

    LucidWorks for Solr

    - **Solr 与 Lucene**:Solr 是基于 Lucene 的企业级搜索服务器,提供了更高级的功能和更易于使用的界面。Lucene 是 Java 的全文检索库,而 Solr 则是构建在 Lucene 上的应用服务器,提供了更强大的功能如分布式索引...

    ElasticStack(ELK)从入门到实践.pdf

    Kibana的安装和配置相对简单,用户可以通过安装和启动Kibana,访问其Web界面,通过数据探索功能来操作数据和创建仪表盘。同时,Kibana也提供了开发者工具,方便开发者进行定制开发。 Logstash是Elastic Stack中的...

Global site tag (gtag.js) - Google Analytics