Lucene6.0基础教学

本文介绍了一个使用 Lucene 6.0 的简易示例,包括如何建立索引、搜索文档等基本操作。示例中创建了两个文章对象,并通过内存方式建立索引,演示了基于 ID 和内容的查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lucene6.0测试demo

这里是相关的实体类,没有相关的理论 需要的可以自行百度

public class Article {

    private Integer id ;
    private String name;
    private String content;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public Article() {
    }
    public  Article(Integer id,String name,String content) {
        this.content=content;
        this.id = id ;
        this.name = name ;
    }

}

话不多说 直接上代码 一个相当简单的测试用例 因为Lucene6.0的版本中有一点改变所以 我们直接将相关的索引建立在内存中的 当然也可以固定到磁盘中去

public static void main(String[] args) throws Exception {
        //創建内存對象
        Directory directory = new RAMDirectory();
        //创建分词器对象
        IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
        //创建indexwriter
        IndexWriter writer = new IndexWriter(directory, config);
        //创建节点对象
        Document document = new Document();
        Document document2 = new Document();
        //创建文章对象
        Article article = new Article(1, "Java学习技术", "21世纪最伟大的Java技术依然成为了世界人民关注的焦点");
        Article article2 = new Article(2, "PHP学习技术", "21世纪最伟大的Java技术依然成为了世界人民关注的焦点");
        //添加
        document.add(new Field("id", article.getId().toString(), TextField.TYPE_STORED));
        document.add(new Field("name", article.getName(), TextField.TYPE_STORED));
        document.add(new Field("content", article.getContent(), TextField.TYPE_STORED));
        document2.add(new Field("id", article2.getId().toString(), TextField.TYPE_STORED));
        document2.add(new Field("name", article2.getName(), TextField.TYPE_STORED));
        document2.add(new Field("content", article2.getContent(), TextField.TYPE_STORED));
        //添加索引
        writer.addDocument(document);
        writer.addDocument(document2);
        //关闭相应的写的方法
        writer.close();

        //读取相应的读
        DirectoryReader  directReader = DirectoryReader.open(directory);
        //得到查询
        IndexSearcher searcher = new IndexSearcher(directReader);

        //封装查询对象
        Query query = new TermQuery(new Term("id", "2"));

        Query query2 = new TermQuery(new Term("content", "人"));

        TopDocs search = searcher.search(query, 100);
        for (int i = 0; i < search.scoreDocs.length; i++) {
            // search.scoreDocs[i].doc 是获取索引中的标志位id, 从0开始记录    
            int doc = search.scoreDocs[i].doc;
            //得到查询对象
            Document doc2 = searcher.doc(doc);

            System.out.println( "------>"+doc2.getField("id"));
            System.out.println( "------>"+doc2.getField("name"));
            System.out.println( "------>"+doc2.getField("content"));
        }



    }

相关的pom文件

<dependency>  
            <groupId>org.apache.lucene</groupId>  
            <artifactId>lucene-core</artifactId>  
            <version>6.0.0</version>  
        </dependency>  

        <dependency>  
            <groupId>org.apache.lucene</groupId>  
            <artifactId>lucene-highlighter</artifactId>  
            <version>6.0.0</version>  
        </dependency>  

        <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->  
        <dependency>  
            <groupId>org.apache.lucene</groupId>  
            <artifactId>lucene-analyzers-common</artifactId>  
            <version>6.0.0</version>  
        </dependency>  

        <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-memory -->  
        <dependency>  
            <groupId>org.apache.lucene</groupId>  
            <artifactId>lucene-memory</artifactId>  
            <version>6.0.0</version>  
        </dependency>  

        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.9</version>  
        </dependency>  

        <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->  
        <dependency>  
            <groupId>org.apache.lucene</groupId>  
            <artifactId>lucene-queryparser</artifactId>  
            <version>6.0.0</version>  
        </dependency>  

希望对大家学习简单的Lucene入门有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值