lucene 5可以运行的demo

简介:
复制代码
package hello;

import java.io.IOException;

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.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class Lucene5_5_5_demo {

    public static void main(String[] args) throws IOException, ParseException {
        Analyzer analyzer = new StandardAnalyzer();

        // Store the index in memory:
        Directory directory = new RAMDirectory();
        // To store an index on disk, use this instead:
        //Directory directory = FSDirectory.open("/tmp/testindex");
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter iwriter = new IndexWriter(directory, config);
        Document doc = new Document();
        String text = "This is the text to be indexed.";
        doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
        iwriter.addDocument(doc);
        iwriter.close();
        
        // Now search the index:
        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher isearcher = new IndexSearcher(ireader);
        // Parse a simple query that searches for "text":
        QueryParser parser = new QueryParser("fieldname", analyzer);
        Query query = parser.parse("text");
        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
        assert(1 == hits.length);
        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
          Document hitDoc = isearcher.doc(hits[i].doc);
          System.out.println(hitDoc.get("fieldname"));
        }
        ireader.close();
        directory.close();
    }

}
复制代码

 











本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6600685.html,如需转载请自行联系原作者

相关文章
|
30天前
|
Java
elasticsearch使用java程序添加删除修改
elasticsearch使用java程序添加删除修改
9 0
|
7月前
|
自然语言处理 搜索推荐 Java
Lucene简单使用
Lucene简单使用
48 0
|
JSON Java API
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(上)
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作 (上)
1139 0
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(上)
|
Java 索引
【ElasticSearch实战】——java操作ES基本查询在项目中应用
【ElasticSearch实战】——java操作ES基本查询在项目中应用
879 0
【ElasticSearch实战】——java操作ES基本查询在项目中应用
|
SQL 分布式计算 Java
Kudu 使用_Java API_扫描 | 学习笔记
快速学习 Kudu 使用_Java API_扫描
119 0
Kudu 使用_Java API_扫描 | 学习笔记
|
Java API 索引
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(下)
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(下)
Elasticsearch——使用Java API实现ES中的索引、映射、文档操作(下)
|
自然语言处理 Java 索引