Lucene学习

Lucene学习

一、创建索引

public class LuenceIndex {
    /**
     * 1.首先创建索引关键类
     * IndexWriter indexWrite=new IndexWriter(directory,indexWriterConfig);
     * 2.Directory(索引的存储位置)
     * Directory directory = FSDirectory.open(new File("D://index/testindex"));
     * 3.Analyzer(标准的分词技术)
     * Analyzer anlyzer=new StandardAnalyzer(Version.Lucene_43);
     * 4.Document(一个文档有很多个域)
     * Document document=new Document();
     * 5.Field(域)
     * doc.add(new TextField("fieldname","测试标题",Store.YES));
     */
    public static void main(String[] args) {
        //创建标准分词技术
        Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
        //IndexWriter配置信息
        IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_46, analyzer);
        //创建索引的打开方式(没有就新建,有就打开)
        indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        Directory directory=null;
        IndexWriter indexWriter=null;
        try {
            directory=FSDirectory.open(new File("D://index/testindex"));
            if(indexWriter.isLocked(directory)){
                indexWriter.unlock(directory);
            }
            indexWriter=new IndexWriter(directory, indexWriterConfig);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Document doc1=new Document();
        doc1.add(new StringField("id", "adcns", Store.YES));
        doc1.add(new TextField("content", "急速漂流",Store.YES));
        doc1.add(new IntField("num", 1, Store.YES));
        //写入索引
        try {
            indexWriter.addDocument(doc1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Document doc2=new Document();
        doc2.add(new StringField("id", "abc", Store.YES));
        doc2.add(new TextField("content", "开始了",Store.YES));
        doc2.add(new IntField("num", 2, Store.YES));
        //写入索引
        try {
            indexWriter.addDocument(doc2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            indexWriter.commit();
            indexWriter.close();
            directory.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

二、索引检索关键类

public class LuceneRead {
/**
 * 1.DirectoryReader(读取硬盘上的索引文件)
 * DirectoryReader dReader=DirectoryReader.open(directory);
 * 2.IndexSearcher(提供索引的检索方法)
 * IndexSearcher indexSearcher=new IndexSearcher(dReader);
 * 3.Query(查询条件的创建)
 * QueryParser query=new QueryParser(Version.LUCENE_46, field, analyzer);
 * Query query=parser.parse("空间向量");
 * 4.TopDocs(保存检索的结果)
 * TopDocs docs=indexSearcher.search(query, 10);
 * 
 */
    public static void main(String[] args) {
        Directory directory=null;
        try {
            directory=FSDirectory.open(new File("D://index/testindex"));
            //读取这个索引文件
            DirectoryReader dReader=DirectoryReader.open(directory);
            //创建索引的检索方法
            IndexSearcher indexSearcher=new IndexSearcher(dReader);
            //创建分词技术,要和创建索引的一样
            Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
            //创建查询字符串
            //field(搜索域)
            QueryParser parser=new QueryParser(Version.LUCENE_46, "content", analyzer);
            //搜索词
            Query query=parser.parse("开始");
            //检索前十条
            TopDocs docs=indexSearcher.search(query, 10);
            //循环输出
            if(docs!=null){
                System.out.println("符合条件的总数为"+docs.totalHits);
                for(int i=0;i<docs.scoreDocs.length;i++){
                    Document document=indexSearcher.doc(docs.scoreDocs[i].doc);
                    System.out.println("id="+document.get("id"));
                    System.out.println("content="+document.get("content"));
                    System.out.println("num="+document.get("num"));
                }
            }
            directory.close();
            dReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

三、各种分词技术(Analyzer)

public class AnalyzerStudy {
    private static String str="武汉麦达,Lucence 学习";
/**
 * 
 */
    public static void printAnalyzer(Analyzer analyzer){
         StringReader reader=new StringReader(str);
         try {
            TokenStream ts=analyzer.tokenStream("", reader);
            ts.reset();
            //获取分词结果的CharTermAttribute
            CharTermAttribute attribute=ts.getAttribute(CharTermAttribute.class);
            //打印分词技术
            System.out.println(analyzer.getClass());
            //循环输出分词劫夺
            while(ts.incrementToken()){
                System.out.println(attribute.toString()+"|");
            }
            System.out.println();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    public static void main(String[] args) {
        //创建分词器
        Analyzer analyzer=null;
        //标准分词器
        analyzer=new StandardAnalyzer(Version.LUCENE_46);
        printAnalyzer(analyzer);

        //空格分词
        analyzer=new WhitespaceAnalyzer(Version.LUCENE_46);
        printAnalyzer(analyzer);
        //简单分词(一句话就是一个词,遇到标点,空格分开)

        analyzer=new SimpleAnalyzer(Version.LUCENE_46);
        printAnalyzer(analyzer);

        //二分分词
        analyzer=new CJKAnalyzer(Version.LUCENE_46);
        printAnalyzer(analyzer);
        //关键字分词
        analyzer=new KeywordAnalyzer();
        printAnalyzer(analyzer);

        //被忽略词分词器
        analyzer=new StopAnalyzer(Version.LUCENE_46);
        printAnalyzer(analyzer);
    }
}

四、 Query子类

public class QueryStudy {
/**
 * 1.QueryParser(使用单个域搜索时,指定具体的域名和分词方法)
 * QueryParser parser =new QueryParser(Version.LUCENE_46, field,analyzer);
 * Query query=parser.parse(key);
 * 2.MultiFieldQueryParser(多个域搜索时)
 * MultiFieldQueryParser parser=new MultiFieldQueryParser(Version.LUCENE_46, fields, analyzer);
 * Query query=parser.parse(key);
 *
 * 3.TermQuery(按关键词搜索)
 *  Query query=new TermQuery(new Term(field, key));
 * 
 * 4.PrefixQuery(使用前缀搜索,前缀指定若干个相同)
 * PrefixQuery query=query = new PrefixQuery(new Term(field, key);
 * 
 * 5.PhraseQuery(短语搜索,可以指定关键词之间的最大距离)
 *  PhraseQuery query = new PhraseQuery();
 *  query.setSlop(2);//两个关键词之间的最大距离不能超过2
 *  query.add(new Term("content","lucene"));
 *  query.add(new Term("content","训练"));
 * 6.WildcardQuery(通配符搜索)
 * 表示0到多个字符
   ? 表示一个单一的字符
   WildcardQuery query=new WildcardQuery(new Term(“content”,”?qq*”));
    7.TermRangeQuery(在某一范围内搜索)
    域的上限,域的下限,是否包含上限,是否包含下限
    TermRangeQuery query=new TermRangeQuery(field, "ab", "af", true, false);
    8.NumericRangeQuery(数据范围内搜索)
    NumericRangeQuery query=NumericRangeQuery.newIntRange("start", 0, 3 ,false, false);
    NumericRangeQuery query=NumericRangeQuery.newDoubleRange("start", 0.01, 0.62,false, false);

    9.BooleanQuery(当需要把多个query对象组合成一个时,此query可以嵌套非常复杂的查询)
    query=new BooleanQuery();
    query.add(new Term("contetnt", "基于"),BooleanClause.Occur.MUST);
    query.add(new Term("contetnt", "Lucene"),BooleanClause.Occur.MUST_NOT);
    1.和 MUST与MUST_NOT 
    2.或 SHOULD与SHOULD
    3.A与B的并集-B  MUST与MUST_NOT

    IndexSearch(检索关键类)
    主要用这两个方法
    search(Query query,int n);
    search(Query query,int n, Sort sort);

 */
    public static void main(String[] args) {



    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值