【问题标题】:How to do a RavenDB query over multiple (complex structure) fields and return the matched values?如何对多个(复杂结构)字段进行 RavenDB 查询并返回匹配的值?
【发布时间】:2018-07-29 10:40:14
【问题描述】:

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

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

注意,districts 属性也可以为空。

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

我用.Search().Suggest() 尝试了几个选项,但都无法达到目标。

【问题讨论】:

    标签: c# indexing lucene ravendb


    【解决方案1】:

    为了同时搜索市名称和地区名称,您可以构建一个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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多