日期:2014-05-18  浏览次数:21013 次

Lucene.net 如何查询一个关键词 并且ID大于指定的数据
Lucene.net 如何查询一个关键词 并且ID大于指定的数据
我有数据 TITLE ID 两个字段 
索引是这样建立的
...略
  Document doc = new Document();
  doc.Add(new Field("title", ddr.GetString(0), Field.Store.NO, Field.Index.TOKENIZED));
  doc.Add(new Field("auto_id",ddr["auto_id"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
  writer.AddDocument(doc);
  System.Console.Out.WriteLine(ddr.GetString(0));
...略  

例如我现在查询一个关键词为"打印机" auto_id 大于1000的记录

我写的是
SortField sf_id = new SortField("auto_id", SortField.INT, true);  
  SortField[] sortField = new SortField[] { sf_id };
  Sort sort = new Sort(sortField);

  RangeFilter filter = new RangeFilter("auto_id", "1", "1000", true, true);  
  QueryParser q = new QueryParser("title", new StandardAnalyzer());
  Query query = q.Parse("打印机");
  //Query query = new RangeQuery(new Term("auto_id", "1"), new Term("auto_id", "1000000"), true);

  Hits myhit = mysea.Search(query,filter,sort);

但是无法查出相应数据
请问如何更改
先谢谢了

------解决方案--------------------
在Lucene中,没有数字这么一说。一切皆为文本。
在Lucene中,数字7, 71, 20的本来顺序是7, 20, 71,但Lucene的顺序是20, 7, 71他是按文本的顺序排列的,如果你想实现数字7, 20, 71,这样的文本顺序,方法是加前导"0"字符,如:007, 020, 071,那么Lucene出来的顺序就是auto_id:[001:100],出来的顺序就是007, 020, 071,转换成数字就是你想要的7, 20, 71。当你要用auto_id:[1 TO 10]分析时,他不是按RangeQuery.为你实习数字范围的查找,得要加前导“0”
Consider three numeric Fields whose values are 7, 71,and 20. Although their natural order is 7, 20, 71, their lexicographical order is20, 7, 71. A simple and common trick for solving this inconsistency is to prepadnumeric Fields with zeros, like this: 007, 020, 071. Notice that the natural andthe lexicographical order of the numbers is now consistent。(Lucene in Action)

所以,您建立索引的字段
C# code
 
doc.Add(new Field("auto_id",ddr["auto_id"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));

------解决方案--------------------
C#代码和JAVA代码,可以互相CTRL + C, CTRL + V的