gpt4 book ai didi

c# - 如何对多个(复杂结构)字段进行 RavenDB 查询并返回匹配的值?

转载 作者:太空狗 更新时间:2023-10-29 19:42:08 24 4
gpt4 key购买 nike

我有一个 RavenDB 3.5 集合“Municipalities”,其文档结构如下:

{
"Name": ...,
...,
"Districts": [
{
"Name": ...,
...
}
]
}

请注意,districts 属性也可以为 null。

现在我正在开发一个打字头功能,您可以在其中搜索城市名称和地区名称。所以我想(通配符全部)查询这两个字段并取回匹配的值。所以我不想要整个文档,因为如果匹配的是地区名称,我就无法轻松返回该值。

我已经用 .Search().Suggest() 尝试了几个选项,但无法完全实现。

最佳答案

为了同时搜索城市名称和地区名称,您可以构建一个 MultiMap index这会将来自同一个集合的名称映射两次,一次来自市政当局名称,一次会散布在地区名称上。

索引查询的结果将是包含所有数据的文档。为了从索引中获得特定结果,您可以 store the data你想在索引中检索。这样只有预期的数据会返回,而不是所有文档。

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);
}
}

关于c# - 如何对多个(复杂结构)字段进行 RavenDB 查询并返回匹配的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com