1

以下は、値を持つインデックス付きフィールドです。

 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);

}

ロジック :- 現在の日付は、これら 2 つのフィールドの間にある必要があります。

以下の範囲のクエリが機能しています:-

EffectiveDate : [ * TO 2013-06-26 ]

以下の範囲のクエリが機能していません:-

ExpirationDate : [2013-06-26 TO *] 

事前に感謝します。

4

1 に答える 1

1

ExpirationDate : [2013-06-26 TO *]コア Lucene クエリ パーサーは、バージョン 3.6 まで( のような) 制限のない範囲を受け入れませんでした(関連するチケットを参照してください)。

使用するというあなたの答えは、あなたをExpirationDate : [2013-06-26 TO null]誤解させるかもしれません。 nullは特別な値として扱われるのではなく、単なる単語として扱われます。辞書編集的には、句読点 ( *) は数字の前2013にあり、数字は文字 ( ) の前にあります (非常に一般的に説明すると、順序付けはUnicode 値nullに基づいていると思います)。

だから、仕事をしながら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 ]

うまくいくかもしれませんが、すべての間違った理由があります。

于 2013-06-28T16:03:36.713 に答える