1. 程式人生 > >從Lucene 4.10.3到Lucene 7.1.0:帶你瞭解版本之間的些許差異

從Lucene 4.10.3到Lucene 7.1.0:帶你瞭解版本之間的些許差異

一.環境說明:
※Windows 10 v1709
※IDEA 2017.2.6
※JDK 1.8.0_144
※Lucene 7.1.0
二.哪裡有差異
因本人能力有限,所以也只是放出我在更新版本的時候,專案中用到的需要升級的地方,其他還未研究.
所以,本文主要從

  • NumericRangeQuery
  • BooleanQuery
  • Directory
  • IndexWriterConfig

這幾個方面來進行說明.

三.NumericRangeQuery
數字範圍查詢,一般用於價格等域的查詢

  /**
     * 測試數字範圍查詢
     *
     * @throws IOException 讀取索引庫異常
     */
@Test public void testNumericRangerQuery() throws IOException { /* 版本更新說明: 在lucene4.10中,關於數字範圍的查詢是使用的NumericRangerQuery 使用方式: Query query = NumericRangerQuery.newFloatRange(域名,較小值,較大值,是否包含較小值,是否包含較大值) 在lucene6.6中(不知最早在什麼版本,沒有細查),NumericRangerQuery被legacyNumericRangerQuery替代 在lucene7中(應該是從7開始的),lucene開始使用PointValue來替代之前的Filed.而數字範圍查詢方法也進行了修改, Float/Long/IntPoint.newRangeQuery取代了之前的NumericRangeQuery的newFloat/Long/IntRange 使用方法: Query query = Float.newRangeQuery(域名,較小值,較大值); 而這種查詢方法預設包含範圍的端點值,即查詢的是 較小值<=域名<=較大值 如果不想包含端點值: 1.不包含左端點值(即較小值):Query query = Float.newRangeQuery(域名,FloatPoint.nextUp(較小值),較大值); 2.不包含右端點值(即較大值):Query query = Float.newRangeQuery(域名,較小值,Float.nextDown(較大值)); 3.均不包含:結合1和2即可 */
Query query = FloatPoint.newRangeQuery("price", FloatPoint.nextUp(108F), FloatPoint.nextDown(488F)); doQuery(query); }

四.BooleanQuery
組合查詢,用於多個查詢條件組合起來一起參與查詢

 /**
     * 測試組合查詢
     *
     * @throws IOException 讀取索引庫異常
     */
    @Test
    public void testBooleanQuery() throws IOException {

        //新建兩條查詢
Query query1 = new TermQuery(new Term("description", "spring")); Query query2 = FloatPoint.newRangeQuery("price", FloatPoint.nextUp(50F), 110F); /* 1、MUST和MUST表示“與”的關係,即“交集”。 2、MUST和MUST_NOT前者包含後者不包含。 3、MUST_NOT和MUST_NOT沒意義 4、SHOULD與MUST表示MUST,SHOULD失去意義; 5、SHOUlD與MUST_NOT相當於MUST與MUST_NOT。 6、SHOULD與SHOULD表示“或”的概念。 版本更新說明: 在lucene 4.10.3中,組合查詢還有無參構造方法,可以通過下面這種方式實現組合查詢: Query query = new BooleanQuery() //新增查詢條件,並指定該條件的判斷級別 query.add(query1,Occur.MUST); query.add(query2,Occur.MUST); 在lucene7.1中,組合查詢只有一個有參構造方法,並沒有無參構造方法.而是多了一個靜態內部類Builder public static class Builder { private int minimumNumberShouldMatch; private final List<BooleanClause> clauses = new ArrayList<>(); //大概是4.10.3中的BooleanQuery的無參構造 public Builder() {} //設定最小需要匹配的數 public Builder setMinimumNumberShouldMatch(int min) { this.minimumNumberShouldMatch = min; return this; } public Builder add(BooleanClause clause) { if (clauses.size() >= maxClauseCount) { throw new TooManyClauses(); } clauses.add(clause); return this; } //4.10.3中的BooleanQuery的add方法,支援鏈式程式設計(一般使用這個add方法) public Builder add(Query query, Occur occur) { return add(new BooleanClause(query, occur)); } //返回一個BooleanQuery,用於構造Query public BooleanQuery build() { return new BooleanQuery(minimumNumberShouldMatch, clauses.toArray(new BooleanClause[0])); } } 7.1中,Occur.MUST等全都放到了BooleanClause中,所以,Occur.MUST等變成了BooleanClause.Occur.MUST等 所以在lucene中,組合查詢的使用方法: Query booleanQuery = new BooleanQuery.Builder().add(query1,BooleanClause.Occur.MUST).add(query2,Boolean.Occur.MUST).build(); */ Query booleanQuery = new BooleanQuery.Builder().add(query1, BooleanClause.Occur.MUST).add(query2, BooleanClause.Occur.MUST).build(); doQuery(booleanQuery); }

五.Directory

 /*
        版本變更說明:
        在4.10.3中,FSDirectory.open的引數是file物件,
        可能在6.6以後,開始使用Path類(java.nio.file)
        File物件轉Path的方法:
        File file = new File(檔案路徑);
        Path path = file.toPath();
         */
        File file = new File("D:\\bookIndex\\");
        Directory directory = FSDirectory.open(file.toPath());
        IndexWriter writer = new IndexWriter(directory, config);

六.

  /*
        可能從6.6開始,IndexWriterConfig就不在需要制定版本了
        而且有了無參構造方法,預設使用的分詞器就是StandardAnalyzer.
        即表示,如果你不想更改分詞器,就使用無參構造
         */
        IndexWriterConfig config = new IndexWriterConfig();

好了,大概就是這樣了.
我始終認為,IT從業人員應該經常進行自我更新,包括知識的更新,要保證自己能跟上技術的更新,才能更好的適應新時代的工作要求.

努力吧,少年.

2017/11/16
Lucifer