4

辞書に属性のリストを含むドキュメントを使用するアプリケーションがあります。何らかの理由で、これらの属性に対して静的インデックスとクエリ/フィルターを使用する必要があります。

プロトタイプは次のようになります。

class Program
{
    static void Main(string[] args)
    {
        IDocumentStore store = new DocumentStore() { DefaultDatabase = "Test", Url = "http://localhost:8081" };
        store.Initialize();

        IndexCreation.CreateIndexes(typeof(Program).Assembly, store);

        using (var session = store.OpenSession())
        {
            session.Store(new Document { Id = "1", Name = "doc_name", Attributes = new Dictionary<string, object> { { "Type", "1" }, { "Status", "Active" } } });
            session.SaveChanges();
        }

        using (var session = store.OpenSession())
        {
            // works
            var l1 = session.Query<Document, Documents_Index>().Where(a => a.Attributes["Type"] == "1").ToList();
            // not working
            var l2 = session.Query<Document, Documents_Index>().Where(a => a.Attributes["Status"] == "Active").ToList();
        }
    }
}

public class Documents_Index : AbstractIndexCreationTask<Document>
{
    public Documents_Index()
    {
        Map = docs => docs.Select(a =>
         new
         {
             a.Name,
             a.Attributes,
             Attributes_Type = a.Attributes["Type"]
         });
    }
}

[Serializable]
public class Document
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Dictionary<string, object> Attributes { get; set; }
}

しかし、任意の属性名/値を使用してクエリを実行する必要があるため、このインデックスは問題を解決します。実際には、属性のリストは実行時に認識されます(したがって、Map式を変更して任意の数の属性名を挿入しようとしましたが、これまでのところ成功しませんでした)。動的な方法でインデックスを定義する方法はありますか?

4

1 に答える 1

6

あなたはそれを次のように書く必要があります:

public class Documents_Index : AbstractIndexCreationTask<Document>
{
    public Documents_Index()
    {
        Map = docs => docs.Select(a =>
         new
         {
             a.Name,
             _ = a.Attributes.Select(x=>CreateField("Attributes_"+x.Key, x.Value),
         });
    }
}
于 2012-07-13T07:53:28.560 に答える