1.聲明
當前內容主要用於本人學習和複習,當前內容爲一個非常簡單的查詢demo
主要解決方案
整數使用:IntPoint和LongPoint
小數使用:DoublePoint和FloatPoint
日期直接專函爲LongPoint即可完成
模糊查詢使用RegexpQuery可以實現
這裏隨便寫一個圖書類即可
具有字段:編號、價格、數量、出版日期、作者
然後藉助前面寫的工具類和接口來實現即可
2.創建Book類
/**
* @description 一個非常簡單的圖書類
* @author hy
* @date 2020-06-10
*/
public class Book implements LuceneAble {
private String id; // 圖書的編號
private String bookName; // 圖書的名稱
private Double price; // 圖書的價格
private Date publishDate; // 發行時間
private Integer storeNum; // 剩餘數量
private String author; // 作者
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public Integer getStoreNum() {
return storeNum;
}
public void setStoreNum(Integer storeNum) {
this.storeNum = storeNum;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String bookName, Double price, Date publishDate, Integer storeNum, String author) {
super();
this.id = id;
this.bookName = bookName;
this.price = price;
this.publishDate = publishDate;
this.storeNum = storeNum;
this.author = author;
}
@Override
public Document convertToDocument() {
Document doc = new Document();
doc.add(new StringField("id", this.getId(), Store.YES));
doc.add(new StringField("bookName", this.getBookName(), Store.YES));
doc.add(new DoublePoint("price", this.getPrice()));
doc.add(new LongPoint("publishDate", this.getPublishDate().getTime()));
doc.add(new IntPoint("storeNum", this.getStoreNum()));
doc.add(new StringField("author", this.getAuthor(), Store.YES));
return doc;
}
}
3.創建測試類
/**
* @description 一個非常簡單的demo,實現,範圍查詢,日期查詢、模糊查詢
* @author hy
* @date 2020-06-10
*/
public class BookTest {
String indexPath = "D:\\eclipse-workspace\\Lucene-Start\\booksIndexs";
public List<Book> getDatas() {
List<Book> books = new ArrayList<Book>();
books.add(new Book("1001", "java開發", 99.9, new Date(2009, 10, 12), 100, "java"));
books.add(new Book("2001", "c開發", 200.9, new Date(2011, 9, 12), 5, "c"));
books.add(new Book("3001", "python開發", 78.9, new Date(2013, 3, 12), 9, "python"));
books.add(new Book("1021", "go開發", 18.8, new Date(2018, 12, 12), 18, "go"));
books.add(new Book("1024", "android開發", 59.9, new Date(2020, 10, 12), 50, "java"));
return books;
}
@Test
public void init() throws IOException {
List<Book> datas = getDatas();
LuceneUtils.addDocuments(indexPath, datas);
}
// 查詢庫存小於50的書籍
@Test
public void queryStore() throws IOException, ParseException {
Query query = IntPoint.newRangeQuery("storeNum", 0, 50);
ArrayList<Document> list = LuceneUtils.findDocumentsByQuery(indexPath, query, 100);
LuceneUtils.printDocuments(list); // 測試成功
}
// 查詢價格在50 到100 元的書籍
@Test
public void queryPrice() throws IOException, ParseException {
Query query = DoublePoint.newRangeQuery("price", 50.0, 100.0);
ArrayList<Document> list = LuceneUtils.findDocumentsByQuery(indexPath, query, 100);
LuceneUtils.printDocuments(list);
}
// 查詢日期在2017-1-1到2020-12-12年的書籍
@Test
public void queryPublishDate() throws IOException, ParseException {
java.util.Date start = new java.util.Date(2017, 1, 1);
java.util.Date end = new java.util.Date(2020, 12, 12);
Query query = LongPoint.newRangeQuery("publishDate", start.getTime(), end.getTime());
ArrayList<Document> list = LuceneUtils.findDocumentsByQuery(indexPath, query, 100);
LuceneUtils.printDocuments(list); // 測試成功
}
// 查詢所有的數據
@Test
public void queryAll() throws IOException, ParseException {
ArrayList<Document> list = LuceneUtils.findAllDocuments(indexPath, "id", 100);
LuceneUtils.printDocuments(list); // 測試成功
}
// 模糊查詢
@Test
public void queryLike() throws IOException, ParseException {
ArrayList<Document> list = LuceneUtils.findDocumentsByQuery(indexPath, new RegexpQuery(new Term("author", "ja.*")), 100);
LuceneUtils.printDocuments(list); // 測試成功
}
}
結果:實現了各種操作,這裏就不顯示了
4.總結
1.當前的demo中實現了各種查詢操作,但是沒有使用排序操作
2.對於日期的查詢可以使用LongPoint來實現日期範圍查詢
3.對於價格可以使用DoublePoint來實現
4.對於分頁,這裏省略,因爲當前的Lucene自身沒有實現分頁操作,也沒有分頁接口,所以只能物理分頁
以上純屬個人見解,如有問題請聯本人!