Lucene:使用当前的Lucene实现的增删改查

1.声明

当前内容主要用于学习和复习,内容主要为使用Lucene实现增删改查操作

2.创建基本的接口、实体类和工具

1.可以Lucene化的接口

/**
 * @description 可以转换为Document的类的能力
 * @author hy
 * @date 2020-06-08
 */
public interface LuceneAble {
	Document convertToDocument();
}

2.工具类用于循环转换当前的实体类为Document

/**
 * @description 转换为Lucene的工具类
 * @author hy
 * @date 2020-06-08
 */
public class LuceneUtils {
	public static <T> List<Document> convertToDocuments(List<? extends LuceneAble> list) {
		List<Document> documents = new ArrayList<Document>(list.size());
		for (LuceneAble t : list) {
			Document doc = t.convertToDocument();
			documents.add(doc);
		}
		return documents;
	}
}

3.创建实体类

/**
 * @description 一般的实体类
 * @author hy
 * @date 2020-06-08
 */
public class Person implements LuceneAble {
	private String id;
	private String name;
	private Double salary;
	private Integer age;

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getSalary() {
		return salary;
	}

	public void setSalary(Double salary) {
		this.salary = salary;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Person(String id, String name, Double salary, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.age = age;
	}

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public Document convertToDocument() {

		Document doc = new Document();
		doc.add(new StringField("id", this.getId(), Store.YES));
		doc.add(new StringField("name", this.getName(), Store.YES));
		doc.add(new StringField("salary", this.getSalary() + "", Store.YES));
		doc.add(new StringField("age", this.getAge() + "", Store.YES));
		return doc;

	}

}

3.创建基本的测试类完成操作

/**
 * @description 一个基本的demo用于实现基于Lucene的增删改查
 * @author hy
 * @date 2020-06-08
 */
public class LucenePersonTest {
	public static void main(String[] args) {
		try {
			String indexPath = "D:\\eclipse-workspace\\Lucene-Start\\personIndexs";
			// 初始化当前的数据
			// initWriteIndexs(indexPath);
			ArrayList<Document> findDocuments = null;
			// 查询,需要使用正则方式匹配
			// findDocuments = findDocuments(indexPath, "name", "张*");
			// printDocuments(findDocuments);
			// 按照id查询直接查询
			// findDocuments = findDocuments(indexPath, "id", "1002");
			// printDocuments(findDocuments);
			// 查询所有
			// findDocuments = findDocuments(indexPath, "id", "1*");
			// printDocuments(findDocuments);
			// 删除
			//deleteDocumentByFiled(indexPath, "name", "张三");
			//findDocuments = findDocuments(indexPath, "id", "1*");
			//printDocuments(findDocuments);
			// 添加数据
			//addDocument(indexPath, new Person("1006", "张老四", 99.9, 18));
			//findDocuments = findDocuments(indexPath, "id", "1*");
			//printDocuments(findDocuments);
			// 修改数据
			 findDocuments = findDocuments(indexPath, "id", "1*");
			 printDocuments(findDocuments);
			 //updateDocument(indexPath, "1006", new Person("1006", "哈哈", 666.6, 66));
			 //findDocuments = findDocuments(indexPath, "id", "1006");
			 //printDocuments(findDocuments);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 打印显示数据
	public static void printDocuments(List<Document> docs) {
		for (Document document : docs) {
			List<IndexableField> fields = document.getFields();
			for (IndexableField field : fields) {
				System.out.println(field.name() + ":" + field.stringValue());
			}
			System.out.println("=============================");
		}
	}

	// 添加数据的方法
	public static void addDocument(String indexPath, Person p) throws IOException {
		IndexWriter indexWriter = createIndexWriter(indexPath);
		indexWriter.addDocument(p.convertToDocument());
		indexWriter.commit();
		indexWriter.close();
	}

	// 删除数据的方法(按照字段相同方式删除数据)
	public static void deleteDocumentByFiled(String indexPath, String filedName, String textValue) throws IOException {
		IndexWriter indexWriter = createIndexWriter(indexPath);
		indexWriter.deleteDocuments(new Term(filedName, textValue));
		indexWriter.close();
	}

	// 修改数据的方法,按照id修改Person数据
	public static void updateDocument(String indexPath, String id, Person newP) throws IOException {
		IndexWriter indexWriter = createIndexWriter(indexPath);
		indexWriter.updateDocument(new Term("id", id), newP.convertToDocument());
		indexWriter.close();
	}

	// 查询数据
	public static ArrayList<Document> findDocuments(String indexPath, String findFiled, String findValue)
			throws IOException, ParseException {
		Directory dir = FSDirectory.open(Paths.get(indexPath));
		DirectoryReader directoryReader = DirectoryReader.open(dir);
		IndexSearcher indexSearcher = new IndexSearcher(directoryReader);

		//
		CJKAnalyzer cjkAnalyzer = new CJKAnalyzer();
		QueryParser queryParser = new QueryParser(findFiled, cjkAnalyzer);
		Query query = queryParser.parse(findValue);

		// 开始查找name中具有张的文档
		TopDocs topDocs = indexSearcher.search(query, 50);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;

		TotalHits totalHits = topDocs.totalHits;
		System.out.println("查找的数量为:" + totalHits);
		System.out.println("开始显示找到的数量===>");

		ArrayList<Document> findDocuments = new ArrayList<Document>(scoreDocs.length);
		for (int i = 0; i < scoreDocs.length; i++) {
			// 获取文档索引
			int docID = scoreDocs[i].doc;
			System.out.println("doc===>" + docID);
			Document document = indexSearcher.doc(docID);
			findDocuments.add(document);
		}
		directoryReader.close();
		dir.close();
		return findDocuments;

	}

	public static IndexWriter createIndexWriter(String indexPath) throws IOException {
		Directory dir = FSDirectory.open(Paths.get(indexPath));
		IndexWriterConfig conf = new IndexWriterConfig(new CJKAnalyzer());
		IndexWriter indexWriter = new IndexWriter(dir, conf);
		return indexWriter;
	}

	// 写入数据文件
	public static void initWriteIndexs(String indexPath) throws IOException {
		IndexWriter indexWriter = createIndexWriter(indexPath);

		// 创建document
		List<Document> documents = getDocuments();
		indexWriter.addDocuments(documents);
		indexWriter.commit();
		indexWriter.close();
	}

	public static List<Document> getDocuments() {
		List<Person> persons = getPersons();
		List<Document> documents = new ArrayList<Document>(persons.size());
		for (int i = 0; i < persons.size(); i++) {
			Person person = persons.get(i);
			Document doc = person.convertToDocument();
			documents.add(doc);
		}
		return documents;
	}

	public static List<Person> getPersons() {
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("1001", "张三", 100.0, 12));
		persons.add(new Person("1002", "李四", 100.0, 13));
		persons.add(new Person("1003", "王五", 100.0, 16));
		persons.add(new Person("1004", "张老二", 100.0, 15));
		return persons;
	}

}

初始化
在这里插入图片描述

1.测试查询
1.1 正则查询
在这里插入图片描述
1.2 id查询(等价查询)
在这里插入图片描述
1.3 查询所有
在这里插入图片描述

2.测试添加
在这里插入图片描述
此时发现当前的数据发生变化:
在这里插入图片描述

3.测试修改
在这里插入图片描述
此时发现数据改变:
在这里插入图片描述
4.测试删除
在这里插入图片描述
发现存在修改:
在这里插入图片描述

所有测试都已经完成

发现只要发生修改操作,当前的索引中的数据内容就会发生改变!

4.总结

1.当前的查询操作可以通过使用正则表达式来实现查询操作,可以使用*方式,但是如果使用不正确的那么就会报错

2.感觉那个Trem类就像一个过滤匹配器一样,用于匹配删除(等价匹配)

以上纯属个人见解,如有问题请联本人!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章