2013-06-27 59 views
1

下面是索引的字段值爲:的Lucene 2.4.0範圍查詢不按預期工作

EffectiveDate="1970" 
ExpirationDate="2035" 

代碼來創建索引和SEACH:

public class IndexTest{ 

static Analyzer analyzer = new StandardAnalyzer(); 
static IndexSearcher isearcher; 

@BeforeClass 
public static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException{ 
    Store s = Field.Store.YES; 
    Store ds = Field.Store.YES; 
    Index IA = Field.Index.ANALYZED; 
    Index INA = Field.Index.NOT_ANALYZED; 

    IndexWriter iwriter = new IndexWriter("C://tmp/testindex/sample", analyzer, true); 
    iwriter.setMaxFieldLength(25000); 

    //Sample dummy docs 
    Document doc = new Document(); 
    Document doc1 = new Document(); 
    Document doc2 = new Document(); 
    Document doc3 = new Document(); 

    doc.add(new Field("EffectiveDate", "1970", ds, IA)); 
    doc.add(new Field("ExpirationDate", "2035", ds, IA)); 

    iwriter.addDocument(doc); 

    doc1.add(new Field("EffectiveDate", "1970", ds, IA)); 
    doc1.add(new Field("ExpirationDate", "2035", ds, IA)); 

    iwriter.addDocument(doc1); 

    iwriter.optimize(); 
    iwriter.close(); 
} 

    @Test 
public void testRangeQuery() throws java.text.ParseException, Exception, IOException{ 


    isearcher = new IndexSearcher("E://tmp/testindex/sample"); 

     // String rQuery = " EffectiveDate : [* TO 1971 ]"; 
      // String rQuery = " EffectiveDate : [1960 TO 2000]"; 
      // String rQuery = " ExpirationDate : [2000 TO 2050]"; 


      //Below Query is Not Working 
      String rQuery = " ExpirationDate : [2000 TO *]"; 

    MultiFieldQueryParser parser = new MultiFieldQueryParser(
       new String[] { 
       "EffectiveDate" 
       ,"ExpirationDate"}, analyzer); 
     //parser.setDefaultOperator(QueryParser.Operator.OR); 
     parser.setAllowLeadingWildcard(true); 
     Query query = parser.parse(rQuery); 
     System.out.println("Str = "+rQuery); 
     System.out.println("query = "+query); 
     Hits hits = isearcher.search(query); 
     assertEquals(2, hits.length()); 
     for (int i = 0; i < hits.length(); i++) { 
      Document hitDoc = hits.doc(i); 
      System.out.println("hitDoc = "+hitDoc); 
      System.out.println(hitDoc.get("Code")); 
     } 
    System.out.println("1query = "+query); 

} 

邏輯: - 當前的日期應該是這兩個領域之間。

範圍以下的查詢工作: -

EffectiveDate : [ * TO 2013-06-26 ] 

範圍以下查詢不工作: -

ExpirationDate : [2013-06-26 TO *] 

任何幫助將是提前高度appreciable.Thanks

+1

你能告訴你如何索引這兩個字段?另外,你的意思是不工作? (太多命中?缺少命中嗎?無?引發異常?) – femtoRgon

+0

找不到文檔...但所有文檔值都在範圍 – nav0611

+0

因此,您是否能夠提供將這些字段添加到文檔中的代碼索引時? – femtoRgon

回答

0

以下範圍查詢解決了我的問題: -

到期日期:2013年6月26日爲null]

+0

這實際上是不正確的 - 請參閱下面的@femtoRgon的答案。 – George

1

核心Lucene的查詢分析器不接受開放性範圍(如ExpirationDate : [2013-06-26 TO *]),直到3.6版(見the related ticket)。

你的回答,使用ExpirationDate : [2013-06-26 TO null]可能會誤導你。 null不被視爲特殊的價值,而只是一個字!按照字母順序,標點符號(*)在數字2013之前,且數字在字母之前(null)(非常籠統地描述,訂購基於Unicode value,我相信)。

所以,雖然ExpirationDate : [2013-06-26 TO null]EffectiveDate : [ * TO 2013-06-26 ]做的工作,

既不ExpirationDate : [2013-06-26 TO *]也不EffectiveDate : [null TO 2013-06-26 ]意志。

在2.4.0版本中,您需要爲搜索提供什麼,需要提供足夠高的值和低的值來考慮搜索有效的開放式搜索,以滿足所有意圖和目的,例如:

+EffectiveDate : [0000-01-01 TO 2013-06-26 ] +ExpirationDate : [2013-06-26 TO 9999-12-31] 

使用類似:

+ExpirationDate : [2013-06-26 TO null] +EffectiveDate : [ * TO 2013-06-26 ] 

可能工作,但所有錯誤的原因。