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);但是无法查出相应数据
请问如何更改
先谢谢了

解决方案 »

  1.   


    例如我现在查询一个关键词为"打印机" auto_id 大于1000的记录 SELECT * FROM TB WHERE ID>1000 AND TITLE LIKE '%打印机%'
    你这样查询看有没有数据
      

  2.   

    RangeFilter filter = new RangeFilter("auto_id", "1", "1000", true, true);
    是查1~1000的数据
    改为
    RangeFilter filter = new RangeFilter("auto_id", "1000", "100000", true, true);
    试试
      

  3.   

    Filter filter = new RangeFilter(FieldNumber, int.Parse("1"), int.Parse("1000"), true, true);
    Hits hits = searcher.Search(query, filter);
    参考
      

  4.   


    RangeFilter 的第二个和第三个参数是 string 的....
    参考里面我也是过了 建立索引和搜索的时候 使用了LongToString 也是无数据
      

  5.   

    doc.Add(new Field("title", ddr.GetString(0), Field.Store.NO, Field.Index.TOKENIZED)); 请把Store改为YES
      

  6.   

    QueryParser q = new QueryParser("title", new StandardAnalyzer());
    Query query = q.Parse("打印机"); 搜索title,必须是Store.YES,另外,如果还是没有结果,有可能是StandardAnalyzer对中文的支持有问题
      

  7.   

    搜索id是1000以上的记录,我的想法是范围取1-1000然后取反(前提是最小id号是1)
      

  8.   

    我也在找一个Luncene的问题,麻烦各位看一下吧!
    http://topic.csdn.net/u/20090916/14/0281240d-45ce-4ee5-a380-9ddaa22f015f.html?23083
      

  9.   

    问题依然没有解决 ....
    我将语句改成 Query query = q.Parse("auto_id:[1 TO 10]");
    出来数据只有两个..id 是1 的核id 是0 的
      

  10.   

    用 luke试了一下 auto_id:[1 TO 10] 出来数据也是两条 1 和 10 ..怎么搞的
      

  11.   

    在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)所以,您建立索引的字段
     
    doc.Add(new Field("auto_id",ddr["auto_id"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); 或许要改改,必须要加前导“0”//生成前导0的数字
    public class NumberUtils {
       private static final DecimalFormat formatter = new DecimalFormat("0000000000");
       public static String pad(int n) {
         return formatter.format(n);
       }
    }
    //调用示例
    {
    Document doc = new Document();
    while (!ddr.eof())
    {
    doc.add(new Field("auto_id", NumberUtils.pad(ddr["auto_id"].ToString()),    Field.Store.YES, Field.Index.UN_TOKENIZED);
        writer.addDocument(doc);
    }
    建立自己的QueryParser类:CustomQueryParser//主要方法是:getRangeQuery
    protected Query getRangeQuery(String field, Analyzer analyzer,
                                  String part1, String part2,
                                  boolean inclusive)
    throws ParseException {
    if ("id".equals(field)) {
      try {
           int num1 = Integer.parseInt(part1);
           int num2 = Integer.parseInt(part2);
          return new RangeQuery(
                    new Term(field, NumberUtils.pad(num1)),
                    new Term(field, NumberUtils.pad(num2)),
                    inclusive);
         } catch (NumberFormatException e) {
         throw new ParseException(e.getMessage());
      }
    }
    return super.getRangeQuery(field, analyzer, part1, part2,
    inclusive);
    }范围查找实现CustomQueryParser parser = new CustomQueryParser("field", analyzer);
    Query query = parser.parse("id:[37 TO 346]");
    assertEquals("padded", "id:[0000000037 TO 0000000346]",query.toString("field"));
    IndexSearcher searcher = new IndexSearcher(directory);
    Hits hits = searcher.search(query);
    自己把Lucene In Action 的6.3.3看看吧。
      

  12.   

    C#代码和JAVA代码,可以互相CTRL + C, CTRL + V的
      

  13.   


    我提个建议吧,
    不要把所有的东西都往Lucene里面放,他的索引库是有大小限制的。而且为了速度,尽量只放文本数据,
    如Title,和Content
    像,ID这样的数字型字段,还是存在数据库中查询比较好。
      

  14.   

    怎么解决的? DecimalFormat没有定义哦。要引入什么包吗?