Lucene:爲前面的圖書的demo添加join查詢(QueryTimeJoin)

1.聲明

當前內容主要用於本人學習和複習,當前內容爲使用join查詢方式(將兩個索引庫進行連接操作)

當前內容借鑑:Lucene官方文檔

2.創建圖書銷售索引(bookSalesIndexs)

這裏直接關聯當前的圖書id編號,銷售中以圖書id編號進行標記(類似數據庫表中的外鍵)

創建當前的SaleRecord類

/**
 * @description 書籍的銷售記錄
 * @author hy
 * @date 2020-06-11
 */
public class SaleRecord implements LuceneAble {
	private String id;// 書籍的id編號
	private Integer num;// 銷售數量
	private Date saleDate;// 銷售時間
	private String buyer;// 購買人

	public String getId() {
		return id;
	}

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

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public Date getSaleDate() {
		return saleDate;
	}

	public void setSaleDate(Date saleDate) {
		this.saleDate = saleDate;
	}

	public String getBuyer() {
		return buyer;
	}

	public void setBuyer(String buyer) {
		this.buyer = buyer;
	}

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

	public SaleRecord(String id, Integer num, Date saleDate, String buyer) {
		super();
		this.id = id;
		this.num = num;
		this.saleDate = saleDate;
		this.buyer = buyer;
	}

	@Override
	public Document convertToDocument() {
		SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
		Document doc = new Document();
		doc.add(new StringField("id", this.getId(), Store.YES));
		doc.add(new StringField("num", this.getNum() + "", Store.YES));
		doc.add(new StringField("saleDate", format.format(this.getSaleDate()), Store.YES));
		doc.add(new StringField("buyer", this.getBuyer(), Store.YES));
		return doc;
	}

}

這裏直接使用前面的LuceneUtils這個工具類實現快速操作

String toPath ="D:\\eclipse-workspace\\Lucene-Start\\bookSalesIndexs";
public List<LuceneAble> getRecords(){
	List<LuceneAble> datas=new ArrayList<LuceneAble>();
	datas.add(new SaleRecord("1001",2,new java.util.Date(2020, 10, 10),"張三"));
	datas.add(new SaleRecord("1001",1,new java.util.Date(2020, 10, 12),"李四"));
	datas.add(new SaleRecord("1001",10,new java.util.Date(2020, 10, 10),"王五"));
	datas.add(new SaleRecord("1021",10,new java.util.Date(2020, 10, 10),"王五"));
	return datas;
}
@Test
public void initSaleReocrds() throws IOException {
	LuceneUtils.addDocuments(toPath, getRecords());
}

執行後的結果
在這裏插入圖片描述

3.開始使用當前的join查詢

String fromPath = indexPath;
String toPath ="D:\\eclipse-workspace\\Lucene-Start\\bookSalesIndexs";
// Query-time joins
@Test
public void QueryTimeJoinSearch() throws IOException {
	try (Directory fromDir = FSDirectory.open(Paths.get(fromPath));
			DirectoryReader fromDr = DirectoryReader.open(fromDir);
			Directory toDir = FSDirectory.open(Paths.get(toPath));
			DirectoryReader toDr = DirectoryReader.open(toDir);) {
		IndexSearcher fromSearcher = new IndexSearcher(fromDr);
		IndexSearcher toSearcher = new IndexSearcher(toDr);
		String fromField = "id"; // from表中關聯的字段
		boolean multipleValuesPerDocument = false; // Set only to true in the case when your fromField has multiple
													// values per document in your index
		String toField = "id"; // to表中關聯的字段
		ScoreMode scoreMode = ScoreMode.Max; // Defines how the scores are translated into the other side of the
												// join.
		Query fromQuery = new TermQuery(new Term("id", "1001")); // Query executed to collect from values
																			// to join to the to values

		Query joinQuery = JoinUtil.createJoinQuery(fromField, multipleValuesPerDocument, toField, fromQuery,
				fromSearcher, scoreMode);
		TopDocs topDocs = toSearcher.search(joinQuery, 10); // Note: toSearcher can be the same as the fromSearcher
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (int i = 0; i < scoreDocs.length; i++) {
			ScoreDoc scoreDoc = scoreDocs[i];
			int doc = scoreDoc.doc;
			System.out.println(doc);
		}
	}
}

當前查詢就是查詢id爲1001的書籍的銷售記錄

結果爲:
在這裏插入圖片描述
發現當前的結果就是記錄索引中的數據索引,發現結果是正確的

4.總結

1.通過測試當前的QueryTimeJoin方式發現,這個就類似兩張表外鍵的查詢,使用起來很方便(有mysql的使用起來非常友好)

2.可以跨索引庫方式實現查詢

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

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