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);但是无法查出相应数据
请问如何更改
先谢谢了
我有数据 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);但是无法查出相应数据
请问如何更改
先谢谢了
解决方案 »
- 关于TcpClient收发数据的小问题,求解
- 关于DataGridView数据转换的问题
- FillRectangle可以填充Rectangle的内部,如果我想填充Rectangle的外部该怎么办?
- vs2008的安装问题,报unable to find baseline data,请高手赐教,有图有真相。
- 写Xml title属性设置?????
- 求助:关于类型导出导入程序
- 双系统不能启动
- Memcached捕捉对象释放
- C#中模运算(求余)是哪个?
- 在C#中使用正则表达式的问题
- C# 定义行数不固定的二维数组
- 求助一个C# WinForm下,关于System.OutOfMemoryException的一个问题
例如我现在查询一个关键词为"打印机" auto_id 大于1000的记录 SELECT * FROM TB WHERE ID>1000 AND TITLE LIKE '%打印机%'
你这样查询看有没有数据
是查1~1000的数据
改为
RangeFilter filter = new RangeFilter("auto_id", "1000", "100000", true, true);
试试
Hits hits = searcher.Search(query, filter);
参考
RangeFilter 的第二个和第三个参数是 string 的....
参考里面我也是过了 建立索引和搜索的时候 使用了LongToString 也是无数据
Query query = q.Parse("打印机"); 搜索title,必须是Store.YES,另外,如果还是没有结果,有可能是StandardAnalyzer对中文的支持有问题
http://topic.csdn.net/u/20090916/14/0281240d-45ce-4ee5-a380-9ddaa22f015f.html?23083
我将语句改成 Query query = q.Parse("auto_id:[1 TO 10]");
出来数据只有两个..id 是1 的核id 是0 的
在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看看吧。
我提个建议吧,
不要把所有的东西都往Lucene里面放,他的索引库是有大小限制的。而且为了速度,尽量只放文本数据,
如Title,和Content
像,ID这样的数字型字段,还是存在数据库中查询比较好。