Skip to content

Commit

Permalink
More fun stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Aug 22, 2012
1 parent 16c3a67 commit b58958d
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 4 deletions.
22 changes: 22 additions & 0 deletions NewYork.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,38 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{02072A
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsersSearch", "UsersSearch\UsersSearch.csproj", "{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Debug|x86.ActiveCfg = Debug|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Release|Any CPU.Build.0 = Release|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{289EBBA6-9C1D-485C-8A8D-84FBC6D260D2}.Release|x86.ActiveCfg = Release|Any CPU
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Debug|Any CPU.ActiveCfg = Debug|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Debug|Mixed Platforms.Build.0 = Debug|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Debug|x86.ActiveCfg = Debug|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Debug|x86.Build.0 = Debug|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Release|Any CPU.ActiveCfg = Release|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Release|Mixed Platforms.ActiveCfg = Release|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Release|Mixed Platforms.Build.0 = Release|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Release|x86.ActiveCfg = Release|x86
{5E69816E-D21D-4C6C-A9CC-67AB12788ECF}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions NewYork/Controllers/AuctionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace NewYork.Controllers
{
public class AuctionController : RavenController
{
public ActionResult Strange()
{
Session.Advanced.LoadStartingWith<Bid>("customers/1/cars");

return Json("In a Strange Land");
}
public ActionResult Create()
{
var auction = new Auction
Expand Down
17 changes: 17 additions & 0 deletions NewYork/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
using System.Web.Mvc;
using NewYork.Models;
using System.Linq;
using Raven.Abstractions.Data;
using Raven.Client.Document;
using Raven.Client.Linq;

namespace NewYork.Controllers
{
public class HomeController : RavenController
{
public ActionResult Patch()
{
DocumentStore.DatabaseCommands.UpdateByIndex("Raven/DocumentsByEntityName",
new IndexQuery
{
Query = "Tag:Teams"
}, new ScriptedPatchRequest
{
Script = @"
this.Name = this.Name.replace('team #', '');
"
});

return Json("Okay");
}

public ActionResult Dynamic()
{
var load = Session.Load<dynamic>("users/ayende");
Expand Down
26 changes: 26 additions & 0 deletions NewYork/Controllers/ShirtsController.cs
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
});
}
}
}
15 changes: 15 additions & 0 deletions NewYork/Models/Shirt.cs
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; }
}
}
12 changes: 8 additions & 4 deletions NewYork/NewYork.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Raven.Abstractions">
<HintPath>..\packages\RavenDB.Client.1.2.2067-Unstable\lib\net40\Raven.Abstractions.dll</HintPath>
<Reference Include="Raven.Abstractions, Version=1.2.0.0, Culture=neutral, PublicKeyToken=37f41c7f99471593, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\ravendb-1.2\Raven.Client.Lightweight\bin\Debug\Raven.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Raven.Client.Lightweight">
<HintPath>..\packages\RavenDB.Client.1.2.2067-Unstable\lib\net40\Raven.Client.Lightweight.dll</HintPath>
<Reference Include="Raven.Client.Lightweight, Version=1.2.0.0, Culture=neutral, PublicKeyToken=37f41c7f99471593, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\ravendb-1.2\Raven.Client.Lightweight\bin\Debug\Raven.Client.Lightweight.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Data.Entity" />
Expand Down Expand Up @@ -69,10 +71,12 @@
<Compile Include="Controllers\AuctionController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\RavenController.cs" />
<Compile Include="Controllers\ShirtsController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\Auction.cs" />
<Compile Include="Models\Shirt.cs" />
<Compile Include="Models\Team.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
74 changes: 74 additions & 0 deletions UsersSearch/Program.cs
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);
}
}
}
}
8 changes: 8 additions & 0 deletions UsersSearch/User.cs
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; }
}
}
66 changes: 66 additions & 0 deletions UsersSearch/UsersSearch.csproj
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>
30 changes: 30 additions & 0 deletions UsersSearch/Users_Search.cs
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);
}
}
}

0 comments on commit b58958d

Please sign in to comment.