lucene 案例 存

//注:先导入依赖的jar包

package com.bw.lucene;

import java.nio.file.Paths;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

//1.指向一个本地的目录存放索引  和 content 内容文件
//2.存放数据的时候解析器进行分词 
//3.因为我们是lucene文档搜索引擎  那么存放在文件夹中的东西应该是一个文档(document)
public class WriteDocument {
    static String path = "E://lucene"; // 在E盘中创建文件夹 lucene

    public static void main(String[] args) throws Exception {
        // 路径
        FSDirectory directory = FSDirectory.open(Paths.get(path));

        // 官方提供了一个标准的分词器 将文档中的每个字都分为一个关键字
        StandardAnalyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);

        // directory :存放文件夹路径 //config :分词器
        IndexWriter writer = new IndexWriter(directory, config);

        Artical art = new Artical();// 创建document对象
        art.setAuthor("nihao");
        art.setContent("this is hadoop");
        art.setId(123L);
        art.setTitle("hadoop");
        art.setUrl("www.baidu.com");

        // 假如我们要将一个文章的信息放入到lucene
        writer.addDocument(art.toDocument());// 将数据存放到磁盘中, 需要一个document对象
        writer.close();
    }

}

//创建document 对象 类

package com.bw.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

public class Artical {
    private Long id;
    private String title;
    private String content;
    private String author;
    private String url;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Artical(Long id, String title, String content, String author, String url) {
        super();
        this.id = id;
        this.title = title;
        this.content = content;
        this.author = author;
        this.url = url;
    }

    public Artical() {
        super();
    }

    @Override
    public String toString() {
        return "Artical [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author + ", url=" + url
                + "]";
    }

    public Document toDocument() {
        Document doc = new Document();
        // 这个Field用来构建一个Long数字类型Field 进行分词和索引
        // 这个field只能创建索引 在查询的时候可以根据这个字段查询 但是查询结果中 不存在这个字段
        // 索引表中存在id字段 但是查询内容的时候 不存在id字段
        doc.add(new LongPoint("id", this.id));// doucment对象性是一个lucene中 的字段对象
        doc.add(new StoredField("id", this.id));// 单另存储
        doc.add(new StringField("author", this.author, Store.YES));
        doc.add(new StoredField("url", this.url));
        doc.add(new TextField("content", this.content, Store.YES));
        doc.add(new TextField("title", this.title, Store.YES));
        return doc;

    }

}

//运行第一个类

在E盘中查看生成的结果   结果是二进制的所以看不懂

原文地址:https://www.cnblogs.com/JBLi/p/10898985.html