Lucene 2.4.0 Range Query未按预期工作 [英] Lucene 2.4.0 Range Query is not working as expected

查看:134
本文介绍了Lucene 2.4.0 Range Query未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是带值的索引字段:

  EffectiveDate =1970
ExpirationDate =2035

创建索引和搜索的代码:

 public class IndexTest {

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

@BeforeClass
public static void createIndex()抛出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);

//示例dummy文档
Document doc = new Document();
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();

doc.add(新字段(EffectiveDate,1970,ds,IA));
doc.add(新字段(ExpirationDate,2035,ds,IA));

iwriter.addDocument(doc);
$ b $ doc1.add(新字段(EffectiveDate,1970,ds,IA));
doc1.add(新字段(ExpirationDate,2035,ds,IA));

iwriter.addDocument(doc1);

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

$ b @Test
public void testRangeQuery()抛出java.text.ParseException,Exception,IOException {


isearcher = new IndexSearcher的( E:// TMP / testindex /样品);

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


//以下查询不起作用
字符串rQuery =ExpirationDate:[2000 TO *];

MultiFieldQueryParser parser = new MultiFieldQueryParser(
new String [] {
EffectiveDate
,ExpirationDate},analyzer);
//parser.setDefaultOperator(QueryParser.Operator.OR);
parser.setAllowLeadingWildcard(true);
查询查询= 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 *] 

任何帮助都是非常可观的。提前感谢 解决方案

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



ExpirationDate:[2013-06-26 TO null]


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

解决方案

Below Range Query Solved my problem:-

ExpirationDate : [2013-06-26 TO null]

这篇关于Lucene 2.4.0 Range Query未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆