超轻量级全文搜索框架的设计和实现

Lucene是Java领域最出色的全文搜索引擎,然而其API比较复杂,并且有严格的线程同步模型,直接使用不易。Compass则是封装了Lucene的一个OSEM:Object-SearchEngine Mapping,与Hibernate封装JDBC类似,然而过于复杂,支持的Lucene版本较低,在www.javaeedev.com发现雪峰开发一个类似Compass的简单封装Lucene的全文搜索框架,支持最新版本Lucene和Java 5泛型代码,用户通过简单的代码即可对自定义Bean进行搜索:

List<T> list = Searcher.search(Class<T>, String q, Page page);

下载地址:

http://code.google.com/p/lightweight-search/downloads/list

 

 

 

下面介绍下简单的用法:

 

1.自定义Bean,例如:Article

import com.javaeedev.lightweight.search.Index;
import com.javaeedev.lightweight.search.SearchableId;
import com.javaeedev.lightweight.search.SearchableProperty;
import com.javaeedev.lightweight.search.Store;

/**
 * Article bean that can be searched.
 *
 * @author Xuefeng
 */
public class Article {

    private String id;
    private String title;
    private String author;
    private String content;

    @SearchableId
    public String getId() {
        return id;
    }

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

    @SearchableProperty(store=Store.YES, index=Index.TOKENIZED, boost=3.0f)
    public String getTitle() {
        return title;
    }

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

    @SearchableProperty(store=Store.YES, index=Index.TOKENIZED, boost=2.0f)
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @SearchableProperty(store=Store.YES, index=Index.TOKENIZED)
    public String getContent() {
        return content;
    }

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

其中特别的地方就是@SearchableId和@SearchableProperty

@SearchableId是描述lucene索引的ID

@SearchableProperty是描述lucene索引域的

以上二属性的详细信息请查询lucene,应为此框架的原理仅仅是对lucene的封装。
2.实现全文全文搜索

有两种方法:

        一 直接用行,所需参数在调用类中配置,例如:

import com.javaeedev.lightweight.common.Page;
import com.javaeedev.lightweight.search.Searcher;
import com.javaeedev.lightweight.search.SearcherProvider;

/**
 * Run directly.
 * 
 */
public class Run {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        // define classes that can be searched:
        Set<Class> searchableClasses = new HashSet<Class>();
        searchableClasses.add(Article.class);
        // init a provider:
        SearcherProvider provider = new SearcherProvider("./index-location", searchableClasses);
        provider.setAnalyzer(new StandardAnalyzer());
        provider.setHighlightPre("<b>");
        provider.setHighlightPost("</b>");
        // get a searcher:
        Searcher searcher = provider.get();
        // now do all search work with "searcher" object:
        // first add new Articles:
        Article a = new Article();
        a.setId(UUID.randomUUID().toString());
        a.setTitle("How to use lightweight search?");
        a.setAuthor("Liao Xuefeng");
        a.setContent("Lightweight search project is an open source project that using Lucene but simplify full text search.");
        searcher.index(a);
        // now search:
        Page page = new Page(1);
        List<Article> articles = searcher.search(Article.class, "lightweight", page);
        System.out.println("Results: " + page.getTotalCount());
        for(Article article : articles) {
            System.out.println(article.getTitle() + " by " + article.getAuthor());
        }
    }

}

(注意:请注意其代码中的注释部分)

       二 借助于Guice注入(相关Guice的资料

    借助于Guice注入,首先要做的就是初始化Guice

import com.google.inject.Module;
import com.google.inject.name.Names;
import com.javaeedev.lightweight.search.Searcher;
import com.javaeedev.lightweight.search.SearcherProvider;

/**
 * Init Google Guice IoC Container.
 * 

 */
public class MyModule implements Module {

    @SuppressWarnings("unchecked")
    public void configure(Binder binder) {
        binder.bindConstant().annotatedWith(Names.named("HighlightPre")).to("<span class=\"highlight\">");
        binder.bindConstant().annotatedWith(Names.named("HighlightPost")).to("</span>");
        binder.bindConstant().annotatedWith(Names.named("IndexLocation")).to("./index-location");
        Set<Class> set = new HashSet<Class>();
        set.add(Article.class);
        binder.bind(new Key<Set<Class>>(Names.named("SearchableClasses")) {}).toInstance(set);
        binder.bind(Analyzer.class).to(StandardAnalyzer.class).asEagerSingleton();
        binder.bind(Searcher.class).toProvider(SearcherProvider.class).asEagerSingleton();
    }

}
其次创建调用

import java.util.List;
import java.util.UUID;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.javaeedev.lightweight.common.Page;
import com.javaeedev.lightweight.search.Searcher;

/**
 * Run demo with Google Guice as IoC container.
 *
 * @author Xuefeng
 */
public class RunWithGuice {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new MyModule());
        Searcher searcher = injector.getInstance(Searcher.class);
        // now do all search work with "searcher" object:
        // first add new Articles:
        Article a = new Article();
        a.setId(UUID.randomUUID().toString());
        a.setTitle("How to use lightweight search?");
        a.setAuthor("Liao Xuefeng");
        a.setContent("Lightweight search project is an open source project that using Lucene but simplify full text search.");
        searcher.index(a);
        // now search:
        Page page = new Page(1);
        List<Article> articles = searcher.search(Article.class, "lightweight", page);
        System.out.println("Results: " + page.getTotalCount());
        for(Article article : articles) {
            System.out.println(article.getTitle() + " by " + article.getAuthor());
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP(Active Server Pages)是一种动态网页开发技术,它使用VBScript或JScript等脚本语言作为编程语言,并通过IIS(Internet Information Services)作为Web服务器来运行。而ASP轻量级Web服务器则是指那些运行ASP网页的内置或独立服务器。 ASP轻量级Web服务器主要有以下几个特点: 首先,它具有简单而轻巧的特性。相比于传统的Web服务器如IIS,ASP轻量级Web服务器的安装和配置都非常简单,占用的系统资源也比较少。这使得它适合在资源有限的环境下使用,如个人电脑、嵌入式设备等。 其次,它具备高性能与快速的特点。由于ASP轻量级Web服务器的设计初衷是为了处理简单的动态网页请求,它通常会对网络请求进行高效的处理和优化,以提高网页的响应速度和用户体验。 此外,ASP轻量级Web服务器还提供了一些基本的功能和特性,如会话管理、服务器端脚本解析、数据库访问等,这些功能可以满足一般的Web开发需求。 最后,尽管ASP轻量级Web服务器功能较为简单,但它的易用性和灵活性十分出色。开发人员可以使用ASP轻量级Web服务器轻松地编写和调试ASP网页,并且它也支持与其他服务器软件进行集成,如Apache等。 总而言之,ASP轻量级Web服务器是一种简单而高效的Web服务器,它适用于一些资源有限的环境,同时也可以满足一般的Web开发需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值