如何在多个(复杂结构)字段上执行RavenDB查询并返回匹配的值?

Koe*_*oen 8 c# lucene indexing ravendb

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

{
  "Name": ...,
  ...,
  "Districts": [
    {
       "Name": ...,
       ...
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,zone属性也可以为null.

现在我正在开发一个类型头功能,您可以在其中搜索市政名称和地区名称.所以我想(通配符全部)查询两个字段,并获取匹配的值.所以我不想要整个文档,因为如果匹配是在区域名称上,我不能轻易返回该值.

我试过几个选项.Search().Suggest()但不能完全到达那里.

Fit*_*aki 5

为了搜索市镇名称和地区名称,您可以构建一个MultiMap 索引,该索引会将同一集合中的名称映射两次,一次来自市镇名称,一次将在地区名称上扇出。

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)