1

I have some Ranged Facets defined in a FacetSetup document. I like having the ability to retrieve individual Facets from a FacetSetup (by specifying them instead of the Id of the FacetSetup in my call to ToFacets()), so I tried to do that with these Ranged Facets but have been unsuccessful so far.

Here is my failing test. Any tips?

using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Tests.Helpers;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace RavenDBTests
{
    public class RangedFacetTests : RavenTestBase
    {
        [Fact]
        public void RangedFacetTest()
        {
            using (EmbeddableDocumentStore documentStore = NewDocumentStore())
            {
                // create and store an index
                Dictionary<string, string> analyzers = new Dictionary<string, string>();
                analyzers.Add("MyProperty", "");
                Dictionary<string, SortOptions> sortOptions = new Dictionary<string, SortOptions>();
                sortOptions.Add("MyProperty", SortOptions.Long);

                IndexDefinition indexDefinition = new IndexDefinition()
                    {
                        Analyzers = analyzers,
                        SortOptions = sortOptions,
                        Map = @"from d in docs
select new
{
    MyProperty = d.MyProperty
}",
                    };

                documentStore.DatabaseCommands.PutIndex("MyIndex", indexDefinition);

                using (IDocumentSession documentSession = documentStore.OpenSession())
                {
                    // store some sample documents
                    documentSession.Store(new { MyProperty = 10 });
                    documentSession.Store(new { MyProperty = 25 });
                    documentSession.Store(new { MyProperty = 100 });

                    // store a facetsetup with one ranged facet
                    documentSession.Store(new FacetSetup
                    {
                        Id = "facets/MyFacetSetup",
                        Facets = new List<Facet>()
                        {
                            new Facet()
                            {
                                Mode = FacetMode.Ranges,
                                Name = "MyProperty_Range",
                                Ranges = new List<string>()
                                {
                                    "[0x0000000000000001 TO 0x0000000000000032]"
                                }
                            }
                        }
                    }, "facets/MyFacetSetup");

                    documentSession.SaveChanges();
                }

                // let that process
                WaitForIndexing(documentStore);

                using (IDocumentSession documentSession = documentStore.OpenSession())
                {
                    // retrieve ALL facets
                    FacetResults facetResults1 = documentSession.Query<dynamic>("MyIndex").ToFacets("facets/MyFacetSetup");

                    Xunit.Assert.True(facetResults1.Results.Values.First().Values.First().Hits > 0);

                    // retrieve SPECIFIED facets
                    FacetResults facetResults2 = documentSession.Query<dynamic>("MyIndex").ToFacets(new List<Facet>()
                        {
                            new Facet()
                            {
                                Mode = FacetMode.Ranges,
                                Name = "MyProperty_Range"
                            }
                        },
                        0,
                        null);

                    // this fails: why can't I specify the ranged facet?
                    Xunit.Assert.True(facetResults2.Results.Values.First().Values.First().Hits > 0);
                }
            }
        }
    }
}
DJ Grossman
  • 600
  • 6
  • 17

1 Answers1

2

You don't specify what the actual ranges are in the code.

In RavenDB, you have two ways to create facets. One is to specify the facet doc id, and the second is to actually pass the facets. In this case, you are passing a range facets without any ranges, so it returns no results. Use this code:

 FacetResults facetResults2 = documentSession.Query<dynamic>("MyIndex").ToFacets(new List<Facet>()
                        {
                           new Facet()
                            {
                                Mode = FacetMode.Ranges,
                                Name = "MyProperty_Range",
                                Ranges = new List<string>()
                                {
                                    "[0x0000000000000001 TO 0x0000000000000032]"
                                }
                            }
                        },
                        0,
                        null);
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41