gpt4 book ai didi

c# - RavenDB 索引 : Need a solution to merge 2 dictionary fields into a single dictionary, 将其展平并使其可搜索

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:44 31 4
gpt4 key购买 nike

我们正在为客户构建一个嵌套的 UI View ,需要一个解决方案来将 2 个字典字段合并到一个合并的字典中,并使键像字段名称一样可搜索。我设法使用 http://ravendb.net/docs/2.5/client-api/advanced/dynamic-fields 中提到的技术创建了一个 Map/Reduce 索引。和 https://groups.google.com/forum/#!msg/ravendb/c0HdJT-yyvQ/qvkVRrZfvmgJ .

public class ViewFolderResultWithIndividualProperties
{
public string EntryId { get; set; }
public List<KeyValuePair<string, string>> MetadataProperties { get; set; }
public List<KeyValuePair<string, string>> NamedProperties { get; set; }
public List<KeyValuePair<string, string>> Properties { get; set; }
public string FlattenedProperties { get; set; }
public string _ { get; set; }
}

MetadateProperties – 它是键值对的字典。例如,

"MetadataProperties": [
{
"Key": "JobName",
"Value": "one job"
},
{
"Key": "Organization",
"Value": "foo"
}]

NamesProperties – 它是已知键值对的字典。例如,

"NamedProperties": [
{
"Key": "Tags",
"Value": ""
},
{
"Key": "Name",
"Value": "file-184"
},
{
"Key": "Uploader",
"Value": "rmani@transper.com"
},
{
"Key": "FileType",
"Value": "Jpg"
},
{
"Key": "Language",
"Value": "English"
}]

Properties – 它是一个合并的字典,包含来自 MetadataProperties 和 NamedProperties 的键值对。

FlattenedProperties 和 _ 是分别包含“NamedProperties”和“MetadataProperties”的扁平化字段值的属性。我想不出一种方法来展平像“Properties”这样的计算属性(它结合了 MetadataProperties 和 NamedProperties 字典)。我试过连接

这是索引创建代码:

public class PortalEntryViews_DocumentIdSplitIndex : AbstractIndexCreationTask<PortalEntry, ViewFolderResultWithIndividualProperties>
{
public PortalEntryViews_DocumentIdSplitIndex()
{
Map = portalEntries => from portalEntry in portalEntries
select new
{
EntryId = portalEntry.Id,
MetadataProperties = portalEntry.MetaData.Select(t => new KeyValuePair<string, string>(t.Key, t.Value)).ToList(),
NamedProperties = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Tags", string.Join(",", portalEntry.Tags.Where(t => !t.IsInternal).Select(t=>t.Name))),
new KeyValuePair<string, string>("Name", portalEntry.Name),
new KeyValuePair<string, string>("Uploader", portalEntry.Uploader),
new KeyValuePair<string, string>("FileType", portalEntry.FileType),
new KeyValuePair<string, string>("Language", portalEntry.Language),
new KeyValuePair<string, string>("Name", portalEntry.Name) },
Properties = new List<KeyValuePair<string, string>>(),
FlattenedProperties = "",
_ = ""
};


Reduce = results => from result in results
group result by new { result.EntryId, result.MetadataProperties, result.NamedProperties, result.FlattenedProperties, result._ } into g
select new
{
EntryId = g.Key.EntryId,
MetadataProperties = g.Key.MetadataProperties,
NamedProperties = g.Key.NamedProperties,
Properties = g.Key.MetadataProperties.Concat(g.Key.NamedProperties).ToList(),
FlattenedProperties = g.Key.NamedProperties.Select(f => CreateField(f.Key, f.Value)),
_ = g.Key.MetadataProperties.Select(t => CreateField(t.Key, t.Value, true, true))
};


}
}

当我直接从 RavenDb Explorer 运行“Language:English”这样的查询时,它会工作并返回一个投影。而当我在 C# 代码中使用 LuceneQuery 运行相同的查询时:

var entries =
session.Advanced.LuceneQuery<ViewFolderResultWithIndividualProperties>(
"PortalEntryViews/DocumentIdSplitIndex")
.WhereEquals("Language", "English").ToList();

我收到这个错误:

Raven.Imports.Newtonsoft.Json.JsonSerializationException : Could not read value for property: FlattenedProperties  ----> Raven.Imports.Newtonsoft.Json.JsonReaderException : Error reading string. Unexpected token: StartArray 

我的最终目标是使用 CreateField() 将组合字典(即属性字段)展平为单个字段,可以使用键进行搜索,就好像它们是字段名称一样。但是,如果我使用这样的调用:

Properties = g.Key.MetadataProperties.Concat(g.Key.NamedProperties).ToList().Select(t => CreateField(t.Key, t.Value, true, true)),它似乎可以运行,但是当您从 Ravendb Explorer 查看索引时,它会显示实际错误:

阶段:索引部分:Reduce 描述:“System.Collections.Generic.List”不包含“Select”的定义

现在,我只能将一个字典 (MetadataProperties) 平展到 reduce 部分的那个“_”字段中,它在 Ravendb Explorer 和 C# 代码中都可以使用 LuceneQuery 工作,但这不符合我的要求。

有人可以帮我解决这个问题吗?

最佳答案

如果只想搜索key-value,可以很简单

//the index
public class FlattenIndex: AbstractIndexCreationTask<PortalEntry>
{
public class ReduceResult
{
public string Key { get; set; }
public string Value { get; set; }
}
public FlattenIndex()
{
Map = portalEntries => from portalEntry in portalEntries
from p in portalEntry.MetadataProperties.Concat(portalEntry.NamedProperties)
select new
{
Key=p.Key,
Value=p.Value
};
}
}

//the query
using (var session = _docStore.OpenSession())
{
var someEntries = session.Query<FlattenIndex.ReduceResult, FlattenIndex>()
.Where(x => x.Key == "Language" && x.Value == "English")
.As<PortalEntry>()
.ToArray();
if (someEntries!=null)
foreach(var entry in someEntries )
{
Console.WriteLine(entry.Id);
}
}

关于c# - RavenDB 索引 : Need a solution to merge 2 dictionary fields into a single dictionary, 将其展平并使其可搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573809/

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