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類就像一個過濾匹配器一樣,用於匹配刪除(等價匹配)

以上純屬個人見解,如有問題請聯本人!

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