zoukankan      html  css  js  c++  java
  • Lucene 全文检索 Lucene的使用

    Lucene  全文检索  Lucene的使用

    一.简介:

    参考百度百科:

    http://baike.baidu.com/link?url=eBcEVuUL3TbUivRvtgRnMr1s44nTE70odpjF8VbUpg8z3o8u1mt2PLpP-WnLBZY7ifUNLHDUtKSAQDthiiIhIa

     

    二.使用:

    1.必备包:

    lucene有7个包需要导入:analysis,document,index,queryParser,search,store,util

    2.建立索引:

    主要使用类:IndexWriter

    主要使用函数:writer.AddDocument(doc);

    IndexWriter writer = new IndexWriter("E:/index", new StandardAnalyze(),true,MaxFieldLength.UNLIMITED); //true代表覆盖原先数据,maxFieldLength用来限制Field的大小
    
    Document doc = new Document();
    
    doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.ANALYZED,
    
    Field.TermVector.WITH_POSITIONS_OFFSETS));
    
    doc.add(new Field("time", "60", Field.Store.YES, Field.Index.ANALYZED,
    
    Field.TermVector.WITH_POSITIONS_OFFSETS));
    
    writer.addDocument(doc);
    
    writer.optimize(); //优化
    
    writer.close();

    3.检索:

    主要类:IndexSearcher

    主要函数:searcher.search(query);

    IndexSearcher searcher= new IndexSearcher("E:/index");
    
    Query query = new TermQuery(new Term("title", "lucene"));//单个字节查询
    
    //Query query = new FuzzyQuery(new Term("title", "lucene"));//模糊查询
    
    //Query query = new WildcardQuery(new Term("title", "lu*"));//通配符查询 ?代表一个字符,*代表0到多个字符
    
    //BooleanQuery query = new BooleanQuery();//条件查询
    
    //BooleanQuery qson1 = new BooleanQuery();
    
    //Query q1 = new TermQuery(new Term("title", "lucene"));
    
    //qson1.add(q1, Occur.MUST);//MUST是必须满足的
    
    //BooleanQuery qson2 = new BooleanQuery();
    
    //Query q2= new TermQuery(new Term("sex", "woman"));
    
    //qson2 .add(q2, Occur.MUST_NOT);//MUST_NOT是必须不满足
    
    //query.add(qson1, Occur.SHOULD);
    
    //query.add(qson2, Occur.SHOULD);//SHOULD代表满足qson1或者满足qson2都可以
    
    //PhraseQuery query = new PhraseQuery();//近距离查询
    
    //query.setSlop(5);//距离设置为5
    
    //query.add(new Term("title", "lucene"));
    
    //query.add(new Term("title", "introduction"));//查询出title中lucene和introduction距离不超过5个字符的结果
    
    //Query query = new PrefixQuery(new Term("title", "lu"));//WildcardQuery的lu*一样
    
    //RangeQuery query = new RangeQuery(new Term("time", "50"),new Term("time", "60"), true);
    
    //true代表[50,60],false代表(50,60)
    
    Hits hits = searcher.search(query);
    
    for (int i = 0; i < hits.length(); i++) {
    
    Document d = hits.doc(i);
    
    String title= d.get("title");
    
    System.out.print(title+ " ");
    
    }
    这样,基本上就可以使用了
    注:以上代码为lucene早些版本的写法。lucene3.02的写法有所改变。
  • 相关阅读:
    [转]C# 文本框只能输入数字
    [转]C# dataGridview 报“索引-1没有值”的解决办法
    配置<authorization>节(配置文件)
    [转]javascript的urlencode
    SSM框架搭建问题
    web server
    mysql 系列
    UI 框架、ORM、权限系统
    一个基于 .NET Core 2.0 开发的简单易用的快速开发框架
    DotNetty 版 mqtt 开源客户端 (MqttFx)
  • 原文地址:https://www.cnblogs.com/zlp520/p/4867678.html
Copyright © 2011-2022 走看看