Lucene的2.4.0范围查询工作不正常(Lucene 2.4.0 Range Query is

2019-10-18 07:40发布

下面是与价值的索引字段:

 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

Answer 1:

核心Lucene的查询分析器不接受开放性范围(如ExpirationDate : [2013-06-26 TO *]直到3.6版本(请参阅相关的票 )。

你的回答,使用ExpirationDate : [2013-06-26 TO null]可能会误导你的道路。 null不被视为一个特殊的价值,而只是一个字! 按字典,标点符号( * )是数字之前2013和数字是之前的字母( null )(形容很一般,排序基于Unicode值 ,我相信)。

因此,虽然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 ]

可能工作,但所有错误的原因。



Answer 2:

下面范围查询解决我的问题: -

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



文章来源: Lucene 2.4.0 Range Query is not working as expected