Lucene学习入门1

一、概述
搜索的方式:
    1、只处理文本(不处理多媒体,多媒体是另外一个领域)
    2、不处理语义,而是按词查询
    3、对于英文,不区分大小写
Lucene(全文检索):
    指以文本作为检索对象,找出含有指定词汇的文本
    全面、准确和快速是衡量全文检索系统的关键指标
全文检索与数据库搜索
    1、全文检索的搜索效果更加准确
    2、相关排序,数据库没有(全文检索会把最符合要求的放在最前面)
    3、速度更快(采用索引检索,数据库中的select不能)
应用场景:
    1、系统内搜索(站内搜索)
    2、垂直搜索(利用爬虫等把部分网站的信息拿过来存到自己系统中)

    3、全网搜索(需要软件和硬件的配合,很好的算法等,不常用)
二、Lucene的简介
    全文检索就如同一个ORM,是一个概念,ORM的框架有很多种:Hibernate、TopLink、iBatis等。同样的,全文检索领域中也有很多种框架,Lucene就是其中的一个开源的全文检索框架。
    Lucene的主页为:http://lucene.apache.org/ .我现在使用的为3.0.1的版本。

三、做个小例子
   1、开发环境的配置
      加入jar包:
        lucene-core-3.0.1.jar(核心包)
 contrib\analyzers\common\lucene-analyzers-3.0.1.jar(分词器)
 contrib\highlighter\lucene-highlighter-3.0.1.jar(高亮)
 contrib\memory\lucene-memory-3.0.1.jar(高亮)
   2、先做个实体类,用来模拟数据

package com.lucene.entity;

public class ArticleEntity {

private Integer  id ;

private String title ;

private String content ;


public void setId(Integer id) {
this.id = id;
}


public Integer getId() {
return id;
}


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}



}
 

开始做lucene搜索:

package com.lucene.hello;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.testng.annotations.Test;

import com.lucene.entity.ArticleEntity;

public class HelloLucene {

@Test
public void createindex() throws Exception{

// 模拟一个存到了数据库中的文章对象
ArticleEntity article = new ArticleEntity();
article.setId(111);
article.setTitle("Hello Lucene !");
article.setContent("第一个lucene实例 ");

// 在索引库中建立索引
   // 1>> 把Article转为Document

Document doc = new Document();
doc.add(new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("title",article.getTitle(),Store.YES,Index.ANALYZED));
doc.add(new Field("content",article.getContent(),Store.NO,Index.ANALYZED));
   // 2>> 把Document添加到索引库中以建立索引
Directory d = FSDirectory.open(new File("./filepath"));
Analyzer analyzar = new StandardAnalyzer(Version.LUCENE_30);

IndexWriter indexWrite = new IndexWriter(d, analyzar, MaxFieldLength.LIMITED);
indexWrite.addDocument(doc);
indexWrite.close();
}
@Test
public void search() throws Exception{
// 搜索条件
String queryString = "Hello" ;

// 执行搜索
// =============================================================
Directory path = FSDirectory.open(new File("./filepath"));
Analyzer  a = new StandardAnalyzer(Version.LUCENE_30);
   // 1>> 把查询字符串转为Query对象
QueryParser queryp = new QueryParser(Version.LUCENE_30, "title", a) ;// 默认只在title中查询
Query query = queryp.parse(queryString);
  // 2>> 搜索,得到中间结果
IndexSearcher indexSearch = new IndexSearcher(path);
TopDocs topd = indexSearch.search(query, 1000);// 第1参数是查询条件,第2个参数是指定要返回前n条结果
int totalnum = topd.totalHits; //得到符合条件的总记录数
ScoreDoc[] sore = topd.scoreDocs; //返回前n条结果的信息

// >> 处理结果
List<ArticleEntity> list = new ArrayList<ArticleEntity>();
for(int i = 0 ; i < sore.length ;i++){
ScoreDoc scoreDoc = sore[i];
int id = scoreDoc.doc;//Document内部编号
float scoreNum = scoreDoc.score ;//与搜索条件符合的相关度

// 根据内部编号得到真正的Document数据
Document doc = indexSearch.doc(id);

// 把Document转为Article
ArticleEntity entity = new ArticleEntity();
entity.setId(Integer.parseInt(doc.get("id"))); //需要转型
entity.setTitle(doc.get("title"));
entity.setContent(doc.get("content"));
list.add(entity);
}
indexSearch.close();
// =============================================================

// 显示结果
for(ArticleEntity aEntity : list){
System.out.println("id:"+aEntity.getId());
System.out.println("标题:"+aEntity.getTitle());
System.out.println("内容:"+aEntity.getContent());
}
}

}
 

 

 

 

   

 

内容概要:本文围绕SecureCRT自动化脚本开发在毕业设计中的应用,系统介绍了如何利用SecureCRT的脚本功能(支持Python、VBScript等)提升计算机、网络工程等相关专业毕业设计的效率与质量。文章从关键概念入手,阐明了SecureCRT脚本的核心对象(如crt、Screen、Session)及其在解决多设备调试、重复操作、跨场景验证等毕业设计常见痛点中的价值。通过三个典型应用场景——网络设备配置一致性验证、嵌入式系统稳定性测试、云平台CLI兼容性测试,展示了脚本的实际赋能效果,并以Python实现的交换机端口安全配置验证脚本为例,深入解析了会话管理、屏幕同步、输出解析、异常处理和结果导出等关键技术细节。最后展望了低代码化、AI辅助调试和云边协同等未来发展趋势。; 适合人群:计算机、网络工程、物联网、云计算等相关专业,具备一定编程基础(尤其是Python)的本科或研究生毕业生,以及需要进行设备自动化操作的科研人员; 使用场景及目标:①实现批量网络设备配置的自动验证与报告生成;②长时间自动化采集嵌入式系统串口数据;③批量执行云平台CLI命令并分析兼容性差异;目标是提升毕业设计的操作效率、增强实验可复现性与数据严谨性; 阅读建议:建议读者结合自身毕业设计课题,参考文中代码案例进行本地实践,重点关注异常处理机制与正则表达式的适配,并注意敏感信息(如密码)的加密管理,同时可探索将脚本与外部工具(如Excel、数据库)集成以增强结果分析能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值