请告诉我如何在字典字段上正确进行全文搜索。
我已将所有可翻译字段编入索引:
public class TranTest
{
public string Id { get; set; }
public IDictionary<string, string> Trans1 { get; set; }
public IDictionary<string, string> Trans2 { get; set; }
//for index queries
public IDictionary<string, string> Trans { get; set; }
}
public class TranTestIndex : AbstractIndexCreationTask<TranTest>
{
public TranTestIndex()
{
Map = docs =>
from doc in docs
select new
{
_ = doc.Trans1.Select(x => CreateField("Trans_" + x.Key, x.Value)),
Trans2 = doc.Trans2.Select(x => CreateField("Trans_" + x.Key, x.Value)),
/* etc. */
};
Index("Trans_en", FieldIndexing.Analyzed);
Index("Trans_fi", FieldIndexing.Analyzed);
/* etc. */
}
}
这(查询转换为:Trans_en :( term))返回正确的结果:
var luceneQ = session.Advanced.LuceneQuery<TranTest, TranTestIndex>().Search(x => x.Trans["en"], searchTerms).ToList();
但我更喜欢使用IRavenQueryable API。不幸的是,该API中的类似查询会产生InvalidOperationException:无法理解如何将'x.Trans.get_Item(“en”)'转换为RavenDB查询:
var ravenQ = session.Query<TranTest, TranTestIndex>().Search(x => x.Trans["en"], searchTerms);
results = ravenQ.ToList(); //Exception!
这很奇怪,考虑到这种方式完美无缺(转换为:Trans_en:“term *”):
session.Query<TranTest, TranTestIndex>().Where(x => x.Trans["en"].StartsWith(searchTerms))
我对此问题的疑问:
答案 0 :(得分:1)
你发现了一个错误。我已通过these unit tests验证,并已将其提交至RavenDB issue tracker。
要解决此问题,要么使用Lucene查询方法,要么(正如您在第二个问题中提到的那样),您可以自定义IRavenQueryable的基础DocumentQuery,如下所示:
session.Query<TranTest, TranTestIndex>()
.Customize(x => ((IDocumentQuery<TranTest>) x)
.Search(q => q.Trans["en"], searchTerms))