Created
December 20, 2012 09:59
-
-
Save anonymous/4344296 to your computer and use it in GitHub Desktop.
Indexed Linq POC
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Text; | |
using Lucene.Net.Analysis.Standard; | |
using Lucene.Net.Index; | |
using Lucene.Net.Linq; | |
using Lucene.Net.Linq.Mapping; | |
using Lucene.Net.Store; | |
using NUnit.Framework; | |
using Version = Lucene.Net.Util.Version; | |
namespace ConsoleApplication1 | |
{ | |
class IndexedQueryable<T> : IQueryable<T> where T : new() | |
{ | |
private IQueryable<T> q; | |
public IndexedQueryable(IEnumerable<T> items) | |
{ | |
var directory = new RAMDirectory(); | |
var standardAnalyzer = new StandardAnalyzer(Version.LUCENE_29); | |
var writer = new IndexWriter(directory, standardAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED); | |
var provider = new LuceneDataProvider(directory, standardAnalyzer, Version.LUCENE_29, writer); | |
using (var session = provider.OpenSession<T>()) | |
{ | |
foreach (var item in items) | |
{ | |
session.Add(item); | |
} | |
} | |
q = provider.AsQueryable<T>(); | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return q.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return q.GetEnumerator(); | |
} | |
public Expression Expression { | |
get { return q.Expression; } | |
} | |
public Type ElementType { get { return q.ElementType; } } | |
public IQueryProvider Provider { get { return q.Provider; } } | |
} | |
public class Tests | |
{ | |
[Test] | |
public void Test() | |
{ | |
var article = new Article { Author = "John Doe", BodyText = "some body text", PublishDate = DateTimeOffset.UtcNow }; | |
var articles = new List<Article> { article }; | |
var q = new IndexedQueryable<Article>(articles); | |
var searchResults = (from a in q | |
where a.SearchText == "some search query" | |
select a).ToArray(); | |
Assert.AreEqual(article, searchResults[0]); | |
} | |
} | |
public class Article | |
{ | |
protected bool Equals(Article other) | |
{ | |
return Id == other.Id; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != this.GetType()) return false; | |
return Equals((Article) obj); | |
} | |
public override int GetHashCode() | |
{ | |
return Id.GetHashCode(); | |
} | |
public Article() | |
{ | |
} | |
public string Author { get; set; } | |
public string Title { get; set; } | |
public DateTimeOffset PublishDate { get; set; } | |
// Stores the field as a NumericField | |
[NumericField] | |
public long Id { get; set; } | |
// Stores the field as text | |
public int IssueNumber { get; set; } | |
[Field(IndexMode.NotIndexed, Store = StoreMode.Yes)] | |
public string BodyText { get; set; } | |
// Maps to field "text" | |
[Field("text", Store = StoreMode.No)] | |
public string SearchText | |
{ | |
get { return string.Join(" ", new[] { Author, Title, BodyText }); } | |
} | |
// Add IgnoreFieldAttribute to properties that should not be mapped to/from Document | |
[IgnoreField] | |
public string IgnoreMe { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment