Skip to content

Instantly share code, notes, and snippets.

@lindstromhenrik
lindstromhenrik / gist:4564976
Created January 18, 2013 14:40
Filtering on a GeoLocation dictionary with Dictionary2Find
public class DictionariesWithGeolocation
{
[Fact]
public void FilterByValueInDictionary()
{
new Story("Filter by matching a key/geolocation in a dictionary")
.InOrderTo("be able to filter on a geolocation of a specific key in a dictionary")
.AsA("developer")
.IWant("to be able to map dictionaries to their correct value type")
.WithScenario("map dictionaries to their correct value type")
@lindstromhenrik
lindstromhenrik / gist:4654694
Created January 28, 2013 11:14
Excluding CultureInfo from the type of nested object.
// NestedFieldNameConvention
// we want to nest only lists of objects not previously mapped to a specific type by EPiServer Find
if ((type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
&& !(type.GetGenericArguments()[0].IsValueType || type.GetGenericArguments()[0] == typeof(string) || type.GetGenericArguments()[0] == typeof(DateTime) || type.GetGenericArguments()[0] == typeof(CultureInfo))
&& !fieldName.Contains("$$")
&& !fieldName.StartsWith("__"))
{
fieldName += "$$nested";
}
@lindstromhenrik
lindstromhenrik / TermsFacetExtensions.cs
Last active December 12, 2015 00:18
TermsFacetFor<> - properties of objects in IEnumerables
public static class TermsFacetExtensions
{
public static ITypeSearch<TSource> TermsFacetFor<TSource, TListItem>(this ITypeSearch<TSource> search, Expression<Func<TSource, IEnumerable<TListItem>>> fieldListSelector, Expression<Func<TListItem, object>> fieldListItemSelector)
{
fieldListSelector.ValidateNotNullArgument("fieldListSelector");
fieldListItemSelector.ValidateNotNullArgument("fieldListItemSelector");
var facetName = fieldListSelector.GetFieldPath() + "." + fieldListItemSelector.GetFieldPath();
var fieldName = search.Client.Conventions.FieldNameConvention.GetFieldName(fieldListSelector) + "." + search.Client.Conventions.FieldNameConvention.GetFieldName(fieldListItemSelector);
@lindstromhenrik
lindstromhenrik / gist:5451390
Created April 24, 2013 11:10
Extension for using FilterHits with BuildFilter()
public static ITypeSearch<TSource> FilterHits<TSource>(this ITypeSearch<TSource> search, Filter filter)
{
return new Search<TSource, IQuery>(search, context =>
{
var filterToAdd = filter;
if (context.RequestBody.Filter.IsNotNull())
{
filterToAdd = filter & context.RequestBody.Filter;
}
context.RequestBody.Filter = filterToAdd;
@lindstromhenrik
lindstromhenrik / gist:6699210
Last active December 23, 2015 21:49
List indices (docs/size)
$.ajax
({
type: "GET",
url: "https://server/_status",
dataType: 'jsonp'
}).done(function(data) {
for(indexName in data.indices)
{
$('#indices').append('<div><span>' + indexName + '</span>&nbsp;<span>' + data.indices[indexName].docs.num_docs + '</span>&nbsp;<span>' + data.indices[indexName].index.primary_size +'</span></div>');
}
@lindstromhenrik
lindstromhenrik / gist:8647566
Created January 27, 2014 12:17
Custom tags tracking using EPiServer Find CMS 6 R2
// At query time add appropriate tags
using EPiServer.Find.Framework.Statistics;
client.Search<>()
...
.Track(new string[] { "mytag" })
.GetResults()
// When fetching autocomplete/didyoumean/spellcheck through the REST endpoint
@lindstromhenrik
lindstromhenrik / gist:9779858
Last active December 2, 2016 17:03
ThenByScore-extension for EPiServer Find
public static ITypeSearch<TSource> ThenByScore<TSource>(this ITypeSearch<TSource> search)
{
return new Search<TSource, IQuery>(search, context =>
context.RequestBody.Sort.Add(new Sorting("_score")));
}
@lindstromhenrik
lindstromhenrik / HighlightExtensions.cs
Created March 14, 2017 14:08
Highlighting all attachment fields
public static class HighlightExtensions
{
public static IQueriedSearch<TSource, TExistingQuery> WithAttachmentHighlight<TSource, TExistingQuery>(
this IQueriedSearch<TSource, TExistingQuery> search, Action<FieldHighlightRequest> highlightAction = null)
where TExistingQuery : QueryStringQuery
{
return new Search<TSource, TExistingQuery>(search, context =>
{
var fieldName = "*$$attachment";
var request = new FieldHighlightRequest(fieldName);
@lindstromhenrik
lindstromhenrik / QueryStringSearchExtension.cs
Created April 4, 2017 10:23
InAnalyzedField-extension
public static class QueryStringSearchExtensions {
public static IQueriedSearch<TSource, QueryStringQuery> InAnalyzedField<TSource, TExistingQuery>(
this IQueriedSearch<TSource, TExistingQuery> search,
Expression<Func<TSource, string>> fieldSelector,
double? relativeImportance = null)
where TExistingQuery : QueryStringQuery
{
fieldSelector.ValidateNotNullArgument("fieldSelector");
return search.InField(search.Client.Conventions.FieldNameConvention.GetFieldNameForAnalyzed((Expression)fieldSelector), relativeImportance);
@lindstromhenrik
lindstromhenrik / Program.cs
Created September 19, 2017 02:30
NestPercolation
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NestPercolation
{
class Program
{
static void Main(string[] args)