public
anonymous / gist:2473684
Created

RavenDB.Index.SelectMany

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Database.Server;
using Raven.Json.Linq;
 
namespace RavenDBSample
{
public class SimpleFullTextSearch
{
private static void Main(string[] args)
{
var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
};
 
documentStore.Initialize();
 
// Create the index.
IndexCreation.CreateIndexes(typeof(ProductsSearch).Assembly, documentStore);
 
// Create some fake data.
SeedFakeData(documentStore);
 
// Open ManagementCenter to verify index & data is created
WaitForUserToContinue(documentStore);
 
// Now lets search.
Console.WriteLine("searching for the query: Notebook");
Console.WriteLine("Should return 2 procuts");
Search(documentStore, "Notebook");
}
 
private static void SeedFakeData(IDocumentStore documentStore)
{
if (documentStore == null)
{
throw new ArgumentNullException("documentStore");
}
 
using (var session = documentStore.OpenSession())
{
// First product
var firstProduct = new Product
{
Title = "A Notebook from HP",
Tags = new List<Tag>
{
new Tag
{
Name = "HP",
Aliases = new List<Tag> { new Tag {Name = "Hewlett Packard"} }
},
new Tag
{
Name = "Notebook",
Aliases = new List<Tag> { new Tag {Name = "Laptop"} }
}
}
};
 
// Second Product
var secondProduct = new Product
{
Title = "A Notebook from Lenovo",
Tags = new List<Tag>
{
new Tag {Name = "Lenovo"},
new Tag
{
Name = "Notebook",
Aliases = new List<Tag> { new Tag {Name = "Laptop"} }
}
}
};
 
session.Store(firstProduct);
session.Store(secondProduct);
session.SaveChanges();
}
}
 
private static void Search(IDocumentStore documentStore, string query)
{
if (documentStore == null)
{
throw new ArgumentNullException("documentStore");
}
 
if (string.IsNullOrEmpty(query))
{
throw new ArgumentNullException("query");
}
 
using (var session = documentStore.OpenSession())
{
// Now lets chain the searching.
var products = session.Query<ProductsSearch.Result, ProductsSearch>()
.Customize(x => x.WaitForNonStaleResults())
.SearchMultiple(x => x.Query, query, options: SearchOptions.And)
.As<Product>()
.ToList();
 
Console.WriteLine("Found {0} results.", products.Count);
foreach (var product in products)
{
Console.WriteLine("Product => Id: {0}, Name: {1}, TagsCound: {2}",
product.Id, product.Title, product.Tags.Count);
}
}
}
 
private static void WaitForUserToContinue(EmbeddableDocumentStore documentStore)
{
if (Debugger.IsAttached == false)
return;
 
documentStore.DatabaseCommands.Put("Pls Delete Me", null,
 
RavenJObject.FromObject(new { StackTrace = new StackTrace(true) }),
new RavenJObject());
 
documentStore.Configuration.AnonymousUserAccessMode = AnonymousUserAccessMode.All;
using (var server = new HttpServer(documentStore.Configuration, documentStore.DocumentDatabase))
{
server.StartListening();
Process.Start(documentStore.Configuration.ServerUrl); // start the server
 
do
{
Thread.Sleep(100);
} while (documentStore.DatabaseCommands.Get("Pls Delete Me") != null && Debugger.IsAttached);
}
}
}
 
public class Product
{
public Product()
{
Tags = new List<Tag>();
}
 
public string Id { get; set; }
public string Title { get; set; }
public IList<Tag> Tags { get; set; }
}
 
public class Tag
{
public Tag()
{
Aliases = new List<Tag>();
}
 
public string Name { get; set; }
public IList<Tag> Aliases { get; set; }
}
 
public class ProductsSearch : AbstractIndexCreationTask<Product, ProductsSearch.Result>
{
public class Result
{
public string Query { get; set; }
}
 
public ProductsSearch()
{
Map = products => from product in products
select new
{
Query = new object[]
{
product.Title,
product.Tags.Select(tag => tag.Name),
product.Tags.SelectMany(tag => tag.Aliases, (tag, alias) => alias.Name)
}
};
 
Index(x => x.Query, FieldIndexing.Analyzed);
}
 
public override string IndexName
{
get
{
return "Products/Search";
}
}
}
 
public static class RavenQueryableExtensions
{
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, string queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (string.IsNullOrEmpty(queries))
{
throw new ArgumentNullException("queries");
}
 
var searchValues = queries.Split(' ');
 
return self.SearchMultiple(fieldSelector, searchValues, boost, options);
}
 
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, IEnumerable<string> queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (queries == null)
{
throw new ArgumentNullException("queries");
}
 
return queries.Aggregate(self, (current, query) => current.Search(fieldSelector, query, boost, options));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.