luncene RAMDirectory

package com.robert.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.util.concurrent.Callable;

/**
 * Created  on 15/8/21.
 */
public class SearchArray {

    public static void main(String[] args) {

        try {

            Directory directory = new RAMDirectory();
            Analyzer analyzer = new SimpleAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            IndexWriter writer = new IndexWriter(directory, iwc);
            String[] docs = {
              "a b c d e",
              "a b c d e f g h",
              "a b c d e f g h i j",
              "a c e",
              "e c a",
              "a c e a c e",
              "a c e a b c",
            };

            for (int j = 0; j < docs.length; ++j) {
                Document d = new Document();
                d.add(new TextField("contents", docs[j], Field.Store.YES));
                writer.addDocument(d);
            }
            writer.close();

            IndexReader reader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(reader);

            //搜索contents字段
            QueryParser parser = new QueryParser("contents", analyzer);

            TopDocs topDocs = null;
            String[] queries = {
                    "a c e",
                    "h i"

            };
            for( int j = 0; j < queries.length; ++j) {
                Query query = parser.parse(queries[j]);
                System.out.println(" query:" + query.toString("contents"));
                topDocs = indexSearcher.search(query,100);
                System.out.println("总共匹配多少个:" + topDocs.totalHits);

                for(int i = 0; i < topDocs.totalHits; ++i) {
                    Document d = indexSearcher.doc(topDocs.scoreDocs[i].doc);
                    System.out.println(i + " " + topDocs.scoreDocs[i].score + d.get("contents"));
                }

                System.out.println("-------------");

            }

        } catch (Exception e) {

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值