我最近误认为 Nhibernate.Search 会将我类中的整数属性作为数字字段进行索引。
[Indexed]
public class Location : Entity
{
[IndexedEmbedded(Depth = 1, Prefix = "Country_")]
public virtual Country Country { get; set; }
[Field(Index.Tokenized)]
public virtual string Name { get; set; }
[Field(Index.Tokenized)]
public virtual string AlternativeNames { get; set; }
[Field(Index.Tokenized)]
public virtual string OriginalNames { get; set; }
[Field(Index.UnTokenized)]
public virtual string LocationType { get; set; }
[Field()]
public virtual int? Population { get; set; }
}
但是当我像这样为查询设置排序时:
var words = query.Split(' ');
var luceneQuery = string.Join(" AND ", words.Select(x => "Name:{0}*".F(x)));
luceneQuery += " AND LocationType:locality";
var results = search.CreateFullTextQuery<Location>(luceneQuery)
.SetSort(new Sort(new SortField("Population", CultureInfo.CurrentCulture, true)))
.SetMaxResults(100)
.List<Location>();
它返回按数字排序的结果,其风格与单词排序相同,如下所示:
City Country Region Population
New London United States North America 998
Nueva Londres Paraguay South America 971
New London United States North America 967
Londonderry United Kingdom British Islands 92133
London Kiribati Micronesia 921
London United States North America 8122
London United Kingdom British Islands 7869322
New London United States North America 7316
所以我的问题是,由于 Nhibernate.Search 将其视为文本字段,我如何才能将其更改为数字字段,是否可以转换或是否必须为每条记录重新编制索引。其中 340K。
我开始觉得 Nhibernate.Search 的便利性如果不能做到这一点就失去了。也许我必须重新开始并使用普通的 Lucene.Net?
感谢您的帮助
最佳答案
更新
可能有用的链接:
- Complex Searching / Querying With NHibernate ——这不就达到你的目的了吗?所描述的示例允许按各种数值和范围进行搜索。
- Quickstart tutorial for NHibernate.Search
- http://linqtolucene.codeplex.com/
- Lucene hibernate search NumericRangeQuery<Float> not working on field defined with @NumericField
- Best place for NHibernate documentation?
- Hibernate Search: Apache Lucene™ Integration Reference Guide
我认为您关于数字和范围搜索的两个问题都在这里得到解决:http://find.searchhub.org/link?url=http://wiki.apache.org/lucene-java/SearchNumericalFields
Because Apache Lucene is a full-text search engine and not a conventional database, it cannot handle numerical ranges (e.g., field value is inside user defined bounds, even dates are numerical values). We have developed an extension to Apache Lucene that stores the numerical values in a special string-encoded format with variable precision (called trie, all numerical values like doubles, longs, Dates, floats, and ints are converted to lexicographic sortable string representations and indexed with different precisions).
...
Numeric fields can be sorted on (a special parser is included into FieldCache) and used in function queries (through FieldCache)
我还通过 Range searches for numbers 找到了一个 LongClass.java
实现:
I implemented a "LongField" that encodes any +ve or -ve long into a string that sorts correctly. I posted that class here: http://www.mail-archive.com/lucene-dev@jakarta.apache.org/msg04790.html
Doing a String range search should be fairly straight forward from there. Let me know if you have any problems.
关于nhibernate - 如何在 Nhibernate.Search 中添加数字字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119385/