0
votes

I'm trying to create a static index where I want all documents where a key exists and has a value. The value itself is not important, only the key exists.

I'm exploring this example with dynamic fields: https://ravendb.net/docs/article-page/2.5/csharp/client-api/advanced/dynamic-fields

... and although I'm getting the index to work, I'm not sure if the query I'm using is correct.

This is the sample class:

public class Result
{
    public Dictionary<string, List<Data>> Results { get; set; }
}

The key in the dictionary is the ID of a user (for example "user/1") and the value is a list of data-objects. The so the json-structure looks like this:

{
  "Results" :
  {
    "user/1": [{...}],
    "user/2": [{...}],
  }
}

The index I use is this:

public class Result_ByUserId : AbstractIndexCreationTask<Result>
{
    public Result_ByUserId()
    {
        Map = res => from r in res
                          select new
                          {
                              _ = r.Results
                                  .Select(d => CreateField(d.Key, d.Value))
                          };
    }
}

My problem comes down to the query, as it assumes I want to look at a specific key and value.

var resultat = session.Advanced.DocumentQuery<Result>("Result/ByUserId ")
                .WhereEquals("user/1", "") // How do I write a !isNullOrEmpty?
                .ToList();

... which I don't want to do. I only want the results that has a key in which the value is not null or empty. Does anybody have any good tips?

1

1 Answers

0
votes

What you can do is index a boolean flag depending on if the dictionary has a value or not and then query on that.

public class Result_ByUserId : AbstractIndexCreationTask<Result>
{
    public Result_ByUserId()
    {
        Map = res => from r in res
                        select new
                        {
                            _ = r.Results
                                .Select(d => CreateField(d.Key, d.Value != null ? true : false, false, true))
                        };
    }
}

The query can then be:

var resultat = session.Advanced.DocumentQuery<Result>("Result/ByUserId ")
    .WhereEquals("user/1", true) 
    .ToList();

This will return any Result documents that has a Dictionary with a key of user/1 and a dictionary value that's not null.

Not sure it's the best way of doing it, but it worked for me...

Hope this helps!