Lucene 2.4.0 Range Query is not working as expecte

2019-08-01 00:14发布

Below are the indexed fields with value:

 EffectiveDate="1970"
 ExpirationDate="2035"

Code to create index and 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);

}

Logic :- current date should be between these two field.

Below Range query is working:-

EffectiveDate : [ * TO 2013-06-26 ]

Below Range query is not working:-

ExpirationDate : [2013-06-26 TO *] 

Any help would be highly appreciable.Thanks in advance

2条回答
爷的心禁止访问
2楼-- · 2019-08-01 00:25

Core Lucene query parser didn't accept open ended ranges (like ExpirationDate : [2013-06-26 TO *]) until Version 3.6 (see the related ticket).

Your answer, of using ExpirationDate : [2013-06-26 TO null] might mislead you down the road. null is not treated as a special value, but rather just a word! Lexicographically, punctuation (*) is before numerals 2013, and numerals are before letters (null) (Described very generically, Ordering is based on Unicode value, I believe).

So, while ExpirationDate : [2013-06-26 TO null] and EffectiveDate : [ * TO 2013-06-26 ] do work,

neither ExpirationDate : [2013-06-26 TO *] nor EffectiveDate : [null TO 2013-06-26 ] will.

In version 2.4.0, what you'll need to do to provide for your search, is to come up with adequately high and low values to consider the search effectively open-ended, for all intents and purposes, such as:

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

Using something like:

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

May work, but for all the wrong reasons.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-08-01 00:46

Below Range Query Solved my problem:-

ExpirationDate : [2013-06-26 TO null]

查看更多
登录 后发表回答