如何对多个(复杂结构)字段和 return 匹配值进行 RavenDB 查询?
How to do a RavenDB query over multiple (complex structure) fields and return the matched values?
我有一个 RavenDB 3.5 集合 "Municipalities",其文档结构如下:
{
"Name": ...,
...,
"Districts": [
{
"Name": ...,
...
}
]
}
请注意,地区 属性 也可以为空。
现在我正在开发一个打字头功能,您可以在其中搜索城市名称和地区名称。所以我想(通配符全部)查询这两个字段并取回匹配的值。所以我不想要整个文档,因为如果匹配是在地区名称上,我就不能轻易 return 该值。
我已经尝试了 .Search()
和 .Suggest()
的几种选择,但无法完全实现。
为了同时搜索城市名称和地区名称,您可以构建一个 MultiMap index,它将两次映射同一集合中的名称,一次来自城市名称,一次在地区中散开名字.
索引查询的结果将是包含所有数据的文档。为了从索引中获得特定结果,您可以 store the data 您想要在索引中检索。这样只有预期的数据会 return 而不是所有文档。
public class MunicipalitiesAndDistrictsNamesIndex : AbstractMultiMapIndexCreationTask<MunicipalitiesAndDistrictsNamesIndex.Result>
{
public class Result
{
public string Name { get; set; }
public string Value { get; set; }
}
public MunicipalitiesAndDistrictsNamesIndex()
{
AddMap<Municipality>(municipality => from m in municipalities
select new
{
m.Name,
m.Value,
});
AddMap<Municipality>(municipality => from m in municipalities
from d in m.Districts
select new
{
d.Name,
d.Value,
});
// mark 'Name' field as analyzed which enables full text search operations
Index(x => x.Name, FieldIndexing.Search);
// storing fields so when projection
// requests only those fields
// then data will come from index only, not from storage
Store(x => x.Name, FieldStorage.Yes);
Store(x => x.Value, FieldStorage.Yes);
}
}
我有一个 RavenDB 3.5 集合 "Municipalities",其文档结构如下:
{
"Name": ...,
...,
"Districts": [
{
"Name": ...,
...
}
]
}
请注意,地区 属性 也可以为空。
现在我正在开发一个打字头功能,您可以在其中搜索城市名称和地区名称。所以我想(通配符全部)查询这两个字段并取回匹配的值。所以我不想要整个文档,因为如果匹配是在地区名称上,我就不能轻易 return 该值。
我已经尝试了 .Search()
和 .Suggest()
的几种选择,但无法完全实现。
为了同时搜索城市名称和地区名称,您可以构建一个 MultiMap index,它将两次映射同一集合中的名称,一次来自城市名称,一次在地区中散开名字.
索引查询的结果将是包含所有数据的文档。为了从索引中获得特定结果,您可以 store the data 您想要在索引中检索。这样只有预期的数据会 return 而不是所有文档。
public class MunicipalitiesAndDistrictsNamesIndex : AbstractMultiMapIndexCreationTask<MunicipalitiesAndDistrictsNamesIndex.Result>
{
public class Result
{
public string Name { get; set; }
public string Value { get; set; }
}
public MunicipalitiesAndDistrictsNamesIndex()
{
AddMap<Municipality>(municipality => from m in municipalities
select new
{
m.Name,
m.Value,
});
AddMap<Municipality>(municipality => from m in municipalities
from d in m.Districts
select new
{
d.Name,
d.Value,
});
// mark 'Name' field as analyzed which enables full text search operations
Index(x => x.Name, FieldIndexing.Search);
// storing fields so when projection
// requests only those fields
// then data will come from index only, not from storage
Store(x => x.Name, FieldStorage.Yes);
Store(x => x.Value, FieldStorage.Yes);
}
}