Lucene 2.4.0范围查询未按预期工作

时间:2013-06-27 07:25:50

标签: full-text-search lucene range-query

以下是带有值的索引字段:

 EffectiveDate="1970"
 ExpirationDate="2035"

创建索引和搜索的代码:

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 *] 

任何帮助都会非常明显。谢谢提前

2 个答案:

答案 0 :(得分:1)

Core Lucene查询解析器在版本3.6之前不接受开放式范围(如ExpirationDate : [2013-06-26 TO *])(参见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 ]

可能有效,但出于所有错误的原因。

答案 1 :(得分:0)

低于范围查询解决了我的问题: -

ExpirationDate:[2013-06-26 TO null]

相关问题