Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Lucene RangeQuery没有';适当过滤_C#_Lucene_Lucene.net - Fatal编程技术网

C# Lucene RangeQuery没有';适当过滤

C# Lucene RangeQuery没有';适当过滤,c#,lucene,lucene.net,C#,Lucene,Lucene.net,我正在使用RangeQuery获取所有金额介于0到2之间的文档。 当我执行查询时,Lucene也会给我金额大于2的文档。我错过了什么 这是我的密码: Term lowerTerm = new Term("amount", minAmount); Term upperTerm = new Term("amount", maxAmount); RangeQuery amountQuery = new RangeQuery(lowerTerm, upperTerm, true); finalQue

我正在使用
RangeQuery
获取所有金额介于0到2之间的文档。 当我执行查询时,Lucene也会给我金额大于2的文档。我错过了什么

这是我的密码:

Term lowerTerm = new Term("amount", minAmount);
Term upperTerm = new Term("amount", maxAmount);

RangeQuery amountQuery = new RangeQuery(lowerTerm, upperTerm, true);

finalQuery.Add(amountQuery, BooleanClause.Occur.MUST);
下面是我索引中的内容:

doc.Add(new Field("amount", amount.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES));

更新:就像@basZero在他的评论中说的,从Lucene 2.9开始,你可以添加到你的文档中。只要记住在搜索时使用而不是RangeQuery

原始答案 Lucene将数字视为单词,因此它们的顺序是字母顺序:

0
1
12
123
2
22
这意味着对于Lucene,12在0和2之间。如果你想做一个适当的数字范围,你需要索引数字零填充,然后做范围搜索[0000到0002]。(所需的填充量取决于预期的值范围)

如果你有负数,只需为非负数再加一个零。(编辑:错误。请参阅更新)

如果您的数字包含一个分数部分,请保持原样,并且只对整数部分进行零填充

例如:


更新:负数有点棘手,因为按字母顺序-1在-2之前。给出了在Lucene中处理负数和一般数字的完整解释。基本上,您必须将数字“编码”为使项目顺序有意义的内容。

我创建了一个PHP函数,用于将数字转换为lucene/solr范围搜索表

0.5
转换为
1000000000.5

-0.5
转换为
0999999999.5

function luceneNumeric($numeric)
{
    $negative = $numeric < 0;
    $numeric = $negative ? 10000000000 + $numeric : $numeric;

    $parts = explode('.', str_replace(',', '.', $numeric));

    $lucene = $negative ? 0 : 1;
    $lucene .= str_pad($parts[0], 10, '0', STR_PAD_LEFT);
    $lucene .= isset($parts[1]) ? '.' . $parts[1] : '';

    return $lucene;
}
函数luceneNumeric($numeric) { $negative=$numeric<0; $numeric=$negative?1000000000+$numeric:$numeric; $parts=explode('.',str_replace(',','.',$numeric)); $lucene=$negative?0:1; $lucene.=str_pad($parts[0],10,'0',str_pad_左); $lucene.=isset($parts[1])?“。$parts[1]:”; 返回$lucene; }
这似乎是工作,希望这有助于别人

您能告诉我如何使用rangequery查询十进制数吗?谢谢!对于小数(我假设你指的是带有分数成分的小数),你需要放大它们,例如乘以一百万,去掉任何余数:1.2->1200000。你乘以的数量取决于你需要精确到多少个小数位……当然,正如itsadok所说,你仍然需要对它们进行零填充。我应该说1.2->000120万不需要扩大规模。按字母顺序,1.234排在1.3之前。使用Lucene 2.9.x,您可以在索引中添加数字。
000000
000001
000003.1415
000022
function luceneNumeric($numeric)
{
    $negative = $numeric < 0;
    $numeric = $negative ? 10000000000 + $numeric : $numeric;

    $parts = explode('.', str_replace(',', '.', $numeric));

    $lucene = $negative ? 0 : 1;
    $lucene .= str_pad($parts[0], 10, '0', STR_PAD_LEFT);
    $lucene .= isset($parts[1]) ? '.' . $parts[1] : '';

    return $lucene;
}