Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Last active December 10, 2015 21:59
Show Gist options
  • Save jasondentler/4498700 to your computer and use it in GitHub Desktop.
Save jasondentler/4498700 to your computer and use it in GitHub Desktop.
RavenDB Faceted Search blog post
// Run when we initialize the database, along with uploading indices and stuff
session.Store(new FacetSetup()
{
Facets = HomeSearchFacetSetup.Facets().ToList(),
Id = HomeSearchFacetSetup.HomeSearchFacetDocumentId
});
namespace DWH.Website.Facets
{
public class HomeSearchFacetSetup
{
public const string HomeSearchFacetDocumentId = "facets/SearchIndex";
public static IEnumerable<Facet> Facets()
{
yield return new Facet<MapResult>
{
Name = r => r.SchoolDistricts
};
yield return new Facet<MapResult>
{
Name = r => r.MinPrice,
Ranges =
{
r => r.MinPrice > int.MinValue,
r => r.MinPrice > 100000,
r => r.MinPrice > 125000,
// Lots more ranges here
r => r.MinPrice > 1250000,
r => r.MinPrice > 1275000,
}
};
// Lots more facets here
}
}
}
using System;
using System.Linq;
using DWH.Website.ViewModels;
using Raven.Abstractions.Indexing;
using Raven.Client.Indexes;
namespace DWH.Website.Indices.Search
{
public class PlanSearchIndex : AbstractMultiMapIndexCreationTask<MapResult>
{
public const string TheIndexName = "Plan/Search";
public override string IndexName
{
get { return TheIndexName; }
}
public PlanSearchIndex()
{
AddMap<FloorPlan>(floorPlans =>
from floorPlan in floorPlans
let community = LoadDocument<Community>(floorPlan.Community.Id)
let schoolDistricts = community.SchoolIds.Select(id => LoadDocument<School>(id).SchoolDistrictName).ToArray()
select new MapResult
{
// Lots of boring property setting here
}
);
AddMap<Showcase>(showcases =>
from showcase in showcases
let community = LoadDocument<Community>(showcase.Community.Id)
let schoolDistricts = community.SchoolIds.Select(id => LoadDocument<School>(id).SchoolDistrictName).ToArray()
select new MapResult
{
// Lots of boring property setting here
}
);
TransformResults = (database, results) =>
results.Select(result => new TransformResult
{
Community = database.Load<Community>(result.Community.Id),
FloorPlan =
result.Id.StartsWith("floor", StringComparison.CurrentCultureIgnoreCase)
? database.Load<FloorPlan>(result.Id)
: null,
Showcase =
result.Id.StartsWith("floor", StringComparison.CurrentCultureIgnoreCase)
? null
: database.Load<Showcase>(result.Id)
});
Index(r => r.Amenities, FieldIndexing.NotAnalyzed);
Index(r => r.Geolocation, FieldIndexing.NotAnalyzed);
Index(r => r.SchoolDistricts, FieldIndexing.NotAnalyzed);
}
}
}
AddMap<FloorPlan>(floorPlans =>
from floorPlan in floorPlans
let community = LoadDocument<Community>(floorPlan.Community.Id)
let schoolDistricts = community.SchoolIds.Select(id => LoadDocument<School>(id).SchoolDistrictName).ToArray()
select new MapResult
{
// Lots of boring property setting here
}
);
AddMap<Showcase>(showcases =>
from showcase in showcases
let community = LoadDocument<Community>(showcase.Community.Id)
let schoolDistricts = community.SchoolIds.Select(id => LoadDocument<School>(id).SchoolDistrictName).ToArray()
select new MapResult
{
// Lots of boring property setting here
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment