-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Web.Mvc; | ||
using NewYork.Models; | ||
using System.Linq; | ||
using Raven.Client.Linq; | ||
|
||
namespace NewYork.Controllers | ||
{ | ||
public class ShirtsController : RavenController | ||
{ | ||
public ActionResult Search() | ||
{ | ||
RavenQueryStatistics stats; | ||
var q = Session.Query<Shirt>() | ||
.Statistics(out stats) | ||
.Where(x => x.Types.Any(t => t == new ShirtType { Color = "Red", Size = "XL" })) | ||
.Where(x => x.Types.Any(t => t == new ShirtType { Color = "Blue", Size = "M" })); | ||
|
||
return Json(new | ||
{ | ||
Query = q.ToString(), | ||
Results = q.ToList(), | ||
stats.IndexName | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace NewYork.Models | ||
{ | ||
public class Shirt | ||
{ | ||
public string Name { get; set; } | ||
public string Id { get; set; } | ||
public ShirtType[] Types { get; set; } | ||
} | ||
|
||
public class ShirtType | ||
{ | ||
public string Size { get; set; } | ||
public string Color { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Raven.Abstractions.Data; | ||
using Raven.Client; | ||
using Raven.Client.Document; | ||
using Raven.Client.Indexes; | ||
using Raven.Client.Linq; | ||
|
||
namespace UsersSearch | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using(var docStore = new DocumentStore | ||
{ | ||
Url = "http://172.16.1.55:8080", | ||
DefaultDatabase = "Users" | ||
}.Initialize()) | ||
{ | ||
|
||
IndexCreation.CreateIndexes(typeof(User).Assembly, docStore); | ||
|
||
while (true) | ||
{ | ||
var search = Console.ReadLine(); | ||
|
||
using(var session = docStore.OpenSession()) | ||
{ | ||
DoQuery(session, search); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
private static void DoQuery(IDocumentSession session, string search) | ||
{ | ||
var q = session.Query<Users_Search.Result, Users_Search>() | ||
.Search(x => x.Query, search) | ||
.As<User>(); | ||
|
||
var user = q.FirstOrDefault(); | ||
if (user == null) | ||
{ | ||
var suggestionQueryResult = q.Suggest(new SuggestionQuery()); | ||
switch (suggestionQueryResult.Suggestions.Length) | ||
{ | ||
case 0: | ||
Console.WriteLine("Nothing found, bummer"); | ||
break; | ||
case 1: | ||
DoQuery(session, suggestionQueryResult.Suggestions[0]); | ||
break; | ||
default: | ||
Console.WriteLine("Did you mean?"); | ||
foreach (var suggestion in suggestionQueryResult.Suggestions) | ||
{ | ||
Console.WriteLine("\t{0}", suggestion); | ||
} | ||
break; | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("{0} {1}", user.Name, user.Email); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace UsersSearch | ||
{ | ||
public class User | ||
{ | ||
public string Name { get; set; } | ||
public string Email { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform> | ||
<ProductVersion>8.0.30703</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>UsersSearch</RootNamespace> | ||
<AssemblyName>UsersSearch</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<TargetFrameworkProfile>Client</TargetFrameworkProfile> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> | ||
<PlatformTarget>x86</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> | ||
<PlatformTarget>x86</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Raven.Abstractions"> | ||
<HintPath>..\NewYork\bin\Raven.Abstractions.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Raven.Client.Lightweight"> | ||
<HintPath>..\NewYork\bin\Raven.Client.Lightweight.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.ComponentModel.Composition" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="User.cs" /> | ||
<Compile Include="Users_Search.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Linq; | ||
using Raven.Abstractions.Indexing; | ||
using Raven.Client.Indexes; | ||
|
||
namespace UsersSearch | ||
{ | ||
public class Users_Search : AbstractIndexCreationTask<User, Users_Search.Result> | ||
{ | ||
public class Result | ||
{ | ||
public string Query; | ||
} | ||
|
||
public Users_Search() | ||
{ | ||
Map = users => | ||
from user in users | ||
select new | ||
{ | ||
Query = new object[] | ||
{ | ||
user.Name, | ||
user.Email, | ||
user.Email.Split('@') | ||
} | ||
}; | ||
Index(x => x.Query, FieldIndexing.Analyzed); | ||
} | ||
} | ||
} |